foreach 回圈中的 id 欄位存在于我要加入的兩個表中,我只需要將它參考到 foreach 回圈中的一個表(即 program_types.id):
$program_details = DB::table('program_allocation')
->where('twilio_wa_from_no', '=', $tophonenumber)
->where('twilio_sid' , $account_sid)
->join('program_types', 'program_allocation.program_type', '=', 'program_types.id')
->get();
foreach ($program_details as $program) {
$program_allocation_id = $program->id;
}
uj5u.com熱心網友回復:
您可以使用select('program_allocation.*', 'program_types.id AS program_type_id')來指定要使用的查詢中包含哪些表的哪些列。
$program_details = DB::table('program_allocation')
->where('twilio_wa_from_no', '=', $tophonenumber)
->where('twilio_sid', $account_sid)
->join('program_types', 'program_allocation.program_type', '=', 'program_types.id')
->select('program_allocation.*', 'program_types.id AS program_type_id')
->get();
您可以在官方 Laravel 檔案中查看:https ://laravel.com/docs/9.x/queries#inner-join-clause
uj5u.com熱心網友回復:
選擇欄位并為連接表 id 之一指定別名
$program_details = DB::table('program_allocation')
->join('program_types', 'program_allocation.program_type', '=', 'program_types.id')
->select('program_types.*', 'program_allocation.id as allocationID', )
->where('twilio_wa_from_no', '=', $tophonenumber)
->where('twilio_sid' , $account_sid)
->get();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/430340.html
