在我的專案中,我在資料庫中有兩個表 1st 是存盤票證資料的票證表。第二個是 gp_group,其中票證 ID 存盤在票證 ID 中的 gpid 中。見下圖:-門票表
每當我洗掉票證時,我都必須從票證表和該票證的 gp_group 表中洗掉票證資料。見下圖:- gp_group 表
現在,我的問題是我無法從 gp_group 表中洗掉票證資料,但票證表資料已被洗掉。
因此,當我洗掉票證時,我在洗掉函式中使用了一個函式來從兩個表中洗掉資料。但還是不行。
輸入代碼 TicketController
public function destroy(Ticket $ticket)
{
$ticket =DB::table('tickets')
-> leftJoin('gp_group','tickets.id', '=','gp_group.ticket_id')
-> where('tickets.id',$ticket);
DB::table('gp_group')->where('ticket_id',$ticket)->delete();
$ticket->delete();
return redirect()->route('tickets.index')
->with('success','Group deleted successfully');
}
我收到此錯誤“類 Illuminate\Database\Query\Builder 的物件無法轉換為字串”
并在函式中嘗試其他代碼
public function destroy(Ticket $ticket)
{
$ticket->delete();
ticket::where("id", $ticket)->delete();
gp_group::where("gpid", $ticket)->delete();
return redirect()->route('tickets.index')
->with('success','Group deleted successfully');
}
但還是不行。
我得到不同的錯誤
“SQLSTATE[42S02]:未找到基表或視圖:1146 表 'rp1.gp_groups' 不存在(SQL:從
gp_groupswheregpid= {"id":63,"gpname":"Nasser","detail":洗掉: "Nasser","gender":"Male","semester":432,"status":"未分配","Assigned":null,"user_id":1,"track_id":5,"created_at":" 2021-10-15T18:50:04.000000Z","updated_at":"2021-10-15T18:50:04.000000Z"})"
如果有人能指出我在這里做的錯誤,我將非常感激。
uj5u.com熱心網友回復:
為此,您需要在表中創建一個外鍵delete on cascade:
外鍵約束
然后您可以洗掉父注冊表(tickets表),并自動洗掉gp_group表中參考其他表的注冊表的所有專案。
uj5u.com熱心網友回復:
快速解決您的問題,您只需要將 Id 傳遞給 where 函式而不是像這樣的整個票
-> where('tickets.id',$ticket->id);
這是完整的更新功能
{
$ticket->delete();
gp_group::where("gpid", $ticket->id)->delete();
return redirect()->route('tickets.index')
->with('success','Group deleted successfully');
}
并且您可能會考慮該任務的外鍵,它會gp_group在其票證被洗掉時自動洗掉記錄
,在此處閱讀更多相關資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/331080.html
