hatemjapo hatemjapo - 1 month ago 4
PHP Question

laravel passe array from controller to view undefined

i am new to laravel.
I am trying to route to a controller, get some data out of the model and passing it to the view like this:

class PlayerController extends Controller{

public function index(){
$data['start'] = Player::allPlayer()->get();

return View::make('pages.player')->with($data);
}
}


now when i try to read the $data in the view. It says that it is undefined..

@if (isset($data))
{{'its there'}}
@else
{{'its not there'}}
@endif


what am i doing wrong? i tried to print_r the array in the controller. And it looks good...

Answer

You have to access the variable by it's key:

class PlayerController extends Controller{

 public function index(){
   $data['start'] = Player::allPlayer()->get();

   return View::make('pages.player')->with($data);
 }
}

View:

@if (isset($start)) <--by key
{{'its there'}}
@else
{{'its not there'}}
@endif

DOCS