路线模型绑定和软删除-Laravel 4 [英] Route Model Binding and Soft Deletes - Laravel 4

查看:54
本文介绍了路线模型绑定和软删除-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".

例如

我有一个Job模型.如果我垃圾桶"这些模型之一,然后打开垃圾桶并尝试查看作业模型,我将找不到404资源.我通过使用Route :: bind()函数解决了此问题

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

推荐答案

由于 Route :: model()在模型上使用 find 方法,您可以简单地覆盖它检索垃圾对象的方法:

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

在不希望检索垃圾对象的情况下使用find方法时要小心.

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

这篇关于路线模型绑定和软删除-Laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆