【问题标题】:"where" in relation of eloquent model与雄辩模型相关的“哪里”
【发布时间】:2019-09-23 06:00:11
【问题描述】:

owner 它是 bulletins 表的一列,它是对话模型中的关系。

public function bulletin()
{
    return $this->belongsTo('App\Bulletins','bulletin_id');
}

    $conversations = Conversations::with('bulletin','messages')
        ->where('owner_id', $userId)
        ->orWhere('owner', $userId)
        ->get();

我有未知的列所有者。我知道这是什么意思,但是如何在 laravel 中做到这一点?之前我使用 left join 来处理这个请求,现在我想使用 eloquent 关系。

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    我不明白为什么你有两次?那么列名是owner 还是owner_id

    你应该可以这样做:

    $conversations = Conversations::with(['bulletin' => function($query) use ($userId) {
        $query->where('owner_id', $userId);
    }, 'messages'])->get();
    

    【讨论】:

    • 我需要'where'所有者和所有者ID中的列。原始查询是:->where('owner_id', $userId) ->orWhere('owner', $userId)
    • 那么你需要在列前面使用表作为别名,例如bulletin.owner_id,而owner列是哪个表的一部分?
    • $conversations = Conversations::with(['bulletin'=>function($q){ $q->where('own.owner',2); },'messages']) ->orWhere('owner_id', $userId) ->get();
    • 它现在可以工作,但是当使用 last ->where 或 ->orWhere - 我得到了空结果
    • 对不起我的错误 - 是错误的 ID )谢谢 - 它已解决
    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2014-04-14
    相关资源
    最近更新 更多