How to get only one column as one dimentional array in laravel 5.2 using eloquent?
I have tried:
$array = Word_relation::select('word_two')->where('word_one', $word_id)->get()->toArray();
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)
}
}
but I want to get it as:
array(2) {
[0]=>2
[1]=>3
}
I think you can achieve it by using the below code
Model::get(['ColumnName'])->toArray();
You can use the
pluck
method:For more info on what methods are available for using with collection, you can you can check out the Laravel Documentation.
I came across this question and thought I would clarify that the lists() method of a eloquent builder object was depreciated in Laravel 5.2 and replaced with pluck().
These methods can also be called on a Collection for example
If you receive multiple entries the correct method is called lists.
That can be done in short as:
where model is the Model such as
User
model & column as column name likeid
if you do
& of course you can have any other clauses like
where
clause before pluck