Call to a member function update() on null

Published 3 months ago by irankhosravi

What is this error?

Call to a member function update() on null

RequisitionController.php

public function update(Request $request, SubmitApplication $submitApplication)
{
    $submitApplication = SubmitApplication::find($request['id'])->update(['approved', 1]);
    return redirect()->back();
}

SubmitApplication.php

class SubmitApplication extends Model
{
    public function school_lists()
    {
        return $this->belongsTo(SchoolsList::class);
    }

    public function requisition()
    {
        return $this->hasMany(Requisition::class);
    }
}

I want to update approved form 0 to 1.

BishoyWagih

First of all make sure that the request has an id, fix the synatx for the update it accespts array so you can do it with update[‘approved’ => 1] , also pass thr foregin key in the belongsTo relation

tykus
tykus
3 months ago (661,190 XP)

The error specifically means that SubmitApplication::find($request['id']) returned null. This may be because $request['id'] is itself null, or some value that is not in the database.

You are type-hinting SubmitApplication $submitApplication in the update method signature which should give you an instance using Route Model binding (if your route wildcard is called {submitApplication};are you getting an instance?

public function update(Request $request, SubmitApplication $submitApplication)
{
    $submitApplication->update(['approved' => 1]);
    return redirect()->back();
}
Cronix
Cronix
3 months ago (733,460 XP)

It looks like you've already retrieved the model using route model binding, so you shouldn't have to fetch it again.

public function update(Request $request, SubmitApplication $submitApplication)
{
    // we already retrieved SubmitApplication via route model binding, so just use it.
    $submitApplication->update(['approved' => 1]); // notice the array syntax
    return redirect()->back();
}

And @BishoyWagih is correct, your array is wrong. It needs to be an array of key/value pairs. The way you wrote it, it's sending 2 values "approved" and "1".

irankhosravi

I want to update id 5 for example update approved 0 to 1.

How to do it?

School

Cronix
Cronix
3 months ago (733,460 XP)

By posting to yoursite.com/url/to/update/5, and making the changes that we suggested. You didn't post your route, so can't say for sure. You should show that.

BishoyWagih

How do you submit the form? Are you passing the id with the request?

tykus
tykus
3 months ago (661,190 XP)

What does the route definition look like?

Route::put('requisitions/{submitApplication}', '[email protected]');

If you have this, then the $submitApplication argument find the correct instance whenever you PUT /requisitions/5.

Then do as I described above.

irankhosravi

all.blade.php

<form class="btn-group" action="{{ route('requisitions.update', ['id' => $requisition->id]) }}" method="post">
    {{ method_field('PATCH') }}
    {{ csrf_field() }}
    <button type="submit" class="btn btn-success btn-xs">تایید</button>
</form>

web.php

$this->resource('requisitions', 'RequisitionController');

RequisitionController.php

public function update(Request $request, SubmitApplication $submitApplication)
{
    $submitApplication->update(['approved' => 1]);
    return redirect()->back();
}
tykus
tykus
3 months ago (661,190 XP)

Why is it $requisition->id in the form, and a $submitApplication in the method signature? Your entities appear to be confused...

rin4ik
rin4ik
3 months ago (200,570 XP)

you could update your model like below

<form class="btn-group" action="{{ route('requisitions.update',$requisition]) }}" method="post">
    {{ method_field('PATCH') }}
    {{ csrf_field() }}
    <button type="submit" class="btn btn-success btn-xs">تایید</button>
</form>
public function update(Request $request, Requisition $requisition)
{
    $requisition->update(['approved' => 1]);
    return redirect()->back();
}

but very confused in your case . try to debug with dd

tykus
tykus
3 months ago (661,190 XP)

The trick with Route-Model binding is to name the method argument the same as the route wildcard. If you php artisan route:list you will see that the Route::resource() method expands to (amongst others):

PUT  /requisitions/{requisition}    [email protected]

The wildcard here is {requisition} and so we expect that we are updating a Requisition resource (model instance), not a SubmitApplication instance

irankhosravi

@rin4ik

I get this error

MassAssignmentException

approved

Cronix
Cronix
3 months ago (733,460 XP)

Add 'approved' to the $fillable array on your Requisition model, and read the docs: https://laravel.com/docs/5.6/eloquent#mass-assignment

BishoyWagih

to avoid MassAssignmentException you can do the following,

in the model

define protected $guarded property like

protected $guarded = [];

OR

define an array containing all table fields that want to update or create like

protected $fillable = ['requisition_id' , 'school_id' , 'approved' , 'application']; 
irankhosravi

But I get this error again.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'approved' in 'field list' (SQL: update requisitions set updated_at = 2018-06-20 22:06:42, approved = 1 where id = 1)

Requisition.php

protected $fillable = ['approved','updated_at'];

Please sign in or create an account to participate in this conversation.