【问题标题】:How to fix: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'create_at如何修复:SQLSTATE [42S22]:找不到列:1054 Unknown column 'created_at
【发布时间】:2019-06-30 07:07:53
【问题描述】:

SQLSTATE[42S22]: 未找到列: 1054 未知列 'create_at' in 'order Clause' (SQL: select * from posts where posts.user_id = 2 and posts.user_id is not null order by create_at desc) (查看: D:\xampp\htdocs\xampp\praactise\freecode\resources\views\profiles\index.blade.php)

我试过了:php artisan migrate:fresh

index.blade.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

User.php 文件----

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

post.php 文件

class Post extends Model
{
     protected $guarded=[];
   public function user()
    {
        return $this->belongsTo(User::class);
    }
}

【问题讨论】:

    标签: sql laravel


    【解决方案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');
    

    【讨论】:

    • 更改后,我得到了那个错误。试图获取非对象的属性“标题”(查看:D:\xampp\htdocs\xampp\practise\freecode\resources\views\profiles\index.blade.php)
    • 我不明白。
    • index.blade.php 文件
      {{ $user->posts->count() }} 个帖子
      {{ $user->profile->title }}
      {{ $user->profile->description }}
      @foreach($user->posts as $post)
      image }}" class="w-100"> @endforeach
    • @AnikaTabassum 在你的刀片顶部执行 @dump($user->profile) 并给我输出
    • 我正在使用这个:{{ $user->profile? $user->profile->title:"" }} 替换这个 {{$user->profile->title}} 并且它工作正常。
    【解决方案2】:

    这是一个错误

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

    【讨论】:

    • 更改后,我得到了那个错误。试图获取非对象的属性“标题”(查看:D:\xampp\htdocs\xampp\practise\freecode\resources\views\profiles\index.blade.php)
    【解决方案3】:

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

    【讨论】:

    • 更改后,我得到了那个错误。试图获取非对象的属性“标题”(查看:D:\xampp\htdocs\xampp\practise\freecode\resources\views\profiles\index.blade.php)
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签