Search code examples
laravellaravel-5laravel-5.1laravel-5.3

Errors using alaouy/Youtube package with Laravel 5.4 for fetching videos from Youtube


I am using alaouy/Youtube package on Laravel 5.4. It's fetching videos from Youtube but I am getting errors.

// view
@extends('welcome') 
    @section('content')
       <ul>
           @foreach($videos as $data)
                <div class="well">
                    {{ $data->id->videoId}}
               </div>
           @endforeach
       </ul>
   @endsection

// controller
$videos = Youtube::search($search);
return view('search',compact('videos'));

I am able to get access to all the data in the object except {{$data->id->videoId}}

// Error
Undefined property: stdClass::$videoId (View: 
C:\Users\derrick\testyoutubeapi\resources\views\search.blade.php)

Solution

  • You are not receiving the required data so you are getting the error message of not defined property.

    in other words, you are requesting videoId field which is not there for some of kinds like playlist (playlistId), or maybe a channel (channelId)

    so your problem solution should be just making your code in the view to check if there is a field that is called videoId or no before the query:

               @foreach($videos as $data)
                 @if(isset($data->id->videoId)) // field available?
                <div class="well">
                    {{ $data->id->videoId}}
                 @endif // end if
               </div>
           @endforeach