OunknownO OunknownO - 4 months ago 7
PHP Question

Undefined variable in view when pulling specific data

It says

undefined variable
when I try to pull variable through compact

this is my controller

public function show()
{
$list = List::find(1)->task();
return view('configuration.configuration', compact($list));
}


this is my view

@foreach($list as $value)

<span> {{ $value->tasks }}</span>

@endforeach

Answer

SUGGESTIONS/CORRECTIONS

In $list = List::find(1)->task(); If you are trying to retrieve all task from list, then I this you are doing it wrong instead you should do something like this:

$list = List::find(1)->task;

Again in return view('configuration.configuration', compact($list));, if you want to convert to array then compact() won't do that.

this return view('configuration.configuration', ['list'=>$list->toArray()]); will do that

An to retrieve or print in view(blade), try something like this

@foreach($list as $value=>$val)
   <span> {{{ $val['task'] }}}</span> //"task" is just an assumption, replace it with your own ColumnName
@endforeach

if you don't want convert the $list in array

CONTROLLER

public function show()
{
    $list = List::find(1)->task;
    return view('configuration.configuration',['list'=> $list]);
}

VIEW

@foreach($list as $value)
   <span> {{ $value->tasks }}</span>
@endforeach