1

Is the method "latest" used in Controllers removed in newest version of Laravel?

In PHP Storm I get follow error:
Method latest() not found in App/Thread.

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

I'm following a LaraCasts tutorial, and browsing to said page gives me following error. -> forum.test/threads.

ErrorException (E_ERROR) Method Illuminate\Database\Query\Builder::path does not exist. (View: D:\xampp\htdocs\forum\resources\views\threads\index.blade.php)

As per requested, my view: it is in 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

Also, my routes.

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

Route::resource('threads', 'ThreadController');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Salman Zafar
  • 3,844
  • 5
  • 20
  • 43

1 Answers1

2

The error is not related to the code you posted. Method Illuminate\Database\Query\Builder::path does not exist.. You are calling somewhere path method which does not exist.

To answer your question, method latest() is still present in the (currently) newest version of Laravel 5.6: https://laravel.com/api/5.6/Illuminate/Database/Query/Builder.html#method_latest

My guess would be you have an incorrect config of the Thread model relationships. Most probably you did not define path() relationship.

See this answer to similar question: https://stackoverflow.com/a/37934093/1885946

Patrik Fuhrmann
  • 969
  • 1
  • 12
  • 21
  • There is nothing written in app/Thread.php besides default code. – David Newman Jul 18 '18 at 08:59
  • Actually the error was I used a later version of index.blade.php , where path is a added in Thread.php. I tried to write down all steps of the tutorial, and accidentally wrote wrong version. Solution: I changed path() to path and now all works again. :) – David Newman Jul 18 '18 at 09:04