Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Table Using Laravel Model

I've got a table for a sports team. The record shows the team selection and some other information. I want to update the record with the team selection. My model is thus:

class Selection extends Model {  protected $table = "selection";  protected $fillable = [     'loose',     'hooker',     'tight',     'secrow1',     'secrow2',     'blindflank',     'openflank',     'eight',     'scrum',     'fly',     'leftwing',     'rightwing',     'fullback',     'sub1',     'sub2',     'sub3',     'sub4',     'sub5' ]; 

}

So I have a form which gives all the data for the positions and gives the id for the record in the DB. In my controller, I've got:

public function storeFirstTeam() {     $input = Request::all();      Selection::update($input->id,$input);      return redirect('first-team'); } 

But I get the following error:

Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically, assuming $this from incompatible context

Can anyone point out my silly error?

like image 297
danjswade Avatar asked Feb 08 '16 21:02

danjswade


People also ask

How do you update a model table?

Right click on Model and select 'Update Model From Database'. The Table would be shown in the tab 'Add'. Select this table and Update the model.

How do you update a record in Laravel?

We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table. Run an update statement against the database.

What is fillable in Laravel?

Fillable means what columns in the table are allowed to be inserted, guarded means the model can't insert to that particular column.


2 Answers

Please check the code below and this would solve your problem:

Selection::whereId($id)->update($request->all()); 
like image 68
Jilson Thomas Avatar answered Sep 25 '22 01:09

Jilson Thomas


The error message tells you everything you know: you’re trying to call a method statically (using the double colons) that isn’t meant to be.

The update() method is meant to be called on a model instance, so first you need to retrieve one:

$selection = Selection::find($id); 

You can then can the update() method on that:

$selection->update($request->all()); 
like image 43
Martin Bean Avatar answered Sep 26 '22 01:09

Martin Bean