public function edit($id){
$guests=Guest::with('programs', 'specialties')->find($id);
return view('editform',compact('guests'));
}
public function update(Request $request, $id)
{
$guestupdate = Guest::find($id);
$guestupdate->programs()->update($request->all());
$guestupdate->specialties()->update($request->all());
return redirect('/home/show'.$id)->with('alert', 'You have successfully updated');
}
class Guest extends Model
{
protected $primaryKey = 'guest_id';
protected $table = 'guests';
protected $fillable =
['guest_fname','guest_lname','profession','mobile','work_phone',
'current_job','previous_job','work_address','DOB','DD','program_name','specialty_name'];
public function programs()
{
return $this->belongsToMany(Program::class, 'guest_show', 'guest_id', 'program_id')
->withPivot('subject', 'show_date');
}
public function specialties()
{
return $this->belongsToMany(Specialization::class, 'guest_speciality', 'guest_id', 'speciality_id');
}
class Program extends Model
{
protected $table = 'programs';
protected $primaryKey = 'program_id';
protected $fillable = ['program_name','subject','show_date'];
public function guests()
{
return $this-
>belongsToMany(Guest::class,'guest_show','program_id','guest_id')
->withPivot('subject', 'show_date');
}
}
class Specialization extends Model
{
protected $table = 'specialties';
protected $primaryKey = 'specialty_id';
protected $fillable = ['specialty_name'];
public function guests()
{
return $this-
>belongsToMany(Guest::class,'guest_speciality','speciality_id','guest_id');
}
这是我的控制器和模型的代码,用来编辑数据(只需通过guest_id显示),然后我将从多对多关系中更新请求的所有字段。编辑URL是正确,但当我单击按钮更新数据时,出现此问题列未找到:1054未知列'_token‘in 'field list’(SQL:更新什么是解决方案?
发布于 2019-02-02 18:15:48
这是一个已知错误,可以在here中找到。我认为您的错误来自$request->all()
,其中包括来自csrf_field()
的_token
因此,请尝试在更新函数中将$request->all()
更改为$request->except(['_token'])
;。您的新更新函数应该如下所示
public function update(Request $request, $id) {
$guestupdate = Guest::find($id);
$guestupdate->programs()->update($request->except(['_token']));
$guestupdate->specialties()->update($request->except(['_token']));
return redirect('/home/show'.$id)->with('alert', 'You have successfully updated');
}
更多关于Laravel HTTP Requests的研究
另一个问题可能是->withPivot
中的语法,withPivot接受数组,所以将您的代码更改为以下代码:
public function programs() {
return $this->belongsToMany(Program::class, 'guest_show', 'guest_id', 'program_id')
->withPivot(['subject', 'show_date']);
}
https://stackoverflow.com/questions/54495831
复制相似问题