【问题标题】:Laravel Eloquent use where on with data specific fieldLaravel Eloquent 使用 where on 数据特定字段
【发布时间】:2021-06-14 16:46:05
【问题描述】:

这是我的代码:

$course = Course::where('id', $activeCourseId)
        ->with(["blocks.cards" => function($q) use($cardsDueIds) {
            $q->whereNotIn('blocks.cards.id', $cardsDueIds);
            $q->take(50);
        },
        "cards",
        "blocks.cards.contexts",
        "blocks.cards.user_contexts",
        "blocks.cards.block.course",
        "blocks.cards.thisUser"])
        ->first();

我尝试只获取block.cards,其中每个block.cards[’id'] 不在$cardsDueIds 中。

其实我有这个错误“

“where 子句”中的未知列“blocks.cards.id”

"。

有人知道如何正确执行此操作吗?谢谢!

【问题讨论】:

  • $q->whereNotIn('blocks.cards.id', $cardsDueIds); 到 $q->whereNotIn('id', $cardsDueIds); 因为你已经在那个关系中回拨
  • 它有效,谢谢!

标签: laravel eloquent query-builder relation


【解决方案1】:

试试这个,

$course = Course::where('id', $activeCourseId)
          ->with(["blocks.cards" => function($q) use($cardsDueIds) {
               $q->whereNotIn('id', $cardsDueIds);
        }])
        ->first();
    

【讨论】:

  • 感谢您的回答,不幸的是我遇到了“未定义的上下文”错误。
  • 卡表中是否有上下文字段?
  • 不是关系
  • 您已经处于该关系中,因此您可以直接访问卡表字段。我更新了代码,您可以查看。
  • 抱歉 whereNotIn 不起作用,它只有在我删除“->take(50)”时才起作用
【解决方案2】:
$q->whereNotIn('blocks.cards.id', $cardsDueIds);

到

 $q->whereNotIn('id', $cardsDueIds);

因为你已经在那个关系中回拨

【讨论】:

    猜你喜欢
    • 2021-04-05
    • 2021-07-25
    • 2016-01-27
    • 2014-08-14
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多