mercredi 1 novembre 2017

Implicit route, with firstOrCreate instead of findOrFail

Is it possible to create an implicit route where if it is not found the thing is created? I am using Laravel 5.5.13.

For instance this is my implicit route:

Route::post('thumbs/{player}', 'ThumbController@store');

And in my controller it is this:

public function store(Request $request, Player $player)
{
    $thumb = new Thumb($request->all());
    $player->thumbs()->save($thumb);
    return response()->json($thumb, 201);
}

So now if I go to the endpoint of ..../api/thumb/1 it will create a thumb related with Player of id 1. However instead of a id number I wanted to provide it a string like this:

..../api/thumb/PLAYER_NAME

So example of ..../api/thumb/Blagoh, then my endpoint should first find if a player exists by name "Blagoh", and if it doesn't then it should create it. I couldn't figure this one out.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2z8rOvs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire