Yannick jansen
Yannick jansen

Reputation: 49

Image file is empty laravel using ajax

I have table users :

In adding all the input to the database it works very well .

And when inserting i would not insert the image i would just insert firstName, lastName, login, password and image i would like to assign a null.

But it doesn't work it shows me toastr.error ('', 'Error!'); , knowing that i remove required in the input image.

I try with this code:

Controller:

public function addUser(Request $request){
        $user = new User();
        $user->firstName = $request->firstName;
        $user->lastName = $request->lastName;
        $user->login = $request->login;
        $user->password = bcrypt($request->password);
        if(empty($request->image)){

            $user->image = NULL;

           } else{
                $custom_file_name = $request->image->getClientOriginalName();
                $path = $request->image->storeAs('avatars',$custom_file_name);
                $user->image = $path;
            }
        $user->save();
        return response()->json($user);   
    }

Ajax:

$(document).on('click', "#add", function() {

            var firstName = $('#firstName').val();
            var lastName = $('#lastName').val();
            var login = $('#login').val();
            var password = $('#password').val();
            var image  = $('#image').prop('files')[0];

            var form_data = new FormData();

            form_data.append('firstName', firstName);
            form_data.append('lastName', lastName);
            form_data.append('login', login);
            form_data.append('password', password);
            form_data.append('image', image);

                    $.ajax({
                      url: "{{action('UserController@addUser')}}",
                      method: 'POST',
                      data:form_data,
                      contentType: false,
                      processData: false,
                      success: function(data) {


                        toastr.success('', 'Success!');
                      },

                      error: function(){
                          toastr.error('', 'Error!');
                      }
                    });

View:

<form enctype="multipart/form-data">
 {{ csrf_field() }}
   <div class="form-group">
    <input type="text" id="lastName" autocomplete="off" class="form-control" />
   </div>
   <div class="form-group">
    <input type="text" id="firstName" autocomplete="off" class="form-control" />
   </div>
   <div class="form-group">
    <input type="text" id="login" autocomplete="off" class="form-control" />
   </div>
   <div class="form-group">
    <input type="password" id="password" autocomplete="off" class="form-control" />
   </div>
   <div class="form-group">
    <input type="file" id="image" name="image" autocomplete="off" class="form-control" />
   </div>
</form>
<button type="submit" id="add" class="btn default">Add</button>

Upvotes: 0

Views: 672

Answers (2)

Boghani Chirag
Boghani Chirag

Reputation: 225

Try this code

    if($request->hasFile('image')) {
            $file = $request->file('image');

            $name = $request['phone_number'].$request['phone_number'].'.'.$file->getClientOriginalExtension();

            $image['filePath'] = $name;
            $file->move('uploads', $name);

        }

Upvotes: 1

lewis4u
lewis4u

Reputation: 15027

Probably the column needs to be set to nullable (in your migration file) so you can save a null value i that column.

For example:

$table->string('email')->nullable();

or in your case:

$table->string('image')->nullable();

Upvotes: 2

Related Questions