假设我有3张桌子。
关系是多对多(图像,主题)和多对多(图像,样式)。现在我想做类似的事情:
$result=$subjectResult->images()->merge($styleResult->images())
。我希望结果的类型是图像集合对象。
到目前为止,我已经尝试过:
$subjectResult=Subject::with('images')->where(query)->get();
但这将返回主题行,图像行和数据透视表行。我想从中提取图像集合。
图片模型:
public function styles(){
return $this->belongsToMany('Style','style_images','image_id','keyword_id');
}
public function subjects(){
return $this->belongsToMany('Subject','subject_images','image_id','subject_id');
}
最终目的是使图像集合从主题和样式的查询结果中合并。
好的,这就是对我有用的。使用whereHas
并orWhereHas
为我工作。感谢大家的帮助。
$results=Image::whereHas('subjects',function($q) use ($searchParam){
$q->where('subject','LIKE','%'.$searchParam.'%');
})->orWhereHas('styles',function($q) use ($searchParam){
$q->where('style','LIKE','%'.$searchParam.'%');
})->get();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句