You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel关联查询报错:多条件筛选movies与movie_dates表失败

问题解决:Laravel关联查询触发Column not found错误

错误原因

你在where方法中的参数格式不符合Laravel的规范。直接传入两个独立数组的写法会让框架生成错误的SQL语句,最终导致找不到对应列的报错。

正确实现方案

方案1:链式调用多个where方法

这种写法清晰直观,每个条件单独调用where:

$months = 2;
$years = 1;

return DB::table('movie_dates')
    ->join('movies', 'movie_dates.movie_id', '=', 'movies.id')
    ->where('movie_dates.year', '>=', $years)
    ->where('movie_dates.month', '>=', $months)
    ->get();

方案2:嵌套数组传递多条件

如果想用数组形式批量传条件,需要把每个条件封装成子数组,再放在外层数组中:

$months = 2;
$years = 1;

return DB::table('movie_dates')
    ->join('movies', 'movie_dates.movie_id', '=', 'movies.id')
    ->where([
        ['movie_dates.year', '>=', $years],
        ['movie_dates.month', '>=', $months]
    ])
    ->get();

优化:仅返回movies表数据

如果你的需求是只获取movies表的内容,可以加上select('movies.*')过滤冗余字段:

$months = 2;
$years = 1;

return DB::table('movie_dates')
    ->join('movies', 'movie_dates.movie_id', '=', 'movies.id')
    ->where('movie_dates.year', '>=', $years)
    ->where('movie_dates.month', '>=', $months)
    ->select('movies.*')
    ->get();

内容的提问来源于stack exchange,提问作者Mishen Thakshana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 11:35:18