首页 > 解决方案 > SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列'created_by'

问题描述

我试图将用户 ID 存储为 created_by ana 这是我的功能

public function store(Request $request)
{
    $destinationPath = public_path('upload');
    $image = $request->file('image');
    $input['imagename'] = time() . '.' . $image->getClientOriginalExtension();
    $image->move($destinationPath, $input['imagename']);
    $dbPath = $destinationPath . "/" . $input['imagename'];

    $hotel = new Hotel();
    $hotel->name = $request->input('name');
    $hotel->ville_id = $request->input('ville_id');
    $hotel->price = $request->input('base_price');
    $hotel->created_by=$request->input('created_by');
    $hotel->content = $request->input('content');
    $hotel->room = $request->input('room');
    $hotel->stars = $request->input('stars');
    $hotel->image = $input['imagename'];
    $hotel->save();
    toast('hotel ajouté !','success');

    return redirect('/ahotels');
}

和酒店模型我做relashionships

class hotel extends Model
{
    protected $fillable = [
        'title', 'content','image','created_by',
    ];

    public function user(){
        return $this->belongsTo("App\User");
    }

}

和用户模型

public function hotels(){
    return  $this->hasMany('App\hotel', 'created_by');
}

这是迁移

Schema::create('hotels', function (Blueprint $table) {
                $table->id();
                $table->string('name', 80);      
                $table->integer('ceated_by')->unsigned()->references('id')->on('users')->onDelete("cascade");
                $table->string('image');
                $table->timestamps();

我尝试刷新迁移但仍然相同

标签: laravellaravel-5eloquentlaravel-7

解决方案


'ceated_by'与您的控制器方法不同,迁移文件中的列拼写错误created_by。请改成created_by

$table->integer('created_by')->unsigned()->references('id')->on('users')->onDelete("cascade");

推荐阅读