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显示),然后我将更新请求的字段从多对多关系。 编辑网址为true,但是当我单击更新数据的按钮时,找不到此问题列:1054“字段列表”中的未知列“ _token”(SQL:更新解决方案?
答案 0 :(得分:0)
这是一个已知错误,可以在here中找到。我认为您的错误来自$request->all()
,其中包括_token
来自csrf_field()
因此,请尝试将$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']);
}