本文介紹了Eloquent 只獲取一列作為數(shù)組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!
問題描述
如何使用eloquent在laravel 5.2中只獲取一列作為一維數(shù)組?
How to get only one column as one dimentional array in laravel 5.2 using eloquent?
我已經(jīng)嘗試過:
$array = Word_relation::select('word_two')->where('word_one', $word_id)->get()->toArray();
但是這個給它作為 2 維數(shù)組,如:
but this one gives it as 2 dimentional array like:
array(2) {
[0]=>
array(1) {
["word_one"]=>
int(2)
}
[1]=>
array(1) {
["word_one"]=>
int(3)
}
}
但我想得到它:
array(2) {
[0]=>2
[1]=>3
}
推薦答案
您可以使用 采摘
方法:
You can use the pluck
method:
Word_relation::where('word_one', $word_id)->pluck('word_two')->toArray();
有關(guān)可用于集合的方法的更多信息,您可以查看 Laravel 文檔.
For more info on what methods are available for using with collection, you can you can check out the Laravel Documentation.
這篇關(guān)于Eloquent 只獲取一列作為數(shù)組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!