代码之家  ›  专栏  ›  技术社区  ›  Sid Heart

在Laravel Youtube API中出现奇怪错误

  •  -1
  • Sid Heart  · 技术社区  · 7 年前

    我在youtube api上使用laravel 5.6

    https://github.com/alaouy/Youtube

    我正试图用这样的分页搜索结果

    $params = [
            'q'             => $request,
            'type'          => 'video',
            'part'          => 'id, snippet',
            'maxResults'    => 20
        ];
    
        // $search = Youtube::searchAdvanced($params, true);
    
        $search = Youtube::paginateResults($params, null);
    
        $info = $search['info'];
    
        $nextpagetoken = $info['nextPageToken'];
    
    
    
        return view('search.index', compact('search'));
    

    我的看法是

    @foreach($search as $result)
      @foreach($result as $video)
      {{ dd($video->snippet->title) }}
       @endforeach
    @endforeach
    

    我的结果 enter image description here

    但问题是当我使用 {{ $video->snippet->title }} 出错 enter image description here

    2 回复  |  直到 7 年前
        1
  •  0
  •   ibitebyt3s    7 年前

    您试图将标题作为对象访问,但它是一个数组。

    我会尝试一些类似的东西: $video->片段['title']

    (很抱歉没有使用降价语法,我正在用智能手机回答这个问题)


    我猜dd()足够聪明,可以知道两者之间的区别,并正确地显示两者。

        2
  •  0
  •   Sid Heart    7 年前

    当我 dd($search); 检查我得到2个数组

    array:2 [▼
     "results" => array:20 [▶]
     "info" => array:6 [▶]
    ]
    

    所以我用

    $results = $search['results'];// for videos only
    
    $info = $search['info']; // For Other info's
    

    现在我可以在视图中使用foreach

    @foreach($results as $video)
      <div class="row boxhead">
    
        <div class="col-md-4">
          <a href="{{ url('watch/' . $video->id->videoId)}}">
          <img src="{{$video->snippet->thumbnails->medium->url}}" class="img-fluid">
          </a>
        </div>
        <div class="col-md-8">
          <a href="#">
          <h1 style="font-size: 17px;">{{ $video->snippet->title }}</h1>
          <p>{{ $video->snippet->channelTitle }}</p>
          <p>{{ $video->snippet->description }}</p>
          </a>
        </div>
    
      </div>
      <hr>
    @endforeach  
    

    它很好用