lundi 23 novembre 2015

Retrieve the minimum in Laravel relationship

I working on App similar to CRM App(Customer Relation Management)

I have the following code that get array of $ms that assigned to user

   //check if contract end or not and active or inactive
  // and first_date_for_service < now()
    $ms= MaintenanceService::join('cus_contract','cus_contract.id','=','maintenance_service.contract_id')
        ->where('cus_contract.status','=','active')
        ->where('cus_contract.contract_end_at','>',Carbon::now())
        ->where('maintenance_service.user_id', Auth::user()->id)
        ->where('maintenance_service.first_date_for_service','<', Carbon::now())
        ->get();

now i have array of $ms so i will loop over it as following

  for($x = 0; $x< count($ms);$x++) {
   // here i get all notifications for $ms[$x] in each loop using Eloquent
      return $ms[$x]->notifications;
    }

The above code working well,but i need fetch the minimum firstly ,for example

in first loop the count of notifications 3 and in the second count was 2.

but i need the min in first so when return $ms[$x]->notifications; i need return the array that carries the min count in our case 2

Is there any way to achieve that ?

Thanks for your time.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire