获取属于特定图库的所有图像

时间:2017-07-13 12:02:32

标签: laravel eloquent

我希望获得属于特定图库的所有图片。必须从Image类中选择这些图像。

这是关系,在Image类中:

public function galleries()
{
    return $this->belongsToMany(Gallery::class, 'gallery_image');
}

并在Gallery内:

public function images()
{
    return $this->belongsToMany(Image::class, 'gallery_image', 'gallery_id', 'image_id')->withPivot('order');
}

我试过这个:

$images = Image::with(array(
    'galleries' => function ($query) use($search)
    {
        $query->where('slug', $search);
    }->paginate()

但即使我发送特定的图库,它仍会返回所有图像。

有人可以帮我解决这个问题吗?

必须使用Image ::而不是Gallery ::我必须通过图像类而不是Gallery类选择图像,因为我正在执行此操作的特定方式。所以我需要上面的查询帮助,而不是建议使用Gallery :: some_query_here

2 个答案:

答案 0 :(得分:1)

您应该可以使用PropertyChangedHelper { theProperty: containerItem.currentContainer onThePropertyChanged: { console.log(containerItem.currentContainer.name); } }

whereHas

希望这有帮助!

答案 1 :(得分:0)

为什么不首先尝试获取图库,然后获取图像:

$gallery = Gallery::with('images')->whereSlug($slug)->first();
$images = $gallery->images;
相关问题