Update Table Using Laravel Model
Asked Answered
H

5

36

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?

Hove answered 8/2, 2016 at 21:43 Comment(2)
You have to first select the row you want to update. How would you get the id of the selection when you post the update request?Haircloth
Try Something like this: Selection::whereId($id)->update($request->except(['_method','_token']));Haircloth
H
68

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

Selection::whereId($id)->update($request->all());
Haircloth answered 8/2, 2016 at 21:52 Comment(2)
Does this method update() get rollBackif something goes wrong? If it's between a DB::beginTransaction()Shepley
The request can contain some custom fields, from form e.g. I put array like this:Foydabaranda::whereId($request->id_eslox)->update([ 'login' => $request->login_eslox, 'parol' => $request->parol_eslox, 'tuKiTu' => $request->xeli_tochka, ]); Also works as I wanted.Gamester
U
15

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());
Ujiji answered 8/2, 2016 at 21:51 Comment(0)
B
8

You should write it like given example below:

Selection::where('id', $input['id'])->update($input);
// Or use this using dynamic where
Selection::whereId($input['id'])->update($input);

Alternatively, you may write it like this as well:

Selection::find($input['id'])->fill($input)->save();
Brahe answered 8/2, 2016 at 21:50 Comment(0)
P
3

You can also simply update the fields manually:

Selection::whereId($id)->update($request->all());
Penult answered 25/8, 2021 at 4:20 Comment(1)
Please avoid code only responses. view stackoverflow.com/help/how-to-answer for more informationThorny
M
1

it is possible to update with primary key but in my case I dont have id field in the detail table. To do it just run a query like this:

DB::table("shop_menu_detail")
    ->where(['name' => 'old name', 'language' => 'english'])
    ->update(['name' => 'new name']);

where is used as it is.

Merriment answered 8/2, 2023 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.