添加新数据后,数据透视表无法插入数据

时间:2018-03-27 06:44:14

标签: php laravel

我有三个表:user,request和request_user我想在数据透视表中插入user_id和req_id。请帮忙

//code for user.php.  
public function ssrareqs() 
{
    return $this->belongsToMany(Ssrareq::class, 'request_user');
}

request.php

protected $fillable = ['particular_id','quantity_req', 'quantity_issued', 'status_issue', 'status_req', 'purpose', 'remarks', 'date_applied',
    'date_approved', 'date_issued', 'approver', 'issuer'];
public function users()
{
    return $this->belongsToMany(User::class, 'ssrareq_user');

}
protected $primaryKey = 'req_id';

myController的

 public function store(Request $request)
{

    $storereq = new Ssrareq([
        'particular_id' => $request->get('particular_id'),
        'quantity_req' => $request->get('quantity_req'),
        'purpose' => $request->get('purpose'),
        'date_applied' => \Carbon\Carbon::now()->format('Y-m-d'),
    ]);
    $storereq->save();
    $storereq->users()->sync($request);
    return redirect('/requester');
}

0 个答案:

没有答案