user860511
user860511

Reputation:

Laravel - Undefined variable

I'm having difficulty accessing a variable, when it's clearly there within the table.

The error falls on this line:

@if ($alert->alert_when == 1)
@endif

Undefined variable: alert_when - is the error.

I dd() the get() request prior to accessing the data in the view and I can see the variable it's retrieving:

["alert_when"]=> string(1) "1"

Full dd print screen:

object(Illuminate\Database\Eloquent\Collection)#521 (1) {
["items":protected]=> array(2)
{ [0]=> object(Criteria)#492 (20) { ["connection":protected]=> NULL
["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id"
["perPage":protected]=> int(15) ["incrementing"]=> bool(true)
["timestamps"]=> bool(true) ["attributes":protected]=> array(15) {
["id"]=> string(2) "19" ["user_id"]=> string(2) "23"
["alert_when"]=> string(1) "1"
["created_at"]=> string(19) "0000-00-00 00:00:00"
["updated_at"]=> string(19) "0000-00-00 00:00:00" ["deleted_at"]=> NULL }

I am able to access other data within the row, and there definitely isn't any typos!

Why is this error occurring? Many thanks in advance.

Update

@if ($alert['alert_when'] == 1) returns the correct response without errors. Why is this when I can access $alert->name like that?

Upvotes: 0

Views: 656

Answers (1)

JMc
JMc

Reputation: 355

Do a var_dump on the variable. You might be referencing it wrong. It could be an array. You might have to reference it like this:

$alert[0]->alert_when

Upvotes: 1

Related Questions