Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 571 Bytes

resource-selectable.md

File metadata and controls

29 lines (23 loc) · 571 Bytes

Selectable JSON Resource

SelectionResource::withLabel('full_name');
$formatted = SelectionResource::make(Model::all());
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;

class SelectionResource extends JsonResource{

    public static $label = 'name';

    public static function withLabel($attribute){
        static::$label = $attribute;
    }

    public function toArray($request)
    {
        return [
            'value' => $this->id,
            'label' => $this->{static::$label},
        ];
    }
}