dynamitem
dynamitem

Reputation: 1669

Laravel 4 query display

I'm having a tiny problem. I can't find a way to return queries (to display on an php page). I'm using something like this:

    public function players()
{
   $players = Player::all();
   return View::make('aac.test', array('players' => $players));
}

My test php page is blank. On the profiler it shows that the query was executed.

I've also tried without something like this:

    public function test()
{
         $id = 3;
         $player = Player::find($id);
         $name = $player->name;
         $level = $player->level;

         return View::make('aac.test');

}

and then in the layout:

    <html>
<head>
    <title></title>
</head>
<body>
  <? $name ?>``
</body>
</html>

or even echoing that. It just won't display. The query is executed. I'm kind of new to all of this, if you could explain me..

It only works if I do it like this:

        public function test()
{
         $id = 3;
         $player = Player::find($id);
         $name = $player->name;
         $level = $player->level;

         return $name;

}

Upvotes: 1

Views: 170

Answers (1)

Altrim
Altrim

Reputation: 6756

Try these steps see if it solves your problem.

  • First make sure that the view exists in app/views/aac/test.php.
  • Check if your route is deifined correctly e.g Route::get('/players','ControllerName@players').

Then if you want to return all players in your controller pass the players to the view

public function players()
{
 $players = Player::all();
 return View::make('aac.test')->with('players' => $players);
}

And in your view views/aac/test.php you need to iterate through players. (e.g you can display them in a list)

<ul>
<?php foreach($players as $player):?>
    <li><?php echo $player->name; ?></li>
<?php endforeach;?>
</ul>   

If you want just one player

 public function oneplayer()
 {
  $player = Player::find(1);
  return View::make('aac.test')->with('player',$player); 
 }

And in your view now you have access to the player object

 <?php echo $player->name; ?> 

Upvotes: 2

Related Questions