Laravel 4 从数据库中提取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17860964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:10:52  来源:igfitidea点击:

Laravel 4 pulling data from database

laravellaravel-4

提问by dynamitem

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.

我有一个叫做“玩家”的桌子。我想从数据库中提取每个玩家,或者简单地选择列“在线”= 1 中的玩家。我想在“玩家”表中显示他们的姓名(列“姓名”)。

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.

它们都不起作用。

回答by Antonio Carlos Ribeiro

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>

回答by iori

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>