【问题标题】:Getting weird error in Laravel Youtube API在 Laravel Youtube API 中出现奇怪的错误
【发布时间】:2018-08-19 19:38:43
【问题描述】:

我在这个 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

我的结果

但问题是当我使用{{ $video->snippet->title }} 时出现错误

【问题讨论】:

  • 为什么要显示错误信息的图片?
  • @tehhowch 显示我不明白的实际错误
  • 这意味着 sn-p 为空或属性标题不存在。
  • 但是当我使用dd(); 时,它会显示结果
  • 你必须检查 $search 包含的内容,你可以用 dd 在这里发布图片

标签: youtube-api laravel-5.6


【解决方案1】:

您尝试将 title 作为对象访问,但它是一个数组。

我会尝试类似的东西: $video->sn-p['title']

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


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

【讨论】:

  • 我修好了,但不是这样,谢谢你回答我
  • 不客气,我们至少想知道解决方案背后的理论 :)
  • 我添加了我的答案
【解决方案2】:

当我 dd($search);check 我得到 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  

效果很好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2011-01-08
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多