AndrewMcLagan
AndrewMcLagan

Reputation: 13987

Route Model Binding and Soft Deletes - Laravel 4

When using soft deletes and route to model binding their arises a circumstance when you cannot view the injected model if it has been "soft deleted".

e.g.

I have a Job model. if i "trash" one of these models and then open the trash and try to view the Job model i get a 404 resource not found. I resolved this by using the Route::bind() function as such

Route::bind('job', function($id, $route) {

    return Job::withTrashed()->find($id);
});

although this seems unnecessary and a little silly... is there a way around this so i can use the very eloquent one line binding:

Route::model('job', 'Job');

Upvotes: 10

Views: 5053

Answers (3)

harryg
harryg

Reputation: 24107

What you've outlined in your question seems to solve it.

A slight refinement is that you may want to specify which specific key to include the trashed models.

E.g. have normal route model bind like so:

Route::model('job', 'Job');

And define another key like 'anyjob' where you allow trashed jobs to be queried also:

Route::bind('anyjob', function($id) {    
    return Job::withTrashed()->find($id);
});

Then for routes where you don't want to include trashed jobs you just reference job:

get('/jobs/{job}/edit', ['controller' => 'JobsController@edit']); // We don't want to be able to edit a trashed job.

And only reference the anyjob binding for routes where a trashed job is acceptable:

delete('/jobs/{anyjob}', ['controller' => 'JobsController@destroy']); // we could then forceDelete the trashed job for example as it'll be correctly injected in out our controller method

This prevents you ending up with soft-deleted models in controller methods that should otherwise not deal with them. You specify the exact routes which could accept any job, or even just trashed jobs.

Upvotes: 5

John Mellor
John Mellor

Reputation: 2513

In addition to this if you've built your own traits and scopes, you can define the find function in there. For instance I have an "approvedTrait" which works the same way but with an "approved" column to show whether something has been approved by a moderator. I then simply put this in my approvedTrait class:

public static function find($id, $columns = array('*'))
{
    return self::withUnapproved()->find($id, $columns);
}

Upvotes: 1

maxwilms
maxwilms

Reputation: 2024

As Route::model() is using the find method on the model you can simply override the method to retrieve trashed objects:

class Job extends Eloquent
{
    public static function find($id, $columns = array('*'))
    {
        return parent::withTrashed()->find($id, $columns);
    }
}

Now you can use model bindings without closures

Route::model('job', 'Job');

Be careful while using the find method where you don't want to retrieve trashed objects.

Upvotes: 4

Related Questions