Route Model Binding and Soft Deletes - Laravel 4

喜夏-厌秋 提交于 2019-12-23 07:34:52

问题


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');

回答1:


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.




回答2:


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.




回答3:


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);
}


来源:https://stackoverflow.com/questions/19445615/route-model-binding-and-soft-deletes-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!