I have two tables categories and subcategories.I want to show category in subcategory table.For this i have done follwing code.It shows error when i try to fetch category name from category table using one to many relationships. Databases are categories:- id,name and subcategories:- id,category_id,subname 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();
//dd($categories);
$subcategories=Subcategory::all();
//dd($subcategories);
return view('show',compact('categories','subcategories'));
}
show.blade.php
@foreach($subcategories as $subcategory)
@endforeach
This line is giving me error .Please sort this issues.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire