We help IT Professionals succeed at work.

Could you explain how an Laravel's Eloquent result could be used to operate another eloquent query?

Eduardo Fuerte
on
Hi Experts

Could you explain how an Laravel's Eloquent result could be used to operate another eloquent query?


This query:

$metasMensais = Distribuidormeta::

		  leftjoin('periodos', 'periodos.id', 'distribuidormetas.periodo_id')
		  
		  ->leftjoin('meses', 'periodos.mes', 'meses.mes')
		
		  ->where(function($q) use ($distribuidor_id, $distribuidorespermitidos_id){
			
			if($distribuidorespermitidos_id != null && count($distribuidorespermitidos_id) > 0){
			  $q->whereIn('distribuidor_id', $distribuidorespermitidos_id);
			}

			if($distribuidor_id != null && $distribuidor_id > 0){
			  $q->where('distribuidor_id', \DB::raw($distribuidor_id));
			}
			
		  })
		  ->where('periodos.mes', '>', \DB::raw(1))
		  ->where('periodos.id', \DB::raw($mes_atual))
		  ->groupBy('distribuidormetas.periodo_id')
		  ->select(DB::raw('sum(valor) as metaMensal'), 'periodo_id')  
		  ->get(); 

Open in new window



Results in:

[{"metaMensal":"139747.6600","periodo_id":2}]



After an Eloquent is aplied on the above result
$metaMes = $metasMensais
			->where('periodo_id', \DB::raw(2))
			->first();

Open in new window


But results in "null".

Is that correctly to use Eloquent this way?

Thanks in advance
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Hey Eduardo,

Try dropping the get() from the first call:

This will give you a query builder that you can then add to:

$metasMensais = Distribuidormeta::
    leftjoin('periodos', 'periodos.id', 'distribuidormetas.periodo_id')
    ...
    ->select(DB::raw('sum(valor) as metaMensal'), 'periodo_id');

$metaMes = $metasMensais
    ->where('periodo_id', \DB::raw(2))
    ->first();

Open in new window

Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi Chris


So did I

That results in:

{"metaMensal":"13328188.0700","periodo_id":2}

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Hey Eduardo,

That looks like the correct result to me - what else are you expecting?

In your original post, you say the result of the first query is this:

[{"metaMensal":"139747.6600","periodo_id":2}]

That's an array containing 1 record.

If you then call the following on it:

$metaMes = $metasMensais
    ->where('periodo_id', \DB::raw(2))
    ->first();

Then it's going to pull the first record from that array where periodo_id = 2. As there's only one record, and the periodo_id = 2, it's just going to return that single record:

{"metaMensal":"139747.6600","periodo_id":2}
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi Chris

Sorry!

Your reply is perfectly correct and solved my issue.

My last post wasn't complete before I sent it.

I was just thinking about the 1st query, without ->get() produces an object where I could apply the 2nd query and inadvertently sent it.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Thank you for the solution!
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
No worries Eduardo,

Glad I could help

When you're dealing with Eloquent, you're dealing with a QueryBuilder. This means that you can keep on adding methods to adjust the query (where(), limit(), etc). You carry on with a QueryBuilder instance until you call the get() method, or one of the other equivalents (first() etc) - at this point, the QueryBuilder sends the actualy SQL query to the Database and the results are returned. Once you've sent the query, you no longer have the QueryBuilder so you can't then add more methods to it, which is why we add to it before calling get().

Think of the QueryBuilder as a way to prepare your query, and the get() method etc as a way to send the query.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Chris

Clarified!
Very didatic explanation!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.