0

I have table called 'players'. I want to pull every player from the database, or simply select players that have in column 'online' = 1. I want to display their name (column 'name') in 'players' table.

Here's what I've tried:

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

also tried:

    public function online()
{
  $results = DB::select('select * from players where level = 1');
  return View::make('aac.test')->with('results', $results);
}

None of them works.

2 Answers 2

3

Try this:

public function online()
{
  $results = Player::where('online', 1)->get('name');
  return View::make('aac.test')->with('results', $results);
}

To display it using blade:

<ul>
    @foreach($results as $result)
        <li>
            {{$result->name}}
        </li>
    @endforeach
</ul>
Sign up to request clarification or add additional context in comments.

13 Comments

It works now, so I need to include the get method or w/e it is for working pulling data? Also how could I only display their name? Should I do something like this? <?php foreach($results->players as $player):?> <?php echo $player->name ?> <?php endforeach;?>
You already have your list of players in that collection. Just edited to show you how.
stackoverflow.com/users/1508618/alexander-cogneau proposed to use pluck('name') to retrieve just the name column from your table, but I think that pluck() just return the first record, but you can also do it using get('name') or get(array('id','name)). Edited.
hi, sorry for disturbing, I would like to ask you a question. How could I say when there's no players online (no id's of 1 in column 'online) There is no players online. ?
if empty($results) then you have no records of online users.
|
0
public function online()
{
  $results = Player::where('level','=','1')->get();
  return View::make('aac.test')->with('results', $results);
}

To display it using blade:

<ul>
    @foreach($results as $result)
        <li>
            {{$result->name}}
        </li>
    @endforeach
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.