user1469734
user1469734

Reputation: 801

Laravel Model binding including SoftDeleted values

I have a Route::resource('sheeps', 'SheepsController') with a show function returning a resource:

public function show(Sheep $sheep)
{
    return new SheepResource(
        $sheep->load('farm')
    );
}

So when I call /api/sheeps/123 should I get Sheep 123, but... I don't get it, because it's softdeleted. How to fix the resource that it also searches in softdeleted results?

Upvotes: 1

Views: 135

Answers (2)

Mathieu Ferre
Mathieu Ferre

Reputation: 4422

You should use this :

in your RouteServiceProvider :

/**     
* Define your route model bindings, pattern filters, etc.
*
 * @return void
 */
public function boot()
{


    parent::boot();

    Route::bind('sheep', function ($value) {
        return Sheep::withTrashed()->find($value);
    });



}

Upvotes: 1

Evgeniy
Evgeniy

Reputation: 220

Try this:

public function show($id)
{
    $sheep = Sheep::withTrashed()->findOrFail($id);

and update your route or

public function show(int $sheep)
{
    $sheep = Sheep::withTrashed()->findOrFail($sheep);

or use Explicit Binding

Upvotes: 0

Related Questions