我有2个这样雄辩的模型 -
广告 -
class Advertisement extends Model
{
protected $table = 'advertisements'; //Table Name
protected $fillable = [
'user_id',
'category_id',
'sub_category_id',
'title',
'price',
'description',
'address',
'location_lat',
'location_lon',
'is_active'
];
public function AdvertisementImages()
{
return $this->hasMany('App\AdvertisementImage', 'advertisement_id', 'id');
}
}
和 AdvertisementImage -
class AdvertisementImage extends Model
{
protected $primaryKey = null;
public $incrementing = false;
public $timestamps = false; // disable all behaviour
protected $table = 'advertisement_images'; //Table Name
protected $fillable = [
'advertisement_id',
'image_name'
];
}
我想用它的图像来获取广告数据。所以我正在做的就是这个 -
Advertisement::withTrashed()
->with('AdvertisementImages')
->orderBy('created_at', 'desc')
->get();
得到这样的东西 -
对我来说非常好。但我的问题是,如果任何广告没有任何图像,那么添加不包括。所以,我不能得到这样的结果 -
[
{
"id": 4,
"user_id": 4,
"title": "Anything",
"price": 12,
"description": "sd sdf sdf sdf 123",
"address": "Baridhara, Dhaka, Dhaka Division, Bangladesh",
"advertisement_images": [
]
},
{
"id": 2,
"user_id": 4,
"title": "Anything new",
"price": 12,
"description": "sd sdf sdf sdf 123",
"address": "Boshudhara, Dhaka, Dhaka Division, Bangladesh",
"advertisement_images": [
{
"image_name": "4a06cb3f00aab0545e6b6d.jpg"
},
{
"image_name": "4a2a46b5bbc7fd4c2dc64c.jpg"
},
{
"image_name": "4acababf2bea9aa42b3188.jpg"
}
]
},
{
"id": 3,
"user_id": 4,
"title": "new",
"price": 12,
"description": "sd sdf sdf sdf 123",
"address": "Baridhara, Dhaka, Dhaka Division, Bangladesh",
"advertisement_images": [
]
},
{
"id": 1,
"user_id": 4,
"title": "13",
"price": 12,
"description": "123",
"advertisement_images": [
{
"image_name": "4a0c5735b4dafdda6e957f.jpg"
}
]
}
]
ID = 4且id = 3,应包含此类元素
那么,我还需要做些什么来包括这些结果(包括在eloquent中的emptl列表)?
提前感谢您的帮助。