How to store values into array from database in laravel?

Member

by dana , in category: PHP Frameworks , 5 months ago

How to store values into array from database in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 5 months ago

@dana 

To store values from a database into an array in Laravel, you can use the pluck method provided by Eloquent. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Retrieve values from the database
$values = DB::table('your_table')->pluck('your_column');

// Convert the collection to an array
$array = $values->toArray();

// Access values in the array
foreach ($array as $value) {
    echo $value;
}


In this example, we first use the pluck method to retrieve the values from a specific column in the database table. Then, we convert the collection into an array using the toArray method. Finally, we can access and work with the values in the array as needed.