Could you please help me to get a Category name of the specific Ad (Post) from the CategoryItems table.
Ad Model:
public function cat()
{
return $this->belongsTo(Category::class);
}
public function categoryItems()
{
return $this->hasMany(CategoryItems::class);
}
Category Model:
public function ads()
{
return $this->hasMany(Ad::class);
}
public function categoryItems()
{
return $this->hasMany(CategoryItems::class);
}
CategoryItems Model:
public function cat()
{
return $this->belongsTo(Category::class);
}
public function ads()
{
return $this->hasMany(Ad::class);
}
AdsController Controller:
public function show($slug)
{
$ad = Ad::where('slug', $slug)->firstOrFail();
$cat = Category::get();
return view('ads.show', compact('ad', 'cat'));
}
View:
<h2></h2>
<div class="price">
Price:
</div>
<div class="city">
<h3></h3>
</div>
<div class="cat">
<h3></h3>
</div>
<p></p>
<p>Posted at: </p>
What I get now in the "cat" block is:
[{"id":2,"category_id":5,"ad_id":4,"created_at":"2017-07-18 10:37:33","updated_at":"2017-07-18 10:37:33"}]
I don't get it any further...
Update:
Ads DB:
id | title | slug | body | price | city | created_at | updated_at
Categories DB:
id | title | slug | created_at | updated_at
CategoryItems DB:
id | category_id | ad_id | created_at | updated_at
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire