リレーションが見つからない場合、null を空の配列に置き換えることは可能ですか?
例: 顧客には連絡先と契約がありますが、契約の 1 つには Web がありません。
$customers = Customer::with('contacts', 'contracts.web')
->orderBy('company')->orderBy('prename')->get();
結果は次のようになります...
2 => array:21 [
"id" => 1
"contacts" => array:2 [
0 => array:12 [
"id" => 1
"customer_id" => 1
]
1 => array:12 [
"id" => 2
"customer_id" => 1
]
]
"contracts" => array:2 [
0 => array:9 [
"id" => 1
"customer_id" => 1
"web" => array:7 [
"id" => 1
"contract_id" => 1
]
]
1 => array:9 [
"id" => 2
"customer_id" => 1
"web" => null // should be replaced with []
]
]
]
ドキュメント ( Constraining Eager Loads ) で読んだように、熱心な負荷を制限してクエリを操作することしかできません。
アップデート
契約クラス
class Contract extends Model
{
public function web()
{
return $this->hasOne(Web::class);
}
}