Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jwavess's avatar

So the edit page runs smoothly, and brings up the values, however when I click update food items..

When I click "Update Food items" I get this error

ErrorException in FoodController.php line 50: Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically, assuming $this from incompatible context

Route file

Route::resource('food', 'FoodController');

FoodController file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Food;

use App\Http\Requests\CreateFoodRequest;

class FoodController extends Controller
{



    public function show($id) {
        $food = Food::find($id);

        return view('index')->with('food', $food);

    }

    public function create() {

        return view('vendor.create');
        
    }


    public function store(CreateFoodRequest $request) {
        $input = $request->all();
        $food = Food::create($input);

        return redirect('food/'.$food->id);
        
    }

    public function edit($id) {
        $food = Food::find($id);

        return view('edit')->with('food', $food);

    }


    public function update($id, Request $request){
        $food = Food::find($id);
        $food = Food::update($input);

        return redirect('food/'.$food->id);
        
    }


}

edit file

@extends('app')

@section('body')

<h2>Edit</h2>

{!! Form::model($food, ['method' => 'PATCH', 'url'=>'food']) !!}






<div class="form-group">


    {!! Form::label('vegetables', 'Vegetable item:') !!}



    {!! Form::text('vegetables', null, ['class' => 'form-control']) !!}


</div>







<div class="form-group">


    {!! Form::label('fruit', 'Fruit item:') !!}



    {!! Form::text('fruit', null, ['class' => 'form-control']) !!}


</div>







<div class="form-group">


    {!! Form::label('meat', 'Meat item:') !!}



    {!! Form::text('meat', null, ['class' => 'form-control']) !!}

</div>





<div class="form-group">


    {!! Form::label('grains', 'Grain item:') !!}

    

    {!! Form::text('grains', null, ['class' => 'form-control']) !!}
    
</div>






<div class="form-group">


    {!! Form::label('sugar', 'Sugar item:') !!}

    

    {!! Form::text('sugar', null, ['class' => 'form-control']) !!}

</div>






<div>

{!! Form::submit('Update Food items', ['class' => 'btn btn-primary form-control']) !!}






    {!! Form::close() !!}

</div>




@if($errors->any())
    <ul class="alert alert-danger">

        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>

        @endforeach

    </ul>
@endif


@stop('body')

Seth

0 likes
9 replies
nfauchelle's avatar

@jwavess

Well.. there is not static update method like that.

You can change

 $food = Food::find($id);
$food = Food::update($input);

to

$food = Food::find($id);
$food->fill($input);
$food->save();

I think there is a non static method so you could $food->update($input) but I haven't actually looked into what that does and if it saves or not. Use the fill and save and you should be set.

1 like
jwavess's avatar

@nfauchelle I made the change you recommended above and it worked, but I am receiving an new error.

MethodNotAllowedHttpException in RouteCollection.php line 219:

Seth

bottelet's avatar

It could be your

{!! Form::model($food, ['method' => 'PATCH', 'url'=>'food']) !!}

Try doing

{!! Form::model($food, ['method' => 'PATCH', 'url'=>'food/update']) !!}

or even better link it to the route

'route' => ['food.update', $food->id //instead of url
nfauchelle's avatar

You are missing the id of the item you are editing from your route...

{!! Form::model($food, ['method' => 'PATCH', 'url'=>'food']) !!}

Should really be like

{!! Form::model($food, ['method' => 'PATCH', 'url'=>'food/' . $food->id]) !!}

but even better, as suggested above, is to use the route names

{!! Form::model($food, ['method' => 'PATCH', 'route'=>['food.update', $food->id]]) !!}

You can see the names and things if you run php artisan route:list

jwavess's avatar

@nfauchelle hey, so I got that changed above as recommended. After submit an update its mentioning line 50 erroring out.

ErrorException in FoodController.php line 50: Undefined variable: input

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Food;

use App\Http\Requests\CreateFoodRequest;

class FoodController extends Controller
{



    public function show($id) {
        $food = Food::find($id);

        return view('index')->with('food', $food);

    }

    public function create() {

        return view('vendor.create');

    }


    public function store(CreateFoodRequest $request) {
        $input = $request->all();
        $food = Food::create($input);

        return redirect('food/'.$food->id);

    }

    public function edit($id) {
        $food = Food::find($id);

        return view('edit')->with('food', $food);

    }


    public function update($id, Request $request){
        $food = Food::find($id);
        $food->fill($input);
        $food->save();

        return redirect('food/'.$food->id);

    }


}
jimmck's avatar

You have a cut/paste error. function store defines it. update does not. I would suggest you read your code?......

cristian9509's avatar
Level 4
public function update($id, Request $request){
        $food = Food::find($id);
        //$food->fill($input);
    $food->fill($request->all());
        $food->save();

        return redirect('food/'.$food->id);

    }

That should fix it. You have no $input variable anywhere.

1 like
jwavess's avatar

@cristian9509 Thank you Cristian, I try and follow with Jeffery as closely as possible, besides doing a few things differently, when he says you can do this, this way or this way. But other than making my project about food and his about articles, there seems to be some errors that just wont happen for him, mainly because I believe it to be the difference in versions of Laravel and php, and the way methods he chooses over what I do. Im choosing different ones so I can comprehend how things work and why we use what, just so I'm not writing exactly what he is writing and not learning anything.

Seth

Snapey's avatar

You should be validating inputs.

also, you could use route model binding to eliminate the ->find() call

Your very first posting, you could have used update if you called it on the food

    $food->update($request->all);

Although i would not recommend this without validation

Please or to participate in this conversation.