0
是否可以合并一个雄辩的集合与查询生成器?在Laravel中合并雄辩集合和查询?
在这个例子中,我有3个表格。图片表格,标签表格和图片标签表格。我在图像和标签模型中也有雄辩的关系。
Tag.php:
class Tag extends Model {
protected $table = 'tags';
public $timestamps = false;
protected $fillable = [
'tagname',
];
public function Images() {
return $this->belongsToMany('CommendMe\Models\Images');
}
}
Images.php:
public function tags() {
return $this->belongsToMany('CommendMe\Models\Tag');
}
我希望做的是这样的:
$tag = Tag::where('tagname', $query)->first();
$imagesWithTag = $tag->images;
$imagesWithoutTag = Images::where('title', 'LIKE', "%{$query}%");
$images = $imagesWithTag + $imagesWithoutTag
->whereIn('mature', [0, $mature])
->where('created_at', '>=', Carbon::now()->subHours($withinHours))
->orderBy($orderByString, 'desc')
->Paginate(50);
本质只是采取查询(返回一个来自图像表的n数组)和集合(它也从图像表返回数组),对结果进行组合和排序。
这可能吗?
编辑2:
当试图伞兵的新的应答,则出现下列错误:
FatalThrowableError在SearchController.php管线98:调用一个成员 函数整数
指定者()
更新了我的文章。我想指出图像表和标签表不直接引用对方。然而,这些images_tag表的确如此。这些列被称为tag_id和image_id。你的帖子试图从图片表中获取tag_id,而不是数据透视表。 –
我的图像表的名称确实是“图像”,其主键是“id”。但是,我仍然遇到错误。我更新了我的帖子。 –
它的工作原理!谢谢。 –