简体   繁体   中英

How solve error Column not found: 1054 Unknown column '_token' in 'field list' (SQL: update?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM