0

我正在尝试从 Spatie 的 laravel-medialibrary 中加载所有标识 URL:

然后他们将使用 json 响应。

理想情况下,我图书馆的工作方式如下:

# Get all clients with url to logotype (small)`
    Client::with('media.logotype', function ($q) {
        $q->where('mediaConversion', 'small')->getUrl();
    })->get()



# Get all clients with all urls for media:logotype    
Client::with('media.logotype', function ($q) {
       $q->getUrl();
    })->get()

想法?

4

1 回答 1

0

我得到了 josn,然后我创建了另一个变量容器 URL,如果它只是在图像上,那么:

foreach ($doctors as $doctor)
    if ($doctor->image != null)
        $image_url = '';
        $image_url = $doctor->image->thumbnail;
        $doctor->image_url = $image_url;

然后我通过将图像,媒体添加到隐藏数组中来隐藏Doctor模型中的原始模型(如果没有,则只需在模型中创建变量)

    protected $hidden = ['media', 'image'];

如果您有多个图像,您可以这样做:

        $images_url = [];
        foreach($portfolio->images as $images){
            $images_url[] = $images->url;
        }
        $portfolio->images_url = $images_url;

另一个例子

    public function show($doctor_id)
    {
        $portfolios = Portfolio::where('doctor_id', $doctor_id)->get();
        foreach ($portfolios as $portfolio) {
            $images_url = [];
            foreach ($portfolio->images as $images) {
                $images_url[] = $images->url;
            }
            $portfolio->images_url = $images_url;
        }
        return new PortfolioResource($portfolios);
    }

于 2021-03-15T10:47:29.647 回答