laravel - 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();
我尝试刷新迁移但仍然相同
解决方案
'ceated_by'
与您的控制器方法不同,迁移文件中的列拼写错误created_by
。请改成created_by
$table->integer('created_by')->unsigned()->references('id')->on('users')->onDelete("cascade");
推荐阅读
- php - 到我的网络服务器的奇怪请求,它们是恶意的吗?
- reactjs - React.js 中的 useMemo 钩子是否也可以记住对象引用?
- velo - 我正在尝试将图像随机化器添加到蜡条。我需要编辑代码但不知道如何
- lua - 当 lua 函数异常退出时,带有 'r' 的 Lua sethook 不会触发
- javascript - 如何在javascript中操作日期变量?
- django - 如何正确设计我喜欢的模型?
- entity-framework - 比较更改时,实体框架未获取原始实体
- asp.net - 如何在脚手架 MVC 视图中允许十进制数中的逗号?
- javascript - addeventlistener 不运行函数
- javascript - React - 将变量传递给 css 表