cespon
cespon

Reputation: 5760

Laravel 5.3 get boolean value from associative array

I have a ImageController like this:

$image = Image::where('id', $id)->first();

return [
    'image' => $image,
    'image_360' => $image['360']
];

The previous lines return to the browser the following:

{
    "image": {
        "id": 1,
        "name": "default.jpg",
        "360": 1,
    },
    "image_360": null
}

The Image migration:

$table->increments('id');
$table->string('name');
$table->boolean('360');

The Image model:

class Image extends Model
{
    protected $fillable = ['name', '360'];

    protected $casts = [
        '360' => 'boolean'
    ];
}

Why $images['360'] returns null if its value is true?

Upvotes: 1

Views: 581

Answers (1)

LF-DevJourney
LF-DevJourney

Reputation: 28529

Here is the workaround way: I've tryed many ways but havenot get a direct way to access the number value as descripted in this post

return [
    'image' => $image,
    'image_360' => array_values($image->toArray())[2];
];

Upvotes: 1

Related Questions