我試圖顯示類別的名稱,但出現此錯誤:BadMethodCallExceptionCall to undefined method Illuminate\Database\Query\Builder::with()AdminController.phppublic function gamelist(){ $categories = Category::all(); $home = DB::table('products')->with(['categories']); return view('admin.gamelist', ['categories' => $categories, 'home' => $home, 'mode' => 'admin']);}產品.phpclass Product extends Model{ public function categories() { return $this->belongsTo('App\Category', 'category_id'); }}游戲列表.blade.php@foreach ($homes as $home) <tr> <td>{{ $home->id }}</td> <td>{{ $home->name }}</td> <td>{{ $home->categories->name}}</td> <td>{{ $home->price }} €</td>有人能幫我嗎?謝謝
1 回答

慕后森
TA貢獻1802條經驗 獲得超5個贊
with
用于急切加載 Eloquent 關系。通過調用DB::table
,您決定改用查詢構建器,而這個不能使用 Eloquent 關系。
你應該更換
$home = DB::table('products')->with(['categories']);
經過
$home = Product::with('categories')->get();
- 1 回答
- 0 關注
- 123 瀏覽
添加回答
舉報
0/150
提交
取消