2017-03-20 164 views
1

我是laravel中使用Laravel 5.3的新手。 我正在創建一個check()函數laravel模型用戶登錄 這裏我得到所有數據表格數據庫使用默認$this->all();這個返回我一個大multidymentional 數組。Laravel獲取屬性數據

Illuminate\Database\Eloquent\Collection Object 
(
    [items:protected] => Array 
     (
      [0] => App\wn_users Object 
       (
        [table:protected] => wn_users 
        [timestamps] => 
        [fillable:protected] => Array 
         (
          [0] => role_id 
          [1] => firstname 
          [2] => lastname 
          [3] => username 
          [4] => email 
          [5] => password 
          [6] => companyname 
          [7] => country_id 
          [8] => description 
          [9] => ip 
          [10] => update_date 
          [11] => status 
         ) 

        [connection:protected] => 
        [primaryKey:protected] => id 
        [keyType:protected] => int 
        [incrementing] => 1 
        [with:protected] => Array 
         (
         ) 

        [perPage:protected] => 15 
        [exists] => 1 
        [wasRecentlyCreated] => 
        [attributes:protected] => Array 
         (
          [user_id] => 1 
          [role_id] => 1 
          [firstname] => Aman kumar 
          [lastname] => -- 
          [username] => Aman kumar 
          [email] => [email protected] 
          [password] => e10adc3949ba59abbe56e057f20f883e 
          [companyname] => Imax 
          [country_id] => 123 
          [description] => Testing 
          [ip] => 192.168.1.1 
          [update_date] => 2017-03-20 
          [status] => 0 
          [created_at] => 
          [updated_at] => 
         ) 

        [original:protected] => Array 
         (
          [user_id] => 1 
          [role_id] => 1 
          [firstname] => Aman kumar 
          [lastname] => -- 
          [username] => Aman kumar 
          [email] => [email protected] 
          [password] => e10adc3949ba59abbe56e057f20f883e 
          [companyname] => Imax 
          [country_id] => 123 
          [description] => Testing 
          [ip] => 192.168.1.1 
          [update_date] => 2017-03-20 
          [status] => 0 
          [created_at] => 
          [updated_at] => 
         ) 

        [casts:protected] => Array 
         (
         ) 

        [dates:protected] => Array 
         (
         ) 

        [dateFormat:protected] => 
        [appends:protected] => Array 
         (
         ) 

        [events:protected] => Array 
         (
         ) 

        [observables:protected] => Array 
         (
         ) 

        [relations:protected] => Array 
         (
         ) 

        [touches:protected] => Array 
         (
         ) 

        [hidden:protected] => Array 
         (
         ) 

        [visible:protected] => Array 
         (
         ) 

        [guarded:protected] => Array 
         (
          [0] => * 
         ) 

       ) 

     ) 

) 

但我只想得到'attributes:protected'在laravel中形成整個數組。我已經嘗試

echo $data = $this->getAttributes()['firstname'];但這將返回錯誤

Undefined index: firstname 

請幫我解決我的問題提前

感謝您的幫助和時間。

回答

1

一個非常簡單的方法:

$arr = $this->all()->toArray(); 

var_dump($arr); // oh~ array data! 
0

所以,你必須具有屬性的集合。你可以訪問它們,就像$collection->first()->firstname或者,如果你想要做一些操作所有項目使用每個方法:

$collection = $collection->each(function ($item, $key) { 
    $item->firstname .= ' Smith'; 
});