繁体   English   中英

如何在Laravel 4中查询和过滤软删除的口才模型

[英]How to query and filter soft-deleted Eloquent models in Laravel 4

使用来自https://github.com/snipe/laravel4-starter的 Laravel 4.1入门套件,我有两个模型:类别和兴趣。 两者也都可以软删除。 我希望索引视图根据用户的意愿显示3个不同的列表:“全部显示”,“显示已删除”和“显示未删除”。 一切都很好,直到我在它们之间添加了HasMany / BelongsTo关系。

楷模:

class Category extends Elegant
{
    protected $softDelete = true;

    public function interests()
    {
        return $this->hasMany('Interest');
    }
}

class Interest extends Elegant
{
    protected $softDelete = true;

    public function category()
    {
        return $this->belongsTo('Category');
    }
}

现在,当视图在属于软删除的类别的兴趣上调用$interest->category->name时,都会因ErrorException: Trying to get property of non-objectErrorException: Trying to get property of non-object 我怀疑这是因为$interest->category是NULL,因为它已经被软删除而没有被加载。

控制器:

class InterestsController extends AdminController
{

    public function getIndex()
    {
        $showDisabled = Utility::GetShowDisabled();

        switch ($showDisabled) {
            case 'only':
                // How to load *only* soft-deleted Interest with their categories
                // even if the category has been soft-deleted?
                $interests = Interest::onlyTrashed()->orderBy('name')->paginate(10);
                break;
            case 'with':
                // How to load *all* Interests with their categories
                // even if the category has been soft-deleted?
                $interests = Interest::withTrashed()->orderBy('name')->paginate(10);
                break;

            default:
                // How to load *only* *NON-soft-delted* Interests with their categories
                // even if the category has been soft-deleted?
                $interests = Interest::orderBy('name')->paginate(10);
                break;
        }

        return View::make('backend/interests/index', compact('interests', 'showDisabled'));
    }

请在switch语句的每个3节中查看我的问题。 我怀疑该解决方案将涉及某种急切的加载,但是即使经过一些非常激烈的谷歌搜索和实验之后,正确的咒语仍然让我难以忘怀。

额外要点:在过滤掉具有软删除类别的兴趣时,如何加载非软删除的所有兴趣?

谢谢。

渴望加载类别:

Interest::with(['category' => function ($q) {
   $q->withTrashed();
}])->onlyTrashed()->get();
// the same for other cases

具有类别(未删除)的兴趣:

Interest::with('category')->has('category')->get();

感谢Jarek Tkaczyk提示。 有关其他人的完整参考,请参阅我最终使用的代码:

    $query = Interest::with(array('category' => function ($subQuery) {
        $subQuery->withTrashed();
    }));

    switch ($showDisabled) {
        case 'only':
            $query->onlyTrashed();
            break;
        case 'with':
            $query->withTrashed();
            break;
        case 'without':
            break;

        default:
            $showDisabled = "activeonly";
            $query->has('category');
            break;
    }

    $searchTerms = explode(' ', $searchQ);
    $query->where( function ($subQuery) use ($searchTerms) {
        $termCnt = 0;
        foreach($searchTerms as $term)
        {
            (0 == $termCnt)
                ? $subQuery->Where('name', 'LIKE', '%'. $term .'%')
                : $subQuery->orWhere('name', 'LIKE', '%'. $term .'%');
            $termCnt++;
        }
    });

    $interests = $query->orderBy($sortByCol, $order)->paginate(10);

代替 :

SoftDeleted = true;

采用:

use SoftDeletes;

这项工作对我来说很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM