简体   繁体   English

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

[英]Eloquent Error when specifying table in where clause

I have this query: 我有这个问题:

$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); });
}

It's returning 它正在回归

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

If I change products.category to anything else the error change to the new value for example: 如果我将products.category更改为其他任何内容,则错误会更改为新值,例如:

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

Will output 会输出

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

I need to specify that category belongs to names because if I don't it returns ambiguous fields existence. 我需要指定该类别属于名称,因为如果我不这样做,则返回不明确的字段存在。

The reason I'm joining the models table is because I need to select a single field from model 我加入模型表的原因是因为我需要从模型中选择一个字段

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

How can I specify table in where clause without getting this error? 如何在where子句中指定table而不会出现此错误?

The whereHas method is for querying relations; whereHas方法用于查询关系; the first argument should correspond with the name of a relationship method in your Model. 第一个参数应与模型中关系方法的名称相对应。 In your case, the QueryBuilder is trying to call the method products() on the Product model. 在您的情况下,QueryBuilder尝试在Product模型上调用方法products()

If you're looking to query records by name, you can just use the where() method. 如果您希望按名称查询记录,则可以使用where()方法。

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

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