【问题标题】:Compare two columns of a collection in laravel?比较laravel中集合的两列?
【发布时间】:2021-03-12 05:48:24
【问题描述】:

我想比较一个集合的两列。我不想在数据库级别执行此操作,仅在集合级别执行此操作。

我有一个这样的集合(为了便于阅读,我以数组模型返回它)

   array:3 [
  0 => array:5 [
    "total_amount" => 200000.0

    "admin_max_amount" => "200000000"


  ]
  1 => array:5 [
    "total_amount" => 100000.0

    "admin_max_amount" => "200000000"


  ]
  2 => array:5 [
    "total_amount" => 100000.0

    "admin_max_amount" => "0"

  ]
]

我想在 total_amount > admin_max_amount 的地方获得第一个。

【问题讨论】:

    标签: laravel collections eloquent laravel-7


    【解决方案1】:

    这是一种更“Laravel”的方法:

    $items = collect([
        [
            "total_amount" => 200000.0,
    
            "admin_max_amount" => "200000000",
        ],
        [
            "total_amount"     => 100000.0,
            "admin_max_amount" => "200000000",
        ],
        [
            "total_amount"     => 100000.0,
            "admin_max_amount" => "0",
        ],
    ]);
    $result = $items->first(function ($item)
    {
        return (float)$item['total_amount'] > (float)$item['admin_max_amount'];
    });
    dump($result);
    

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多