【问题标题】:Undefined index: Laravel & TMDB api未定义索引:Laravel & TMDB api
【发布时间】:2021-01-13 10:04:01
【问题描述】:

如果我输入了错误的 url 地址,例如 http://127.0.0.1:8000/okec

未定义索引:poster_path

而不是 404 未找到

<div class="col-sm-2 text-center">
            <div class="figure">
                @if ($infoFilm['poster_path'])
                    <img class="img-fluid img-thumbnail rounded" src="{{ 'https://image.tmdb.org/t/p/w185/'.$infoFilm['poster_path'] }}" alt="Poster">
                @else
                    <img class="img-fluid img-thumbnail rounded" src="https://via.placeholder.com/200x240?text=mUaacH" alt="NoImage">
                @endif 
            </div>
        </div>

控制器

public function details($id) {

    $infoFilm = Http::withToken(config('services.tmdb.token')) 
    -> get(config('services.tmdb.apiurl').'movie/'.$id) 
    -> json();

    return view('pages.users.details', [
        'infoFilm' => $infoFilm,
    ]);

路线

Route::get('{details}', [FilmController::class, 'details'])->name('details');

代码有问题吗?

【问题讨论】:

  • 对于您正在查找的电影,$infoFilm['poster_path'] 显然没有设置。检查 JSON 以仔细检查它的名称,你认为它是什么;有些电影可能没有海报。

标签: arrays json laravel api


【解决方案1】:

已解决:

@empty( $infoFilm['id'] )
    @php
        abort(404);
    @endphp
@endempty

【讨论】:

    猜你喜欢
    • 2018-08-02
    • 2012-08-24
    • 2015-03-23
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2021-10-06
    • 1970-01-01
    相关资源
    最近更新 更多