1

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?

11
  • you need to cast it to boolean. Commented Jan 18, 2017 at 3:44
  • If I cast it like (bool)$image['360'], it returns false when the expected value is true Commented Jan 18, 2017 at 3:50
  • var_dump($image['360']) and var_dump($image) ??? Commented Jan 18, 2017 at 4:02
  • 1
    var_dump($image['360']), var_dump($image[360]) and var_dump($image[`360`]) return NULL Commented Jan 18, 2017 at 4:14
  • 1
    I cannot figure it out now. but array_values($image)[2] may be a walkaround. Though it's not a good one. Commented Jan 18, 2017 at 4:16

1 Answer 1

1

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];
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.