我在Laravel5.4中工作。我創建了3個表格。在Laravel5.4中插入數據到數據透視表中
用戶表:
票表:
ticket_cc_users表:
現在,我已經在用戶和門票模塊之間創建瞭如下關係。
用戶模型:
public function tickets()
{
return $this->belongsToMany('App\User', 'ticket_cc_users', 'user_id', 'ticket_id');
}
票務模型:
public function users()
{
return $this->belongsToMany('App\Models\Tickets\Ticket', 'ticket_cc_users', 'ticket_id', 'user_id');
}
TicketController控制器保存方法:
public function store(Request $request)
{
return $request->all();
$ticket = new Ticket;
$ticket->requester_id = $this->user['id'];
//$ticket->assignee_id = $request->assignee_id;
//$ticket->cc_id = $request->cc_id;
$ticket->type = $request->type;
$ticket->priority = $request->priority;
$ticket->subject = $request->subject;
$ticket->description = $request->description;
$ticket->status = $request->status;
if($request->link)
{
$ticket->link = $request->link;
$ticket->due_date = null;
}
if($request->due_date && $request->due_date !="")
{
$ticket->due_date = date('Y-m-d',strtotime($request->due_date));
$ticket->link = "";
}
if($ticket->save())
{
$ticket->users()->sync($request->cc_id);
foreach($request->ticket_tags as $value){
$tag = new Tag;
$tag->tag_name = $value['text'];
$tag->save();
$ticketTag = new TicketTag;
$ticketTag->tickets_id = $ticket->id;
$ticketTag->tags_id = $tag->id;
$ticketTag->save();
}
$data = Ticket::find($ticket->id);
Mail::to('[email protected]')->send(new CreateTicket($data));
$response = array(
'success' => true
);
}
return $response;
}
在這裏,我將把數據存儲到票據表中。所以我需要將cc用戶數據存儲到ticket_cc_user表中。那麼如何將ticket_id和user_id存儲到此表中。在這裏,我可以得到多個user_id。我正在使用雄辯的ORM。
在這裏,它給出了一個錯誤,如SQLSTATE [42S22]:列未找到:1054未知列在 '字段列表' $$ hashKey「(SQL:插入ticket_cc_users
($$hashKey
,address
,city_id
,country_id
,created_at
,deleted_at
,email
,firstname
,id
,introducer_id
,is_verified
,lastname
,phone
,signature
,state_id
,ticket_id
,updated_at
,user_id
,username
,userrole_id
)的值(目的:109,,,,2017年2月10日5點26分01秒,,nisarg.b @ 1),2017-02-14 08:33:18,0,nisarg,2))
所以,我應該改變什麼樣的代碼保存功能將數據存入ticket_cc_users
表中?