在此coulmn laravel上的联接表后获取null列
目的地有两种类型,一种是航班,另一种是地区,以及 如果目的地类型为地区,则在此coulmn laravel上的联接表后获取null列,laravel,join,eloquent,Laravel,Join,Eloquent,目的地有两种类型,一种是航班,另一种是地区,以及 如果目的地类型为地区,则航班id将为空 DB::table('destinations as d') ->join('flights as f','d.flight_id','=','f.flight_id') ->where('f.status','=','ACTIVE') ->whereNull(['d.deleted_at', 'f.deleted_a
航班id
将为空
DB::table('destinations as d')
->join('flights as f','d.flight_id','=','f.flight_id')
->where('f.status','=','ACTIVE')
->whereNull(['d.deleted_at', 'f.deleted_at'])->paginate(20);
我还需要获取目的地
其中目的地。flight\u id=null
有了这个加入,我不能,因为这个加入是在
flight\u id
上的,只需在查询中添加另一个条件即可
DB::table('destinations as d')
->join('flights as f','d.flight_id','=','f.flight_id')
->where('f.status','=','ACTIVE')
->where('d.flight_id','=','NULL') // added
->whereNull(['d.deleted_at', 'f.deleted_at'])
->paginate(20);
我明白问题所在
DB::table('destinations as d')
->select('d.*','f.flight_id as flightId','f.flight_number','f.departure','f.arrival','f.air_line','f.air_port','f.stops')
->leftJoin('flights as f','d.flight_id','=','f.flight_id')
->whereNull(['d.deleted_at', 'f.deleted_at'])
->paginate(20);
我应该区分目的地.flight\u id
和航班.flight\u id
,因为通过添加select()
但这不是确切的问题,问题在于连接它必须是左连接
这是我得到的一些数据作为例子:
}
3 => {#500 ▼
+"destination_id": 33
+"name": "USA"
+"flight_id": 3
+"type": "FLIGHT"
+"status": "ACTIVE"
+"created_at": "2020-01-27 14:41:17"
+"updated_at": "2020-01-27 14:41:17"
+"deleted_at": null
+"flightId": 3
+"flight_number": "#A205"
+"departure": "2020-01-27 01:00:00"
+"arrival": "2020-01-31 01:00:00"
+"air_line": "sss"
+"air_port": "zzz"
+"stops": 2
}
4 => {#1011 ▼
+"destination_id": 34
+"name": "BEA"
+"flight_id": null
+"type": "REGION"
+"status": "ACTIVE"
+"created_at": "2020-01-27 14:42:57"
+"updated_at": "2020-01-27 14:42:57"
+"deleted_at": null
+"flightId": null
+"flight_number": null
+"departure": null
+"arrival": null
+"air_line": null
+"air_port": null
+"stops": null
}
您可以共享您的db架构吗?我建议启用查询日志记录,查看执行的SQL查询是什么,然后尝试直接执行查询。@SohaibEl Khatib您可能需要修改第二个where条件,只需使用所需的不同列进行尝试即可