0

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?

4
  • So is $alert an object or an array? If the latter, you reference using $alert['alert_when'] Commented Oct 31, 2014 at 14:40
  • I can access other data within the row using, for example, {{ $alert->alert_name }}. This works perfect. Commented Oct 31, 2014 at 14:42
  • Your dd output suggests that it is an array; but show the full output from dd to be certain Commented Oct 31, 2014 at 14:44
  • I've added it to the initial question @MarkBaker. Commented Oct 31, 2014 at 14:47

1 Answer 1

1

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
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.