0

I'm trying to embed video links fetched from my database on the blade page in Laravel. However, it doesn't work. How should I fix this?

Here's what I have got so far.

Controller code:

//This returns array of objects like so: [{video: video_link}, {video: video_link}]
     
       public function getVideos(){
               
            $videos = Video::select('video_link')->where('d', $this->matchD())->get();
            
            return $videos;
        }   

  

Blade page:

<div class="row">

            <div class="col-12 col-md-4 my-2">
                @php $videos = app()->call('App\Http\Controllers\ResponseController@getVideos'); @endphp

                @foreach($videos as $video) 
                   
                
                    <div class="embed-responsive embed-responsive-16by9">
                        <iframe class="embed-responsive-item" src = "{{ url($video['video_link']) }}"></iframe>
                    </div>

                    {{ url($video['video_link']) }}
                    
                @endforeach

            </div>
        </div>    
Shreya Agarwal
  • 676
  • 2
  • 8
  • 20
  • Does it work with `src="{{ $video['video_link'] }}"`? Also, what does each `video_link` look like? What does `{{ dd($video['video_link']) }}` output? – Tim Lewis Oct 08 '20 at 17:21
  • Hey @TimLewis: No, it doesn't. Video links are regular YouTube links. {{ dd($video['video_link']) }}: outputs the link in quotes. "https://www.youtube.com/watch?v=_lkw2xtNMmk" – Shreya Agarwal Oct 08 '20 at 17:23
  • `get()` returns a collection; Have you tried using it as `$video->video_link` – Shahlin Ibrahim Oct 08 '20 at 17:25
  • If you use the `url()` helper on that, you end up with an invalid URL, like `http://localhost/https://youtube.com/watch?v=...`. That being said, it should work with just `src="{{ $video['video_link'] }}"`... Also @ShahlinIbrahim no, that's not the issue. They're looping over `$videos`, and `$video->video_link` is the same as `$video['video_link']` – Tim Lewis Oct 08 '20 at 17:26
  • @TimLewis, so that doesn't work, which surprises me given I'm passing a link to the src here. – Shreya Agarwal Oct 08 '20 at 17:30
  • Yeah, I'm surprised too. Are you getting a 404 error or something else? "It doesn't work" is a little vague. But definitely remove the `url()` helper; that's only useful for local URLs :) – Tim Lewis Oct 08 '20 at 17:31
  • No, 404. Just shows nothing on the page. – Shreya Agarwal Oct 08 '20 at 17:40

2 Answers2

2

You can't embed regular youtube link, because the URL for embed is different.

https://www.youtube.com/watch?v=_lkw2xtNMmk

Should be embedded like:

<iframe width="560" height="315" src="https://www.youtube.com/embed/_lkw2xtNMmk" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

So solution for that will be write helper function to get youtube video ID from end of URL and then paste ID to embed iframe code.

c4ni
  • 33
  • 5
  • Or, modify the `video_link` column to contain the Embed URL as specified above :) Either way, this is useful information! – Tim Lewis Oct 08 '20 at 18:06
0

Use parse_url() and parse_str()

$url="http://www.youtube.com/watch?v=C4kxS1ksqtw"

parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );

echo $my_array_of_vars['v'];    

 // Output: C4kxS1ksqtw

Source: https://stackoverflow.com/a/3393008/320487

Ahmed Saad
  • 53
  • 8