Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag for Brazil asked on

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

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
LaravelPHP

Avatar of undefined
Last Comment
Eduardo Fuerte

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Eduardo Fuerte

ASKER
Hi Chris


So did I

That results in:

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

Open in new window

Chris Stanyon

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 Fuerte

ASKER
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Eduardo Fuerte

ASKER
Thank you for the solution!
Chris Stanyon

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 Fuerte

ASKER
Chris

Clarified!
Very didatic explanation!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.