Call to a member function update() on null [LARAVEL]

Related searches

I am trying to make a Laravel application that allows a logged-in user to edit one of their memorial pages. The edit view works, but as soon as the changes are saved, it throws a Call to a member function update() on null error. I am new to Laravel, so I apologize if this is a stupid question.

Controller:

public function edit(Memorial $memorial)
{
    $this->authorize('update', $memorial);

    return view('memorials.edit', compact('memorial'));
}

public function update(Memorial $memorial)
{
    $this->authorize('update', $memorial);

    $data = request()->validate([
        'fname' => 'required',
        'lname' => 'required',
        'bio' => '',
    ]);

    auth()->user()->memorial->update($data);

    return redirect("/memorial/{$memorial->id}");
}

View:

<div class="container pt-5">
<form action="/memorial/{{ $memorial->id }}" enctype="multipart/form-data" method="POST">
    <legend>Edit Memorial</legend>
    @csrf
    @method('PATCH')
    <div class="form-group">
        <div class="form-row">
            <div class="col-6">
                <input id="fname" type="text" class="form-control @error('fname') is-invalid @enderror" name="fname" placeholder="First Name" value="{{ old('fname') ?? $memorial->fname }}" autofocus>

                @error('fname')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
            <div class="col-6">
                <input id="lname" type="text" class="form-control @error('lname') is-invalid @enderror" name="lname" placeholder="Last Name" value="{{ old('lname') ?? $memorial->lname }}">

                @error('lname')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="form-row">
            <div class="col-6">
                <input id="bio" type="text" class="form-control @error('bio') is-invalid @enderror" name="bio" placeholder="Biography" value="{{ old('bio') ?? $memorial->bio }}">

                @error('bio')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                @enderror
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="form-row">
            <div class="col-12">
                <button class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</form>

Policy:

/**
 * Determine whether the user can update the memorial.
 *
 * @param  \App\User  $user
 * @param  \App\Memorial  $memorial
 * @return mixed
 */
public function update(User $user, Memorial $memorial)
{
    return $user->id == $memorial->user_id;
}

Your relation is incorrect...You don't have memorial relation in User model. So it returns null

You can easily edit one memorial as below code:

public function update(Memorial $memorial)
{
    $this->authorize('update', $memorial);

    $data = request()->validate([
        'fname' => 'required',
        'lname' => 'required',
        'bio' => '',
    ]);

    $memorial->update($data);

    return redirect("/memorial/{$memorial->id}");
}

because in authorize you check the privacy when you edit $memorial that is what you need

Call to a member function update() on null, Call to a member function update() on null RequisitionController.php public The OP really needs to take a Laravel from Scratch tutorial. The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

There are a few things that may be causing this error. I think the first place to investigate is on your relationship vs. what you are trying to update.

Your User model has many memorials:

 public function memorials(){
     return $this->hasMany(Memorial::class);
} 

Your controller is trying to update one memorial:

auth()->user()->memorial->update($data);

To fix, I suggest you try to directly update the memorial in question, rather than through the user relationship. I assume there is a user_id key on the memorial - just add that in as you update the memorial that you have already injected into the update method.

Call to a member function update() on null (i am trying to request the , Call to a member function update() on null (i am trying to request the users data and update). Posted 3 years ago by Shawdow. //This my routes. Configuration read from F:\Repositories\acme\phpunit.xml PHP Fatal error: Call to a member function make() on null in F:\Repositories\acme\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php on line 164 Now the things that I have tried: I have tried. deleting local composer vendor folder and using composer update

so I see the reason is: in User model you define the memory relation:

public function memorials() 
{ 
return $this->hasMany(Memorial::class); 
}

in function update() you call: auth()->user()->memorial->update($data); you are calling a model, and it has proposed to get property of Memorial model.

you need to call like this: in function update():

auth()->user()->memorials()->update($data)

in User model

/**
 * Eloquent:User
 *
 * //relation
 * @property Collection|Memorial[] $memorials
 */
class User extends Model

Verification Issues with Laravel, Verification Issues with Laravel | (1/1) FatalThrowableError Call to a member function update() on null. Posted 1 year ago by AntreasPaps. Laravel - Call to a member function create() on null Posted 2 months ago by noblemfd I am trying to use Laravel-5.8 and sendgrid to send notification and also save into the database.

Call to a member function update() on a non-object, I keep getting this error when I try to update my users. Call to a member function update() on a non-object. Here is my edit.blade.php Call to a member function getClientOriginalExtension() on null Posted 1 year ago by Amalmax hello, I am working with laravel 5.6 and going to update existing image on uploads table in my edit.blade.php form, my edit view is as following,

Call to a member function update() on null [LARAVEL , Call to a member function update() on null [LARAVEL]. I am trying to make a Laravel application that allows a logged-in user to edit one of their memorial pages. Call to a member function users() on null Posted 3 years ago by GrahamMorbyDev Ok so i have a many to many relasionship here and im getting a Call to a member function users() on null error

Hello Good Developers, This is Not a Direct Duplicate of: Call to a member function setCookie() on null As I am calling a Controller action from Middleware and then in the controller action I am

Comments
  • The user has no memorial either data is bad or the relationship is setup wrong
  • This is in my User.php: public function memorials() { return $this->hasMany(Memorial::class); }
  • does memorial row has the correct id or soft deleted?
  • What are you referring to when you say "memorial row"? The memorial row in the database? Also, users can have multiple memorials. Currently, I have 1 test user and 1 test memorial linked to that user. The test user id=1 and the memorial id=1. The memorial and the user are linked by the user_id, as shown in the policy I created above.^
  • This is in my User.php: public function memorials() { return $this->hasMany(Memorial::class); }
  • @JakeScervino yes you have memorials... don't have memorial, and you use memorial as relation
  • @JakeScervino I add correct code. I hope it helps you
  • So you're saying I need to change 'public function memorials()' in my User model to 'public function memorial()' instead?
  • @JakeScervino This is back to your strategy. How many memoirs each user has? but without relation you can handle your problem. Just update $memorial object that you get as input of your update function
  • The user has many memorials but is only trying to update a single one. When I use auth()->user()->memorials()->update($data), it updates the data for every memorial the user has, which is not the desired effect. I want the user to update the data for a single memorial.
  • Yes, this makes sense. See the bottom part of my answer - you don't need to update through the relationship. Use the memorial you have injected in to the method and update that model directly. By adding the user_id, it will be attached to the user as desired. This will only update the one memorial as you want.
  • The user has many memorials but is only trying to update a single one. When I use auth()->user()->memorials()->update($data), it updates the data for every memorial the user has, which is not the desired effect. I want the user to update the data for a single memorial.
  • This is more along the lines of what I am looking for: auth()->user()->memorials($memorial['id'])->update($data); ---- (except that updates the data every memorial for that user).
  • you can add memorial_id into it by where(). auth()->user()->memorials()->where(memorial_id, $memorial_id)->update($data) it is like @MohammadHosseini suggest