0

我得到了一个带有两个可能的支点的资源。现在我想根据加载的枢轴添加值。

这就是我目前的做法:

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\PotentiallyMissing;


class CarAttributeResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'         => $this->id,
            'title'      => ($title
                = $this->whenPivotLoaded('car_car_attribute',
                function () {
                    return (boolean)$this->pivot->custom === true
                        ? $this->pivot->title : $this->title;
                })) instanceof PotentiallyMissing
            && $title->isMissing() ? (($title_ppa
                = $this->whenPivotLoaded('car_bundle_car_bundle_attribute',
                function () {
                    return (boolean)$this->pivot->custom === true
                        ? $this->pivot->title : $this->title;
                })) instanceof PotentiallyMissing
            && $title_ppa->isMissing() ? $this->title : $title_ppa) : $title,
        ];
    }
}

一点都不好看,对吧?

你们是否看到任何使这更优雅和可读的可能性?

4

0 回答 0