Ahmed Shakil
Ahmed Shakil

Reputation: 3

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

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 class="form-group row">
          <input type="text" name="id" value="{{$list->id}}">

                    </div>
                    <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">User name</label>
                        <div class="col-sm-12 col-md-10">
                            <input class="form-control" value="{{$list->name}}" name="name" placeholder="Location" type="text" >
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Email</label>
                        <div class="col-sm-12 col-md-10">
                            <input class="form-control" value="{{$list->email}}" name="email" placeholder="Location" type="text">
                        </div>
                    </div>
        <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Mobile Number</label>
                        <div class="col-sm-12 col-md-10">
                            <input class="form-control" value="{{$list->phone}}" name="phone" placeholder="Location" type="text" >
                        </div>
                    </div>
        <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Address</label>
                        <div class="col-sm-12 col-md-10">
                            <input class="form-control" value="{{$list->address}}" name="address" placeholder="Location" type="text">
                        </div>
                    </div>
        <div class="form-group row">
                        <label class="col-sm-12 col-md-2 col-form-label">Nationality</label>
                        <div class="col-sm-12 col-md-10">
                            <input class="form-control" name="nationality" value="{{$list->nationality}}" placeholder="nationality" type="numeric">
                        </div>
                    </div>
        <div class="form-group row">
          <label class="col-sm-12 col-md-2 col-form-label">Date of birth</label>
          <div class="col-sm-12 col-md-10">
            <input type="text" class="form-control" placeholder="Birth Date" name="date_of_birth" value="{{$list->date_of_birth}}">
          </div>
        </div>

        <div class="form-group row">
          <label class="col-sm-12 col-md-2 col-form-label">Father Name</label>
          <div class="col-sm-12 col-md-10">
            <input type="text" class="form-control" placeholder="Father name" name="father_name" value="{{$list->father_name}}">
          </div>
        </div>
        <div class="form-group row">
          <label class="col-sm-12 col-md-2 col-form-label">Mother Name</label>
          <div class="col-sm-12 col-md-10">
            <input type="text" class="form-control" placeholder="mother name" name="mother_name" value="{{$list->mother_name}}">
          </div>
        </div>

        <div class="form-group">

        <div class="form-group form-float">
                      <div class="card">

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

                    <button class="btn btn-primary" type="submit">Update</button>

Upvotes: 0

Views: 1742

Answers (3)

Amir Daneshkar
Amir Daneshkar

Reputation: 779


function user_update(Request $request){
           $photo_name = '';
           $user= User::findOrFail($request->id);

           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->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'=>$photo_name,
               ]);

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

Upvotes: 0

Hamza Ahmed
Hamza Ahmed

Reputation: 15

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();

Upvotes: 1

Tim Lewis
Tim Lewis

Reputation: 29258

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]);

Upvotes: 1

Related Questions