如何解决错误列未发现:在“字段列表” 1054未知列“_token”(SQL:更新?

问题描述 投票:1回答:1
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”(SQL:更新什么的解决?

php laravel
1个回答
0
投票

这是一个已知的错误,可以发现here。我觉得你的错误来自$request->all()包括_tokencsrf_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']);
}
© www.soinside.com 2019 - 2024. All rights reserved.