简体   繁体   English

Laravel Eloquent如何从数组中获取结果

[英]Laravel Eloquent How to fetch results from array

I have been spending hours searching to find this. 我一直在花很多时间寻找这个。

I am very new to Laravel, ( coming from CodeIgniter ) and I am trying to do everything the Laravel way instead of using pure php/sql anywhere unless it is very necessary. 我是Laravel的新手,(来自CodeIgniter),除非有必要,否则我会尝试以Laravel的方式进行操作,而不是在任何地方使用纯PHP / SQL。

$role_id = Role::select('role_id')
                    ->where('type','Admin')
                    ->get();
    var_dump($role_id);

I am trying to fetch $role_id. 我正在尝试获取$ role_id。

The var_dump gives me this. var_dump给了我这个。

object(Illuminate\Database\Eloquent\Collection)#249 (1) { ["items":protected]=> array(1) { [0]=> object(App\Role)#252 (26) { ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(5) "roles" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(1) { ["role_id"]=> int(99) } ["original":protected]=> array(1) { ["role_id"]=> int(99) } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } } }

I expected just a mysql result row instead of this. 我期望只是一个mysql结果行而不是这个。

What I am looking for is just this ["role_id"]=> int(99) . 我正在寻找的只是这个["role_id"]=> int(99)

I am missing a function to directly get this, and I cant find it from the docs. 我缺少直接获取此功能的函数,但无法从文档中找到它。

$role_id->role_id gives me property not found. $role_id->role_id给我找不到财产。

You have items with get method. 您的项目带有get方法。 This of the reasons cannot access role_id. 由于这个原因,无法访问role_id。

You can use first instead of get method if you want to one record. 如果要一条记录,可以使用first而不是get方法。

and you can use dd() function. 您可以使用dd()函数。

$role_id = Role::select('role_id')
                ->where('type','Admin')
                ->first();
dd($role_id);

You can do this: 你可以这样做:

$role_id = Role::all(['role_id'])->where('type','Admin')->first(); or Role::where('type', 'Admin');

Instead of var_dump you can use dd($role_id) or var_dump($role_id), it dies and dumps at the same time .

Use ->first() instead of ->get() . 使用->first()代替->get() get() returns collection. get()返回collection。

This is the right way of doing it. 这是正确的方法。

$response = Role::select('role_id')
                       ->where('type','Admin')
                       ->first();
        $admin_id = $response->role_id; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM