Home > OS >  how to solve Call to a member function update() on null on laravel 8?
how to solve Call to a member function update() on null on laravel 8?

Time:03-26

I am getting this kind of error call to member function update on null(). shown my code below please anyone can solve all are the things are fine when am getting to the image file update in that time am getting this error.

here is my controller code ////

function user_update(Request $request){

           $user= User::findOrFail($request->id)->update([
             'name'=>$request->name,
             'email'=>$request->email,
             'address'=>$request->address,
             'nationality'=>$request->nationality,
             'date_of_birth'=>$request->date_of_birth,
             'father_name'=>$request->father_name,
             'mother_name'=>$request->mother_name,
             'gender'=>$request->gender,
             'n_photo'=>$request->n_photo,



           ]);


           if ($request->hasFile('n_photo')) {

               $photo_upload     =  $request ->n_photo;
               $photo_extension  =  $photo_upload -> getClientOriginalExtension();
               $photo_name       =  "toletx_auth_image_". $user . "." . $photo_extension;
               Image::make($photo_upload)->resize(452,510)->save(base_path('public/uploads/auth/'.$photo_name),100);
               User::find($user)->update([
               'n_photo'          => $photo_name,
                   ]);


                 }
           return back()->with('success','User information have been successfully Updated.');
     }

///////my blade file

                <form method="POST" action="{{ route('user_update') }}" enctype="multipart/form-data">
                    @csrf
                    <div >
          <input type="text" name="id" value="{{$list->id}}">

                    </div>
                    <div >
                        <label >User name</label>
                        <div >
                            <input  value="{{$list->name}}" name="name" placeholder="Location" type="text" >
                        </div>
                    </div>
                    <div >
                        <label >Email</label>
                        <div >
                            <input  value="{{$list->email}}" name="email" placeholder="Location" type="text">
                        </div>
                    </div>
        <div >
                        <label >Mobile Number</label>
                        <div >
                            <input  value="{{$list->phone}}" name="phone" placeholder="Location" type="text" >
                        </div>
                    </div>
        <div >
                        <label >Address</label>
                        <div >
                            <input  value="{{$list->address}}" name="address" placeholder="Location" type="text">
                        </div>
                    </div>
        <div >
                        <label >Nationality</label>
                        <div >
                            <input  name="nationality" value="{{$list->nationality}}" placeholder="nationality" type="numeric">
                        </div>
                    </div>
        <div >
          <label >Date of birth</label>
          <div >
            <input type="text"  placeholder="Birth Date" name="date_of_birth" value="{{$list->date_of_birth}}">
          </div>
        </div>

        <div >
          <label >Father Name</label>
          <div >
            <input type="text"  placeholder="Father name" name="father_name" value="{{$list->father_name}}">
          </div>
        </div>
        <div >
          <label >Mother Name</label>
          <div >
            <input type="text"  placeholder="mother name" name="mother_name" value="{{$list->mother_name}}">
          </div>
        </div>

        <div >

        <div >
                      <div >

                         <div >
                             <input type="file"  name="n_photo"  >
                             <img src="{{ asset('uploads/auth') }}/{{ $list->n_photo }}" alt="">
                         </div>
                     </div>
                    </div>

                    <button  type="submit">Update</button>

CodePudding user response:

You've got 2 main problems with this code:

User::find($user)->update(['n_photo' => $photo_name]);

First of all, the correct syntax would be to use $user->id, not $user:

User::find($user->id)->update(['n_photo' => $photo_name]);

Second, that is super redundant; $user is already the result of User::find(), so you're doubling up for no reason.

To fix your issue, simply adjust your code to:

$user->update(['n_photo' => $photo_name]);

CodePudding user response:

Just spacing issue please check these two lines

//Before // Wrong

$photo_upload = $request ->n_photo;

$photo_extension = $photo_upload -> getClientOriginalExtension();

//After //Correct

$photo_upload = $request->n_photo;

$photo_extension = $photo_upload->getClientOriginalExtension();

  • Related