【问题标题】:How solve error Column not found: 1054 Unknown column '_token' in 'field list' (SQL: update?如何解决错误 Column not found: 1054 Unknown column '_token' in 'field list' (SQL: update?
【发布时间】:2019-06-26 23:52:26
【问题描述】:
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 为 true,但是当我单击按钮更新数据时出现此问题列未找到:1054 Unknown column '_token' in 'field list' (SQL: update what's solve?

【问题讨论】:

  • 您可以在错误消息中添加您返回的查询吗?
  • hdifen这个查询更新programs内部联接guest_showprogramsprogram_id = guest_showprogram_id_token = LiQTnFrnk8Vb96XS7ovyYmmBJJjpweiI71fV7R24,guest_fname =محمد,guest_lname =فهمي,profession =فاحصجودة,mobile = 796548234,work_phone = 69832511,current_job =شركةمكامن,previous_job =شركةمكامن,work_address =عمان,DOB = 1988- 11-03,DD 987654339 @ =نبیالبلی,specialty_name 987654340 subject 987654341 @ =جودةدةنابيبالنفط,show_date = 2018-10-30,updated_at = 2019-02-03 06:55:26 其中guest_show.guest_id = 3)
  • @AlaaSalah 检查我的更新答案。

标签: php laravel


【解决方案1】:

这是一个已知错误,可以在 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']);
}

【讨论】:

  • 非常感谢 Iftikhar uddin ...我将在模型中执行功能然后在控制器中调用它...运行时我分享我的答案。
  • 试试答案,让我知道输出是什么!如果有任何错误,请分享,以便我调试!
猜你喜欢
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 2018-09-10
相关资源
最近更新 更多