当我尝试保存新对象时,出现此错误:
SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ epigrafe”(SQL:插入到articulo
(titulo
,epigrafe
,revista
,updated_at
,created_at
)值(Nuevo,1,1,2016-04-11 08:23:32,2016-04-11 08:23:32))
插入内容应位于epigrafe_id(也为revista_id)上。
模型:
命名空间应用;
使用Illuminate \ Database \ Eloquent \ Model;
class Articulo extends Model
{
/**
* Model table
*
*/
public $table = "articulo";
protected $fillable = [
'titulo',
];
public function revista()
{
return $this->belongsTo('App\Revista');
}
public function autores()
{
return $this->belongsToMany('App\Autor');
}
public function epigrafe()
{
return $this->belongsTo('App\Epigrafe');
}
}
控制器:
public function store(Request $request)
{
$this->validate($request, [
'titulo' => 'required|max:100',
'revista' => 'required',
'epigrafe' => 'required',
]);
$data = $request->all();
$articulo = new Articulo;
$articulo->titulo = $request->titulo;
$articulo->epigrafe = $request->epigrafe;
$articulo->revista = $request->revista;
$articulo->save();
$articulo->autores()->articulos()->attach($articulo->id);
$articulo->autores()->attach($request->autor);
Session::flash('message', 'Articulo creado satisfactoriamente!');
return redirect()->to('articulo');
}
最佳答案
将您的代码更改为正确的列名称。
$articulo->epigrafe_id = $request->epigrafe;
$articulo->revista_id = $request->revista;