Conditional Attributes: Merging Conditional Attributes

/**
 * Transform the resource into an array.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        $this->mergeWhen(Auth::user()->isAdmin(), [
            'first-secret' => 'value',
            'second-secret' => 'value',
        ]),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

MergeWhen method

Should not be used within arrays that mix string and numeric keys.

It should not be used within arrays with numeric keys that are not ordered sequentially.

Conditional Attributes: Merging Conditional Attributes — Structure map

Clickable & Draggable!

Conditional Attributes: Merging Conditional Attributes — Related pages: