Resource.php 720 Bytes
<?php

namespace FootyRoom\Http;

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

class Resource extends JsonResource
{
    /**
     * Resolve the resource to an array.
     *
     * @param \Illuminate\Http\Request|null $request
     *
     * @return array
     */
    public function resolve($request = null)
    {
        if ($this->resource === null) {
            return $this->nullResource();
        }

        return parent::resolve($request);
    }

    /**
     * Method that gets called instead of toArray() when resource is null.
     *
     * @return array
     */
    public function nullResource(): array
    {
        return [
            'data' => null,
        ];
    }
}