0

I'm trying to save data from a form modal to database .

So , I'm using the following code :

Controller :

  public function store(Request $request)
{

   if ($files = $request->file('casting_photo'))

    {
  
       $destinationPath = public_path('public/castingimages'); // upload path         
       $profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
       $files->move($destinationPath, $profileImage);
      
            $cast = new Casting;

            $cast -> casting_name= $request->input('casting_name');
            $cast -> casting_cin= $request->input('casting_cin');
            $cast -> casting_email= $request->input('casting_email');
            $cast -> casting_phone= $request->input('casting_phone');  
            $cast -> casting_gender= $request->input('casting_gender');
            $cast -> casting_address= $request->input('casting_address');
            $cast -> casting_city= $request->input('casting_city');
            $cast-> casting_photo=$profileImage;

              $cast->save();

    }
    
}

View :

   <x-app-layout>
    <div class="container-fluid">
      <div class="row">
        <div class="col app-col">
          <div class="mb-2">
            <h1>Castings</h1>

            <div class="top-right-button-container">
              <button type="button" data-target="#castingmodel" data-toggle="modal" class="btn btn-outline-primary btn-lg top-right-button  mr-1">Add New
</button>
              <div class="col-xl-6">
                <div id="result"></div>
              </div>
            </div>

            </div>
            <div class="separator mb-5"></div>
          </div>
       
      </div>
    </div>



 <div class="modal fade" id="castingmodel" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalContentLabel">New message</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
    <form method="post"  class="needs-validation tooltip-label-right" id="formcast" novalidate enctype="multipart/form-data">
        {{ csrf_field() }}
            <div class="modal-body">
                <input type="hidden" id="id_hidden" name="id" />

                <div class="form-group position-relative error-l-50">
                  <label>Name</label>
                  <input type="text" class="form-control" name ="casting_name" id="casting_name" >
                  <div class="invalid-tooltip">Name is required!</div>
                </div>
                <div class="form-group position-relative error-l-50">
                  <label>CIN</label>
                  <input type="text" class="form-control" name="casting_cin" id="casting_cin" required>
                  <div class="invalid-tooltip">CIN is required!</div>
                </div>
                <div class="form-group position-relative error-l-50">
                  <label>EMAIL</label>
                  <input type="text" class="form-control" rows="2" name="casting_email" required>
                  <div class="invalid-tooltip">EMAIL is required!</div>
                </div>
                <div class="form-group position-relative error-l-50">
                  <label>PHONE</label>
                  <input type="number" class="form-control" rows="2" name="casting_phone" required>
                  <div class="invalid-tooltip">PHONE is required!</div>
                </div>
                <div class="form-group position-relative">
                  <label>Radio</label>
                  <div>
                    <div class="custom-control custom-radio">
                      <input type="radio" id="jQueryCustomRadio1" name="casting_gender"  class="custom-control-input" required value="homme">
                      <label class="custom-control-label" for="jQueryCustomRadio1">Homme</label>
                    </div>
                    <div class="custom-control custom-radio">
                      <input type="radio" id="jQueryCustomRadio2" name="casting_gender" class="custom-control-input" required value="femme">
                      <label class="custom-control-label" for="jQueryCustomRadio2" >Femme</label>
                    </div>
                  </div>
                </div>
                <div class="form-group position-relative error-l-50">
                  <label>ADDRESS</label>
                  <input type="text" class="form-control" rows="2" name="casting_address" required>
                  <div class="invalid-tooltip">ADDRESS is required!</div>
                </div>
                <div class="form-group position-relative error-l-50">
                  <label>CITY</label>
                  <input type="text" class="form-control" rows="2" name="casting_city" required>
                  <div class="invalid-tooltip">CITY is required!</div>
                </div>
                <div class="input-group mb-3">
                  <div class="input-group-prepend">
                    <span class="input-group-text">Upload</span>
                  </div>
                  <div class="custom-file">
                    <input type="file" name="casting_photo" class="custom-file-input" id="inputGroupFile01">
                    <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
                  </div>
                </div>

                <button type="submit" id="createBtn" class="btn btn-primary">Save</button>

                <div class="result"></div>
           
            </div>

               </form>
          </div>
        </div> 
      </div>




 <script
  src="https://code.jquery.com/jquery-3.6.0.js"
  integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
  crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>



<script type="text/javascript">

     $(document).ready(function(){


        $('#formcast').on('submit', function(e){
            e.preventDefault();

            $.ajax({
                type: "POST",
                url:"castings",
                data : $('#formcast').serialize(),
                success: function(response){
                    console.log(response)
                    $('#castingmodel').modal('hide')
                    alert("Data saved");
                },
                error: function(error){
                    console.log(error)
                    alert("Data not saved");
                }
            });
        });
     });
 
</script>
</x-app-layout>

It doesn't show any error and the data is not inserted into the database.

I checked the file .env the parameters of the database are correct.

I don't know if it is because of the input file that I'm uploading in the form

7
  • does it get to your function? Commented May 26, 2021 at 10:52
  • check that with returning something in function and console it in your response Commented May 26, 2021 at 10:53
  • also return your request in function and make sure it has your photo Commented May 26, 2021 at 10:54
  • I checked, it returns the response in function Commented May 26, 2021 at 10:59
  • and it has your photo? i think you are not sending your photo through you ajax request and so base on your code in your function, your data will not be saved Commented May 26, 2021 at 11:05

2 Answers 2

1

Instead of serialize ,you can use form data to submit your form like below

$('#formcast').on('submit', function(e){
            e.preventDefault();
   var formData = new FormData(this);
            $.ajax({
                type: "POST",
                url: '{{ url('/agents') }}',
                data: formData,
                success: function(response){
                    location.reload();
                },
                error: function(error){
                   
                }
            });

        });

Since in your backend you are creating record only if you have image uploaded .so make sure to upload file as well

Sign up to request clarification or add additional context in comments.

Comments

0

Please check this line:

 $cast = new Casting; 

Shouldn't it be

 $cast = new Casting();

with brackets? Also you are trying to get data from form

($cast -> casting_name= $request->input('casting_name'); ) 

while the content type of your jQuery ajax request is not application/x-www-form-urlencoded So 2 options: 1- get data from controller like this:

 $cast -> casting_name= $request->get('casting_name');

(with the 'get' instead of 'input'

2- Keep the controller and update the ajax content type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.