I'm trying to append the species
' ID to its name at the event of an update through an observer
, but for some reason it's somewhat getting looped hence the SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column
error:
SpeciesController@update
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Species $species
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Species $species)
{
$species->name = $request->name;
$species->sort = $request->sort;
$species->save();
$updatedSpecies = new SpeciesResource($species);
return response()->json(compact('updatedSpecies'), 200);
}
SpeciesObserver@updated
/**
* Handle the species "updated" event.
*
* @param \App\Species $species
* @return void
*/
public function updated(Species $species)
{
$species->name = "$species->name - $species->id";
$species->save();
}
The updated
event is always fired because you're also saving it inside. Use the updating
event instead.
/**
* Handle the species "updating" event.
*
* @param \App\Species $species
* @return void
*/
public function updating(Species $species)
{
$species->name = "$species->name - $species->id";
}