dpkk8687 2019-02-02 17:53
浏览 541

如何解决错误未找到列:1054'字段列表'中的未知列'_token'(SQL:更新?

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');
}

This is my code of controller and model to edit data(just show by guest_id) then I would to update request all field from many to many relationship. The edit URL true but when I click button to update data appear this problem column not found:1054 Unknown column '_token' in 'field list' (SQL: update what's solve?

  • 写回答

1条回答 默认 最新

  • douchixu3686 2019-02-02 18:15
    关注

    This is a known error and can be found here. I think your error comes from $request->all() which includes _token coming from csrf_field()

    So try changing $request->all() to $request->except(['_token']); in your update function. Your new update function should look like below

    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');
    }
    

    For more study Laravel HTTP Requests

    Another issue might be the syntax in ->withPivot, withPivot accepts array so change your code to below:

    public function programs() {
        return $this->belongsToMany(Program::class, 'guest_show', 'guest_id', 'program_id')
            ->withPivot(['subject', 'show_date']);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上