0
votes

While I use Laravel 5.5

public function index()
    {
        $posts = Post::orderBy('created_at','desc')->get();
        return view('post/index',compact('posts'));
        // ['posts' => $posts]);
    }

I met a issue:Method 'get' not found in void.Referenced class is not found in subject class

@foreach($posts as $post)
                <div class="blog-post">
                    <h2 class="blog-post-title"><a href="/posts/{{$post->id}}" >{{$post->title}}</a></h2>
                    <p class="blog-post-meta">{{$post->created_at}} by<a href="/user/5">Kassandra Ankunding2</a></p>

                    <p>{{$post->content}}
                    <p class="blog-post-meta">赞 0  | 评论 0</p>
                </div>
                @endforeach

this is the Post class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

// 表 => posts
class Post extends Model
{
    //
    /**
     * @param $string
     * @param $string1
     */
    public static function orderBy($string, $string1)

    {

    }

}

After I remove all the function in Post class,it says:"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pet_shop.posts' doesn't exist (SQL: select * from posts order by created_at desc)" Anybody can help?

2
Why are you declaring the orderBy() method in your model?Camilo

2 Answers

1
votes

Your code is ok and should work... i tested similar code in my system

but if still not working... try to use DB query

$posts = DB::table('posts')->orderBy('created_at','desc')->get();

and add the

use Illuminate\Support\Facades\DB;
0
votes

I feel a little sorry……

I finally solved this issue by restart the server.