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

Issue

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;
}

Solution

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

Answered By – Mohammad Hosseini

Answer Checked By – Candace Johnson (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published