4

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()
1

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As in laravel 5.4 and 5.5 Query Builder (DB::table) returns collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.