mozew's avatar
Level 6

Call to a member function move() on null

I want to upload an image with laravel 5.8

I use this codes

if (Input::file('image')) {  

And

if($request->has('image')) {

but it is not working. Call to a member function move() on null

    if($request->has('image')) {
        $image = $request->file('image');
        $filename = $image->getClientOriginalName();
        request()->$image->move(public_path('images/users'), $filename);
        $user->image = $request->file('image')->getClientOriginalName();
    }
0 likes
12 replies
psylogic's avatar

instead of

request()->$image->move(public_path('images/users'), $filename);

use

$image->move(public_path('images/users')', $fileName);
1 like
mozew's avatar
Level 6

@psylogic

get this error.

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'users_email_unique' (SQL: insert intousers(first_name,last_name,gender,mobile,code,address,birth_date,province_id,city_id,email,password,updated_at,created_at) values (john, sina, 1, 09389267856, 12602, avenue, 1366/05/05, 8, 87, [email protected], $2y$10$TO9BdA3Ec1cCKZ3KNbAymOngVZZIfzQOJITCfrRSxq0wBjlaN6XY2, 2019-06-08 19:53:58, 2019-06-08 19:53:58))

Cronix's avatar

It's in the docs: https://laravel.com/docs/5.8/requests#files

You may determine if a file is present on the request using the hasFile method:

Notice it's not the same method you are using to check if the request has a file present. You're using the regular one for regular input, not the one specifically for files.

1 like
mozew's avatar
Level 6

This is my validation:

   'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
psylogic's avatar
psylogic
Best Answer
Level 7

@IRANKHOSRAVI - Well that it will work in insert form

'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],

but use this in when you are updating a user

'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$user->id],

you do not want a validation error to be thrown because the user is already the owner of the e-mail address

1 like
Cronix's avatar

The marked solution has nothing at all to do with the stated problem.

Snapey's avatar

its gone from not knowing how to handle images, to sql error inserting duplicate records to a validation issue

This boy works fast

Please or to participate in this conversation.