繁体   English   中英

在where子句中指定表时的Eloquent错误

[英]Eloquent Error when specifying table in where clause

我有这个问题:

$query = Product::query();

$query->join('models AS m', 'products.model_id', '=', 'm.id');

if ($request->has('q')) {
    $terms = explode(' ', $request->get('q'));
    $query->orWhereHas('products.name', function($q) use ($terms) { $q->whereIn('name', $terms); });
}

它正在回归

Call to undefined method Illuminate\Database\Query\Builder::products()

如果我将products.category更改为其他任何内容,则错误会更改为新值,例如:

$query->orWhereHas('s.name', function($q) use ($terms) { $q->whereIn('name', $terms); });

会输出

Call to undefined method Illuminate\Database\Query\Builder::s()

我需要指定该类别属于名称,因为如果我不这样做,则返回不明确的字段存在。

我加入模型表的原因是因为我需要从模型中选择一个字段

$query->get(['products.id', 'products.name', 'products.front_image', 'products.back_image', 'products.slug', 'm.slug as model_slug'])

如何在where子句中指定table而不会出现此错误?

whereHas方法用于查询关系; 第一个参数应与模型中关系方法的名称相对应。 在您的情况下,QueryBuilder尝试在Product模型上调用方法products()

如果您希望按名称查询记录,则可以使用where()方法。

暂无
暂无

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

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