0

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

1 Answers1

0

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.

aynber
  • 22,380
  • 8
  • 50
  • 63
  • Thanks yes i changed it to quote but when i added quote it don't show up news is working quote note ( – Bochorishvili Nikusha Oct 11 '21 at 18:11
  • You probably have the wrong type, then. I'll update the code so you can check to see what other types there are – aynber Oct 11 '21 at 18:12
  • Its don't showing i have 5 types and 4 of them is coming in $newsFirstLine public static function allNewsPageQuery() { return self::where('type', '!=', 'blog')->orderBy('published_at', 'Desc'); } – Bochorishvili Nikusha Oct 11 '21 at 18:23
  • I'm not quite sure what you're saying. Is it showing the types that you're not capturing? If so, what are the names it's showing? – aynber Oct 11 '21 at 18:56
  • I'm getting all types except quote and i don't understand why this is my class logic Article::where('type', '!=', 'blog')->where('category_id', '!=', '32')->where('status','1')->orderBy('published_at','Desc')->get(); – Bochorishvili Nikusha Oct 11 '21 at 19:13
  • Well, you're getting all but `blog` in your query, but that doesn't tell you what you **are** getting. If you view the page with the code I showed you, you should see `Checking for ...` on the page, which will tell you the types that you are not catching with your if/else. If you're expecting a quote and not seeing it, check to see if it's listed as `Checking for quote`. You can also move that line between the foreach and first `if` to see the type for each article. Perhaps the if check is working, but the include for `cards/quote` is not doing what you expect. – aynber Oct 11 '21 at 19:16
  • It's not checking for quote ( tinker shows all news except type quote i even added in class logic where type quote get but it's still not showing quote – Bochorishvili Nikusha Oct 11 '21 at 19:26
  • You're going to have to do some troubleshooting from here, then. As I mentioned earlier, you can move the `Checking for` above the if to see if the quote articles are there. If so, add another echo line inside of the `type == 'quote'` block to make sure it's hitting that block. If all that's good, then you need to start troubleshooting your include file. – aynber Oct 11 '21 at 19:34