Home > other >  Add a block field from a specific article
Add a block field from a specific article

Time:12-08

I have an article, each article has article_blocks, the blocks have a video_link field

And I need to display this field in the list of articles

This is how the article list output is roughly implemented

<div class="blog-list">
@foreach($articles as $article)
  <div class="blog-article">

    <div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
      <span>Watch video</span>
    </div>

    <h2 hljs-string">">{{ $article->title }}</h2>
    <span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
    <span>{{ $article->getTotalViews() }} Views</span>
  </div>
@endforeach
</div>

Each article has a button to open a video, but this video field itself is not in the article itself, but in the article blocks

I now take this field from the block and output it, it turns out like this

<?php

use App\Models\ArticleBlock;

$article_block = ArticleBlock::whereNotNull('video_link')->first();
    
?>

<div class="blog-list">
@foreach($articles as $article)
  <div class="blog-article">

    @if ($article_block->video_link !== 'null')
    <div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
      <span>Watch video</span>
    </div>
    @endif

    <h2 hljs-string">">{{ $article->title }}</h2>
    <span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
    <span>{{ $article->getTotalViews() }} Views</span>
  </div>
@endforeach
</div>

As a result, I get this field with video and it is displayed, but the same video is displayed for all articles in the list, and each article should have its own field and its own video. How can this be fixed?

I probably need to look for something like blocks by article id

$article_block = ArticleBlock::where('article_id', $article->id)->whereNotNull('video_link')->first();

But I can't get id

CodePudding user response:

Cant write comment, under 25 rep :(

data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}"

This looks wrong. You should eager load article blocks (eager loading is generally faster than lazy loading) for all articles with a with statement in your eloquent query see: https://laravel.com/docs/8.x/eloquent-relationships#eager-loading

Then if everything is correct you should be able to do just {{ $article->block->video_link }} or something similar.

Above is right if article has only one article block (one to one relationship).

But it seems that article has multiple blocks (one to many relationship) so you would need to iterate over each of the article block as well with another @foreach statement.

@foreach($articles as $article)

     @foreach($article->article_blocks as $block)

         <p>URL of video is: https://www.youtube.com/embed/{{ $block->video_link }}</p>

     @endforeach


@endforeach

Now lets answer your question why video is same everywhere? Because you've coded it as so. You should NEVER write custom queries in your blade files.

Here i am referring to this part of your code:

<?php

use App\Models\ArticleBlock;

$article_block = ArticleBlock::whereNotNull('video_link')->first();
    
?>

Queries should be either in controllers/services or repositories.

$article_block = ArticleBlock::whereNotNull('video_link')->first(); will just give you first article block defined in ur database where video_link is not null. It won't guarantee that ArticleBlock belongs to given Article.

Lets hope that i put you on the right path ^.^

  • Related