Convert Laravel Query builder to Eloquent builder - laravel

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

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.

Related

get multiple docs by ID as ActiveRecord in Yii2 from ealstic search using mget()

I'm using the Yii2 class yii\elasticsearch\ActiveRecord to recieve data from elastic search. Usually methods in this class for getting data from elastic will return that data as ActiveRecord (AR) object. So it's easy, to create an activeDataProvider from that AR to fill that data into a listview, etc.
But: yii\elasticsearch\ActiveRecord::mget() does not return an AR-object. Instead it return an array of documents.
My questions:
1.) Is there a way to use the mget - feature / elastic multi get feature AND get the result as AR object?
OR
2.) Is there a way to bring that array of documents into an AR object to make ActiveDataProvider including listview working?
I found the following solution:
Runnig Yii2 mget with an list of the ID's I like to get, which returns an array of docs.
Creating an ActiveDataProvider using ArrayDataProvider Class and the mget-output from step before
Example Code:
use app\models\MyModel;
use yii\data\ArrayDataProvider;
$ids = ['123','456','789'];
$myModel = new MyModel(); # Data Model based on yii\mongodb\ActiveRecord;
$result = $myModel->mget($ids); # get documents from elastic where document id is in $ids
$dataProvider = new ArrayDataProvider([
'allModels' => $result
]); # creating ActiveDataProvider, which can be used in Listviews.

What is the meaning of Eloquent's Model::query()?

Can anyone please explain in detail what Eloquent's Model::query() means?
Any time you're querying a Model in Eloquent, you're using the Eloquent Query Builder. Eloquent models pass calls to the query builder using magic methods (__call, __callStatic). Model::query() returns an instance of this query builder.
Therefore, since where() and other query calls are passed to the query builder:
Model::where()->get();
Is the same as:
Model::query()->where()->get();
Where I've found myself using Model::query() in the past is when I need to instantiate a query and then build up conditions based on request variables.
$query = Model::query();
if ($request->color) {
$query->where('color', $request->color);
}

Eloquent find() with joins and eager loading

I want to retrieve one record via the time-tested method of this URL:
public/api/laptop/1
hitting this route:
Route::get('laptop/{id}', 'LaptopController#getLaptop');
then this controller method:
$laptop = Laptop::find($id)->addJoins()->selectListCols()->with('earmarks', 'movements')->get();
return $laptop;
Problem is this doesn't work (it returns every record). To make it work I have to do this:
$laptop = Laptop::where('laptops.id', $id)->addJoins()->selectListCols()->with('earmarks', 'movements')->get();
return $laptop;
But I'm just wondering why find() doesn't work? earmarks and movements are Many-To-One models, by the way.
find() is just a shortcut for where()->first() so it will return an object and Query Builder methods will not work with it:
User::find(1); // Will return User object with ID = 1.
That's why you need to use where(), which returns Query Builder object, so you can use with() and other builder methods to build your query.

Eloquent ORM query of type Model (not Collection)

I'm trying to get an object of type Model. I'm using Laravel 4.x with the Eloquent ORM.
If I do this, it doesn't work :
Liveshow::where('slug', $slug)->get();
And if I do this, it works and returns the Model object :
Liveshow::find(Liveshow::where('slug', $slug)->pluck('id'));
I'm wondering if there is a better way to go.
I need the Object to be of type Model because I'm doing this after :
$userLiveshow = new UserLiveshows;
$userLiveshow->user()->associate($user);
$userLiveshow->liveshow()->associate($liveshow);
$userLiveshow->started_at = Carbon::now();
$userLiveshow->watched = 0;
Thanks for your help.
John
If you expect only one model to match your query and therefore don't want a collection as result, just use first():
$liveshow = Liveshow::where('slug', $slug)->first();

About the laravel eloquent model

The laravel API document: Illuminate\Database\Eloquent\Builder::first() description that:
Model|Builder|null first(array $columns = array('*'))
Execute the query and get the first result.
Parameters
array $columns
Return Value
Model|Builder|null
I can't understand the mean of the return value, In which case it will return Model and In which case it will return Builder?
When you have used eloquent model for retrieving first record it return response in Model, and not sure about builder but when you retrieve records using builder it return builder object.
for example, consider we have states table and we are going to retrieve first record using two different method
1) Query Builder
$state_builder = DB::table("states")->first();
2) Eloquent Model
$state_eloquent = State::first();
you can check the difference between both response, and when no record found it will return null.

Resources