Laracasts 教程:方法 'Latest()' 被移除了吗?

Laracasts tutorial: Is method 'Latest()' removed?

控制器中使用的方法 "latest" 是否已在最新版本的 Laravel 中删除?

在 PHP Storm 中出现以下错误:
在 App/Thread 中找不到方法 latest()。

public function index()
    {
        //
        $threads = Thread::latest()->get();
        return view('threads.index', compact('threads'));
    }

我正在关注 LaraCasts 教程,浏览到所述页面时出现以下错误。 -> 论坛。test/threads。

错误异常(E_ERROR) 方法 Illuminate\Database\Query\Builder::path 不存在。 (查看:D:\xampp\htdocs\forum\resources\views\threads\index.blade.php)

应要求,我的看法:在resources/views/threads/index.blade.php

@extends('layouts.app')
@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Forum Threads</div>

                    <div class="panel-body">
                        @foreach ($threads as $thread)
                            <article>
                                <h4>
                                    <a href="{{ $thread->path() }}">
                                        {{ $thread->title }}
                                    </a>
                                </h4>
                                <div class="body">{{ $thread->body }}</div>
                            </article>
                            <hr/>
                        @endforeach
                    </div>

                </div>
            </div>
        </div>
    </div>
@endsection

另外,我的路线。

<?php
Route::get('/', function () {
    return view('welcome');
});

Route::resource('threads', 'ThreadController');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');

该错误与您发布的代码无关。 Method Illuminate\Database\Query\Builder::path does not exist.。您正在某处调用 path 不存在的方法。

为了回答您的问题,Laravel 5.6 的(当前)最新版本中仍然存在方法 latest()https://laravel.com/api/5.6/Illuminate/Database/Query/Builder.html#method_latest

我的猜测是您对 Thread 模型关系的配置不正确。很可能您没有定义 path() 关系。

查看类似问题的答案: