user17052553
user17052553

Reputation:

Model::update() should not be called statically

I'm working on a small project using PHP and Laravel I try to update my model but I'm getting an error message :

message "Non-static method Illuminate\\Database\\Eloquent\\Model::update() should not be called statically"

This is my code :

AttributeOption::update(array_merge([
                    'sort_order' => $sortOrder++,
                ], $optionInputs), $optionId);

Upvotes: 4

Views: 8465

Answers (2)

Nima Tf
Nima Tf

Reputation: 71

You can do that like this too:

Model::find()
->update([
  'field1' => 'value1',
  'field2' => 'value2',
  ...
]);

Upvotes: 0

IGP
IGP

Reputation: 15879

You're using update() wrong.

mass update with conditions:

YourModel::where(/* some conditions */)
    ->update([
      'field1' => 'value1',
      'field2' => 'value2',
      ...
    ]);

mass update with no conditions

YourModel::query()
    ->update([
      'field1' => 'value1',
      'field2' => 'value2',
      ...
    ]);

single model update

$model = YourModel::where(/* some conditions */)->first();

$model->update([
  'field1' => 'value1',
  'field2' => 'value2',
  ...
]);

// Only accept fillable fields in the update

$model->fill([
  'field1' => 'value1',
  'field2' => 'value2',
  ...
])->save();

// Disregard fillable fields in the update

$model->forceFill([
  'field1' => 'value1',
  'field2' => 'value2',
  ...
])->save();

Upvotes: 10

Related Questions