Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Laravel Query builder to Eloquent builder

Because get() function of Query builder returns an array while I need A collection, is there a way to convert Laravel Query Builder to Eloquent Builder?

$query_builder = DB::table('table1');

// different than
$eloquent_builder = Table1Model::select()
like image 704
Omid Avatar asked Oct 18 '25 13:10

Omid


1 Answers

Laravel ships with a collect helper to convert an array to a collection:

$collection = collect(DB::table('table1')->get());

There's a proposal on Github to have the next version of Laravel return collection instances from the query builder's get method.

like image 199
Joseph Silber Avatar answered Oct 21 '25 08:10

Joseph Silber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!