我是 Laravel 新手,使用 Laravel 5.3。
我正在 laravel 模型中创建一个 check()
函数用于用户登录
在这里,我使用默认的 $this->all();
获取数据库中的所有数据,这会返回一个大的多维数据
数组。
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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e8e4e8e7a7e0e4e8f1fdeceae1e7e6e5e6eee0ecfac9eee4e8e0e5a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a>
[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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d1ddd1de9ed9ddd1c8c4d5d3d8dedfdcdfd7d9d5c3f0d7ddd1d9dc9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>
[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] => *
)
)
)
)
但我只想在 laravel 中获取 'attributes:protected'
形成整个数组。我已经尝试过
echo $data = $this->getAttributes()['firstname'];
但这会返回错误
Undefined index: firstname
请帮我解决问题
提前感谢您的帮助和时间。
最佳答案
一个非常简单的方法:
$arr = $this->all()->toArray();
var_dump($arr); // oh~ array data!
关于php - Laravel 获取属性数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42897045/