Update Table Using Laravel Model
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Update Table Using Laravel Model: A Comprehensive Guide
Body:
Updating existing records in a table can be essential when it comes to maintaining the accuracy of data in your Laravel application. In this blog, we'll discuss the process of updating a table using a Laravel model, along with best practices and common errors to avoid.
Understanding Selection Model
Firstly, let us examine the model you have created for managing the team selection. Your "Selection" class inherits from "Model," which is a base class used in Laravel to interact with databases. In your class definition, a table name is specified using protected $table, and all available columns are defined under protected $fillable. This ensures that only these fields can be mass assigned to the model when creating or updating records.Updating Records Using Model Class
Given your form data in the input variable, you need to update the record with the new team selection. You can achieve this by using the appropriate Laravel functions like "update." The following code should be used in your controller to handle updating the table:public function storeFirstTeam() {
$input = Request::all();
Selection::find($input['id'])->update($input);
return redirect('first-team');
}
Here, we are using the "find" method on the model class to locate the record with the given ID. Once found, we call the "update" function and pass the form data as its argument. This function will set all the provided keys in your $fillable array to their respective values in the database table row for that specific record.
Common Errors and Solutions
Now, let us address the error you encountered: "Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically, assuming $this from incompatible context." This means that your code is trying to access the "update" function as a static method even though it's an instance method. To correct this error, simply replace the code with the above example given in your controller and ensure you are using the model instance rather than the class name. The corrected code should look like:public function storeFirstTeam() {
$input = Request::all();
Selection $selection = Selection::find($input['id']);
$selection->update($input);
return redirect('first-team');
}
In this example, we first find the record with the given ID using the model's "find" method and store it in a variable. Then we call the "update" function on that instance to update the record. This approach ensures that you are calling the necessary functions correctly and avoiding potential errors related to static methods or class names.