我有模型文章,文件组,文件。
使用图片保存新文章时,会显示以下错误:
{“exception”:“Illuminate \ Database \ QueryException”,“message”:“SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'image'(SQL:插入{{1} (
articles
,title
,text
,like_count
,view_count
,comment_count
,image
,web_image
,updated_at
)值(dsvdscdscdscsdcdsc,sacsaxasxsaxsaxsaxsaxsax,0,0,0,11,13989,2017-06-27 15:40:49,2017-06-27 15:40:49))“,”trace“ :[{ “文件”: “/无功/网络/ laravel /网络/供应商/ laravel /框架/ SRC /照亮/数据库/ Connection.php”, “行”:726, “功能”: “runQueryCallback”,“类“:”Illuminate \ Database \ Connection“,”type“:” - >“,”args“:[”插入created_at
(articles
,title
,{{1} },text
,like_count
,view_count
,comment_count
,image
,web_image
)值(?,?,?,?,?, ?,?,?,?)“,..................
模型/ Article.php
updated_at
模型/ FileGroup.php
created_at
模型/ file.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $guarded = ['id'];
protected $with = ['image'];
public function image()
{
return $this->belongsTo('App\Models\Filegroup', 'image_id');
}
}
使用迁移创建的表文章:
use Illuminate\Database\Eloquent\Model;
class Filegroup extends Model
{
protected $table = 'file_groups';
protected $with = ['files', 'name'];
protected $guarded = ['id'];
/**
* One-to-Many relations with SiteString.
*
* @foreignModel SiteString
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function name()
{
return $this->belongsTo('App\Models\SiteString', 'name_id');
}
/**
* Many-to-Many relations with File.
*
* @foreignModel File
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function files()
{
return $this->hasMany('App\Models\File', 'filegroup_id');
}
}
答案 0 :(得分:0)
当您在模型而不是$fillable
上使用$fillable
属性时,它会尝试在模型的属性数组中插入每个值。更多相关内容:https://laravel.com/docs/5.4/eloquent#mass-assignment
您可以确保分配给文章模型的属性数组仅包含具有相应数据库列的属性,或者您可以切换到protected $fillable = [
'image_id',
'text',
'title',
'like_count',
'view_count',
'comment_count',
];
,如下所示:
change()