2
我有兩個表類別和子類別,我想用它們的連接子類別顯示類別。爲此,我創建了一個查詢。當我嘗試使用一對多關係從類別表中獲取類別名稱時顯示錯誤。SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'categories.sub_category_id' - Laravel 5
該表的結構爲
類別; ID,名稱
子類:身份證,CATEGORY_ID,子名
// Category.php model
class Category extends Model
{
protected $primaryKey = 'id';
protected $table = "categories";
public function subcategories()
{
return $this->hasMany('App\SubCategory');
}
}
// Subcategory.php model
class SubCategory extends Model
{
protected $primaryKey = 'id';
protected $table = "subcategories";
public function category()
{
return $this->hasMany('App\Category');
}
}
// Category Controller
public function show()
{
$categories= Category::all();
$subcategories=Subcategory::all();
return view('show',compact('categories','subcategories'));
}
// show.blade.php
@foreach($subcategories as $subcategory)
{{ $subcategory->subname }}
{{ $subcategory->category_id }}
{{ $subcategory->category->name }}
@endforeach
此行給我的錯誤{{ $subcategory->category->name }}
。
請問你的抽象作品?它是如何從「子類別」到「子類別」的。 – DanFromGermany
我不明白你的觀點。 –
請發佈模型類 –