1

Im new to this Framework, i dont know how to optimize it using db::raw count and aliases and display it to my blade.php using @foreach

im trying to optimize my code, my goals is to count pallet_conditions and store it to my aliases, i dont want to count it one by one like what i did on this code

this is my code not optimize:

//computing the total rapairable
$repairable_total = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where('pallet_condition', '=', 1)
->count();
//REPAIRABLE

//computing the total good pallets
$good_total = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where('pallet_condition', '=', 0)
->count();
//GOOD

this is the code, what i wanted to learn. just to minimize, and use aliases

$result = DB::table('liip_psrm_items')
->select(DB::raw('COUNT(liip_psrm_items.pallet_condition = 0 ) AS condition_1',
                 'COUNT(liip_psrm_items.pallet_condition = 1 ) AS condition_2'))                      
                ->where('psrm_items_id', '=' , $psrm_maintenance->id)
                ->get();
1
  • 1
    Your Laravel code looks fine to me. What is your actual question? Commented Oct 26, 2018 at 5:41

4 Answers 4

4

To count at multiple condition I used this approach

 $lastMonthInvoices = Invoice::select(DB::raw("(COUNT(*)) as count"), DB::raw('SUM(total) as total'),'status')
        ->whereDate('created_at', '>', Carbon::now()->subMonth())
        ->groupBy('status')
        ->get();

i got the result with groupBy Status and in each group total number of records as count & also their sum as total

sample of one group

enter image description here

these two snaps are of one query result

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

Comments

3

You can't use single query for two different results, which has totally opposite conditions.

Case 1. You are trying to count the items where pallet_condition = 1;

Case 2. You are trying to count the items where pallet_condition = 0;

Now you want to merge these two cases into single query, which is impossible...

So, For these two cases, you have to use either separate queries ( what you did already )

or you can use single query to grab all the items and then use PHP to separate them.

Like:

$total_items = DB::table('liip_psrm_items')
   ->where('psrm_items_id', '=' , $psrm_maintenance->id)
   ->get();

$repairable_count = count(array_filter($total_items, function($item){
   return (bool)$item->pallet_condition;
}));

$good_count = count(array_filter($total_items, function($item){
   return !(bool)$item->pallet_condition; //just inverse of the above condition
}));

i hope this might help.

1 Comment

thank for the suggestion @jaskaran appriciate it. the above answer jonjie solve my problem
2

You can, first group by, then get count

Like :

DB::table('liip_psrm_items')
  ->groupBy('pallet_condition')
  ->select('pallet_condition', DB::raw('count(*) as total'))
  ->get();

Comments

-1

Try to pass a closure like so:

$results = DB::table('liip_psrm_items')
->where('psrm_items_id', '=' , $psrm_maintenance->id)
->where(function($query){
   $query->where('pallet_condition', 1)
      ->orWhere('pallet_condition', 0);
})->count();

2 Comments

@YasserCHENIK You can file a bug at github.com/laravel/laravel if you feel that where method is useless.
in other words : [ i will wake up tomorrow if you = "like it" or if you ="don't like it" ] is equivalent to [ i will wake up tomorrow ] because it's boolean with all possible conditions inside it, so your where statement is indeed useless my friend :l

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.