Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
like image 633
AndrewMcLagan Avatar asked Oct 18 '13 09:10

AndrewMcLagan


1 Answers

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.

like image 77
harryg Avatar answered Oct 09 '22 15:10

harryg