OunknownO
OunknownO

Reputation: 1206

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

Upvotes: 0

Views: 58

Answers (3)

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Instead of using compact() try with($list)

Upvotes: 0

jonju
jonju

Reputation: 2736

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

Upvotes: 1

Antonio Gocaj
Antonio Gocaj

Reputation: 300

Maybe change your return view to :

return view('configuration.configuration',['list'=>$list]);

Upvotes: 0

Related Questions