troubleshooting Question

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

Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil asked on
LaravelPHP
7 Comments1 Solution66 ViewsLast Modified:
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(); 


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

But results in "null".

Is that correctly to use Eloquent this way?

Thanks in advance
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros