PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 20, 2022

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

 April 20, 2022     laravel, laravel-8, php     No comments   

Issue

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>

Solution

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


Answered By - Tim Lewis
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing