【问题标题】:Retrieve the minimum in Laravel relationship检索 Laravel 关系中的最小值
【发布时间】:2016-02-25 11:29:41
【问题描述】:

我正在开发类似于 CRM 应用(客户关系管理)的应用

我有以下代码获取分配给用户的$ms 数组

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

现在我有 $ms 的数组,所以我将按以下方式循环它

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

上面的代码运行良好,但我需要先获取最小值,例如

在第一个循环中通知的计数 3 在第二个计数中是 2

但我首先需要最小值所以当return $ms[$x]-&gt;notifications; 在我们的例子中,我需要返回带有最小计数的数组 2

有什么办法可以做到吗?

感谢您的宝贵时间。

【问题讨论】:

  • 您只需要通知数量最少的行还是需要按通知排序的所有项目?
  • 不,我需要所有项目才能为每个客户获得最少的通知数

标签: php arrays laravel laravel-4 laravel-5


【解决方案1】:

我认为您尝试通过 notifications 关系计数对 MaintenanceService 项目进行排序?

// 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()
    // sort all by the notifications count
    ->sortBy(function($ms) {
        // assuming 'notifications' is a relationship within the MaintenanceService model
        return count($ms->notifications);
    });

查看更多关于集合 heresortBy 方法。

编辑: 当然你需要sortByDesc HIGH => LOW 和sortBy LOW => HIGH。

【讨论】:

  • @JustUser 请注意,这会遇到“N+1”问题。对于每个 MaintenanceService 对象,您将运行一个单独的查询来获取该对象的通知。您可以使用eager loading 缓解此问题。将with() 方法添加到您的查询中:MantenanceService::with('notifications')-&gt;join...
【解决方案2】:

要获取列中值最低的元素,您可以使用 collections

上提供的 min() 方法
<?php

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

$lowest = $ms->min('notifications');

关于收藏的更多信息: http://laravel.com/docs/5.1/collections

所有可用的收集方法: http://laravel.com/docs/5.1/collections#available-methods


旁注

要遍历 eloquent 结果,您只需在其上执行 foreach,因为它在需要时将其视为数组:

<?php

foreach($ms as $element) {
    $notifications = $element->notifications;
}

【讨论】:

  • 我认为有误解我不需要最小列如果我想要最小列我可以这样做$ms[$x]-&gt;notifications-&gt;min(); 第二件事notifications 是雄辩的方法而不是列我需要获取 for 循环中notifications 关系的最小计数作为循环的开始
猜你喜欢
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 2022-01-10
  • 2020-08-23
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多