如何遍历具有多个记录的 eloquent 对象?

How to iterate through eloquent object which has multiple records?

我正在使用以下代码从 table 中获取记录:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Collection;
use App\Http\Requests;
//use App\tempLogin;

class loginController extends Controller
{
    public function checkCredentials(Request $request){

        $mobile = $request->mobile;

        $users = App\tempLogin::where('mobile','=',$mobile)->get();


        return $user->name;

    }

但这是 eloquent 对象,所以它向我抛出一个错误。还有什么方法可以做到这一点?

您正在尝试使用未定义的 $user 变量。您应该遍历集合以获得单个用户:

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

通常你会像这样循环收集项目:

$users = App\tempLogin::where('mobile','=',$mobile)->get();

if($users->count() > 0)
{
   forach($users as $user)
   {
      // do something with the App\User model
      $name = $user->name;
   }
}

但如果您想以数组形式访问用户集合,请像这样更改查询:

$users = App\tempLogin::where('mobile','=',$mobile)->toArray();

然后你可以这样访问它:

foreach($users as $user)
{
    // now the $user contains an array containing each row data.
    $name = $user["name"];
}

如果您想访问单用户:

$user = App\tempLogin::where('mobile','=',$mobile)->first();
$name = $user->name;

这将为您获取第一行(偏移量 0,限制 1)。您需要指定顺序以获得所需的第一行。

你打错了

$users = \App\tempLogin::where('mobile','=',$mobile)->get();//you are getting $users

& returning

return $user->name;

所以改成

return $users->name;

但我仍然怀疑它是否会解决您的问题,因为您正在使用 get & get returns a collection 除非你在 tempLogin 模型中有关系。

So I assume you have a record in DB which has mobile equal to something & you want to return the name of that if this is the case then you can use this solution

$users = \App\tempLogin::where('mobile','=',$mobile)->firstOrFail();//source https://github.com/laravel/framework/blob/5.3/src/Illuminate/Database/Eloquent/Builder.php#L299-L306

然后return

return $users->name;

If you have multiple records in DB & you want to do a get then you can do it like this way

$users = \App\tempLogin::select(['name'])->where('mobile','=',$mobile)->get();

这只会 select name 来自 table

现在你可以return

return $users;

这将是 tempLogin 模型的集合 & 如果您将它用于视图 api 它将自动成为 JSON使用 laravel 这样的请求编码

[{"name":'YOUR_NAME_OF_THE_RECORD'},{"name":'YOUR_NAME_OF_THE_RECORD'}]

或者如果你想return将记录作为数组,你可以像这样使用

$users = \App\tempLogin::select(['name'])->where('mobile','=',$mobile)->pluck('name')->all();
return $users;

这将return这样的数组

[
 "YOUR_NAME_OF_THE_RECORD",
 "YOUR_NAME_OF_THE_RECORD",
]You have a typo

$users = \App\tempLogin::where('mobile','=',$mobile)->get();//you are getting $users

& returning

return $user->name;

所以改成

return $users->name;

但我仍然怀疑它是否会解决您的问题,因为您正在使用 get & get returns a collection 除非你在 tempLogin 模型中有关系。

So I assume you have a record in DB which has mobile equal to something & you want to return the name of that if this is the case then you can use this solution

$users = \App\tempLogin::where('mobile','=',$mobile)->firstOrFail();//source https://github.com/laravel/framework/blob/5.3/src/Illuminate/Database/Eloquent/Builder.php#L299-L306

然后return

return $users->name;

If you have multiple records in DB & you want to do a get then you can do it like this way

$users = \App\tempLogin::select(['name'])->where('mobile','=',$mobile)->get();

这只会 select name 来自 table

现在你可以return

return $users;

这将是 tempLogin 模型的集合 & 如果您将它用于视图 api 它将自动成为 JSON使用 laravel 这样的请求编码

[{"name":'YOUR_NAME_OF_THE_RECORD'},{"name":'YOUR_NAME_OF_THE_RECORD'}]

或者如果你想return将记录作为数组,你可以像这样使用

$users = \App\tempLogin::select(['name'])->where('mobile','=',$mobile)->pluck('name')->all();
return $users;

这将return这样的数组

[
 "YOUR_NAME_OF_THE_RECORD",
 "YOUR_NAME_OF_THE_RECORD",
]