Home > OS >  How to store values into array from Database in laravel?
How to store values into array from Database in laravel?

Time:12-21

I am new to the laravel ,i write one database query which will fetch values from the database and stored into the database it's storing in the form of object but i need to store into the form of array, please help me to achieve this thing.


if(in_array('super-user',$types)){
   //my logic
}

My expectation is

$types=['admin','super-user'];

CodePudding user response:

In this particular case you are not looking to make any query return an array instead of objects you are looking to "pluck" a particular columns values:

$types = DB::table('status')->pluck('name')->all();

pluck gets the list of column values and the all method call returns the underlying array from the Collection instance returned from pluck.

You could also continue to use the Collection to do what you need instead of using an array:

$types = DB::table('status')->pluck('name');

if ($types->contains('super-user')) {
    ...
}

Laravel 8.x Docs - Queries - Retrieving a List of Column Values pluck

Laravel 8.x Docs - Collections - Available Methods - all

Laravel 8.x Docs - Collections - Available Methods - contains

  • Related