【问题标题】:Custom attribute containing whether or not there's a relation between models包含模型之间是否存在关系的自定义属性
【发布时间】:2019-12-14 03:18:13
【问题描述】:

我有两个模型:用户和事件。

两者之间具有多态的多对多关系。

有什么办法当我Event::all()

$appends 一个getJoinedAttribute() 如果Auth::user() 与列表中的每个事件都有关系则返回true?

    public function getJoinedAttribute() {
        return $this->whereHas('users', function($query) {
           $query->where('users.id', Auth::user()->id);
        })->count() > 0 ? true : false;

我想我需要类似的东西。

【问题讨论】:

  • 这个功能是你在Event Modal中添加的?

标签: laravel laravel-5 eloquent


【解决方案1】:

在这里我尝试使用我的系统:

In my Order Modal:

public function scopeOrderStatus($query, $status, $newOrder = null)
{
    if ($newOrder !== null) {
        return $query->where(function($filter) use ($status, $newOrder){
            $filter->where('status', $status)->whereDate('created_at', $newOrder);
        })->count();
    }
    return $query->where('status', $status)->count();
}

in Your Controller to access :

$pendingConfirmOrders = Order::orderStatus(1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多