php - Laravel 2 different includes
I have article table and it's have type column. I'm trying to get 2 different includes. If type is news get one include if type is quote get another include. But its don't working i'm getting 10 quotes includes then 10 news includes but there must be only 10 news in one page and if i will add quote on web it must have quote include...
Blade:
@if($newsFirstLine->type = 'quotse')
@foreach($newsFirstLine as $quote)
@include("cards/quote", [
'slug' => $quote->getUrl(),
'title' => $quote->tr('title'),
'avatar' => $quote->image,
'created' => $quote->created_at->format('d.m.Y'),
'createdHi' => $quote->created_at->format('H:i'),
])
@endforeach
@endif
@if($newsFirstLine->type = 'news')
@foreach($newsFirstLine as $news_item)
@include("cards/news", [
'image' => $news_item->image,
'slug' => $news_item->getUrl(),
'title' => $news_item->tr('title'),
'created' => $news_item->created_at->format('d.m.Y'),
'createdHi' => $news_item->created_at->format('H:i'),
])
@endforeach
@endif
Answer
Solution:
With the equal sign confusion out of the way, $newsFirstLine
is a collection of items, so it doesn't have a type
attribute. You need one overall loop, and check the type inside of it for each individual item.
@foreach($newsFirstLine as $news_item)
@if($news_item->type == 'quotse')
@include("cards/quote", [
'slug' => $quote->getUrl(),
'title' => $quote->tr('title'),
'avatar' => $quote->image,
'created' => $quote->created_at->format('d.m.Y'),
'createdHi' => $quote->created_at->format('H:i'),
])
@elseif($news_item->type == 'news')
@include("cards/news", [
'image' => $news_item->image,
'slug' => $news_item->getUrl(),
'title' => $news_item->tr('title'),
'created' => $news_item->created_at->format('d.m.Y'),
'createdHi' => $news_item->created_at->format('H:i'),
])
@else
Checking for {{ $item->type }}
@endif
@endforeach
By the way, quotse
is misspelled. Make sure it matches whatever you're actually using.