簡體   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