如何修复:SQLSTATE [42S22]:找不到列:1054未知列'create_at

时间:2019-06-30 07:07:53

标签: sql laravel

SQLSTATE [42S22]:找不到列:1054'order子句'中的未知列'create_at'(SQL:从posts中选择*,其中postsuser_id = 2和{ {1}}。posts不是user_id des的空顺序)(视图:D:\ xampp \ htdocs \ xampp \ practise \ freecode \ resources \ views \ profiles \ index.blade.php)

我尝试过:php artisan迁移:新鲜

index.blade.php

create_at

User.php文件----

<div class="col-9 pt-5">
           <div class=" d-flex justify-content-between align-items-baseline font-weight-bold"><h1>{{ $user->username }}</h1>
            <a href="/p/create">Add New Post</a>
           </div>
                <div class="d-flex">
                    <div ><strong>{{ $user->posts->count() }}</strong> posts</div>
                    <div class="pl-5"><strong>23k</strong> followers</div>
                    <div class="pl-5"><strong>435</strong> following</div>
                </div>
                <div class="pt-4 font-weight-bold" ><strong>{{ $user->profile->title }}</strong></div>
                <div>{{ $user->profile->description }}</div>
                <div><a href="#">{{ $user->profile->url ??'N/A' }}</a></div>
        </div>

    </div>
    <div class="row pt-5">
        @foreach($user->$posts as $post)
        <div class="col-4" >
            <img src="/storage/{{ $post->image }}" class="w-100"> 
        </div>
        @endforeach

post.php文件

 public function posts()
    {
        return $this->hasMany(Post::class)->orderBy('create_at','desc');
    }
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }

3 个答案:

答案 0 :(得分:1)

这是一个错误

    public function posts()
    {
        return $this->hasMany(Post::class)->orderBy('created_at','desc');
    }

答案 1 :(得分:1)

未知列create_at和已知列created_at

在文件User.php

更改

return $this->hasMany(Post::class)->orderBy('create_at','desc');

return $this->hasMany(Post::class)->orderBy('created_at','desc');

答案 2 :(得分:0)

创建日期的默认laravel时间戳属性为 created_at 而非create_at