Laravel WHERE查询单个列结果

时间:2016-11-01 18:36:03

标签: php laravel routes eloquent

我有以下控制器,它应该返回所有尚未验证的用户。

public function getUserRequests()
{
    $userRequests = User::where('status' ,"Not Verified");

    foreach($userRequests as $user)
    {
        echo $user->name;   
    }
}

输出是空白页面。我该怎么办?

4 个答案:

答案 0 :(得分:3)

您应该使用get()方法获取数据:

$userRequests = User::where('status' ,"Not Verified")->get();

答案 1 :(得分:2)

试试这个:您需要使用get()方法来获取数据。

$userRequests = User::where('status', '=', 'Not Verified')->get();

Laravel Docs

答案 2 :(得分:0)

  

您必须使用get()方法返回数据。

所以,你的功能应该是这样的。

public function getUserRequests()
{
    $userRequests = User::where('status', "Not Verified")->get();

    foreach($userRequests as $user)
    {
        echo $user->name;   
    }
}

答案 3 :(得分:0)

非常简单的一种方法是,

public function getUserRequests()
{
   $userRequests = User::where('status' ,"Not Verified")->get(['name]);

  foreach($userRequests as $user)
  {
    echo $user->name;   
  }
}

public function getUserRequests()
{
   $userRequests = User::where('status' ,"Not Verified")->select('name')->get();

  foreach($userRequests as $user)
  {
    echo $user->name;   
  }
}