I have a model that is Company , and its controller CompanyController I want to do the operation of being able to update the data of a record for this I do the following
Route
Route::put('companies/{company}','Api\CompanyController@update');
Controller
public function update(Request $request, Company $company)
{
$request->validate([
'name' => 'required',
'enabled' => 'required',
]);
$company->update($request->all());
if ($company === null) {
return response()->json('Company Not found', 404);
}
return response()->json($company, 200);
}
How can you read in the doc. From Laravel , the update method expects an array with the values to update , which you capture with this syntax:
However at the moment your query does not work and you should consider that:
@csrf
as you can see in this example:Which would generate an error like this when trying to perform the
update
:What you can do at this point is:
Option 1
only
Use one of the orexcept
methods to indicate which values you expect to remain in$request
or not, like this:Option 2
Access the values that the object has
$request
( without the all method or that would transform it to an array ) through its dynamic properties in this way:end points
_token
in any of the columns and you can take the values of the$request
to proceed with your modifications.where
to establish where this should act or you run the risk of losing data due to a massive modification.Consider this example for the last point I mention: