不能从get()函数laravel Eloquent中获得排序整洁的结果 [英] don't get a sorted and clean result from get() function laravel Eloquent
本文介绍了不能从get()函数laravel Eloquent中获得排序整洁的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码不能给我正确的查询结果.
The code below doesn't give me the correct result from the query.
$studentMeta = StudentMeta::where([
'meta_key' => 'brother_phone',
'students_id' => 9
])->get();
print_r($studentMeta);
我得到以下结果:
但是,我期望这样的事情:
However, I expect something like this:
$studentMeta => Array
(
[id] => 49
[meta_key] => brother_phone
[meta_value] => 7926161024
[created] => 2019-04-17 00:10:03
[students_id] => 9
)
有什么想法吗?
推荐答案
就是这样,因为您正在获取模型实例的集合(例如,因为您使用的是get()
而不是first()
).
That's it because you are getting a collection of Model instances (because you are using get()
instead of first()
for example).
如果要将其返回到视图,Laravel将在内部调用->toArray()
方法.
If you want to return it to the view, Laravel will call the ->toArray()
method under the hood.
尝试执行此操作以查看您的预期结果:
Try doind this to see your expected result:
$studentMeta = StudentMeta
::where(['meta_key' => 'brother_phone', 'students_id'=> 9])
->get();
dd($studentMeta->toArray());
检查文档的此部分:雄辩的序列化
这篇关于不能从get()函数laravel Eloquent中获得排序整洁的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文