【问题标题】:Filter an array list result from query builder in model in Laravel在 Laravel 的模型中过滤来自查询构建器的数组列表结果
【发布时间】:2020-12-21 11:43:05
【问题描述】:

我的模型中有一个查询函数来获取列表用户并将数组数据返回到我的存储库类:

模型.php:

public function getUser()
{
    return $this->select("{$this->table}.'*'")
                ->get();
}

以下是数据示例:

[
    [
        "user_id":1,
        "fullname":"amdv",
        "is_active":0
    ],
    [
        "user_id":2,
        "fullname":"abc",
        "is_active":1
    ],
    [
        "user_id":3,
        "fullname":"zyz",
        "is_active":1
    ]
]

现在我想检查 is_active = 1,它继续检查其他查询中的其他条件的用户

在我的模型中,我有另一个查询函数,参数是用户 ID

模型.php:

public function checkUserAvailable($userId)
{
    return $this->select("{$this->table}.'*'")
                ->join('other_table', 'join-condition')
                ->where('user_id', $userId)
                ->get();
}

但是这个函数也返回数据,我不知道如何检查。 非常感谢!

【问题讨论】:

  • 没有得到你想要的。模型中的另一个查询函数是什么 - 您可以发布该函数的代码吗?您想将此数组中的 user_id 传递给其他函数吗?
  • 您想“删除用户”表单数据库? “这是不可用的”..修改您的“我的模型中的查询函数以获取列表用户”以仅获取用户 ID,然后将列表提供给第二个函数。
  • 你真的有一个数组还是有一个模型集合?

标签: laravel query-builder


【解决方案1】:
$data = [
    [
        "user_id":1,
        "fullname":"amdv",
        "is_active":1
    ],
    [
        "user_id":2,
        "fullname":"abc",
        "is_active":1
    ],
    [
        "user_id":3,
        "fullname":"zyz",
        "is_active":1
    ]
];

$filtered = collect($data)->filter(function($user) {
    return User::someMethodYouMentioned($user['user_id']);
});

【讨论】:

    【解决方案2】:

    据我了解,您想检查第一个函数返回的数组中的数据,如果是is_active = 1,那么您想通过模型中的第二个函数检查用户是否可用

    //Get the user_id of records where is_active === 1
    $available = collect($data)
        ->reject(fn($user) => $user['is_active'] !== 1)
        ->pluck('user_id')
        ->map(fn($id) => User::checkUserAvailable($id));
    

    为此,checkUserAvailable 需要是一个静态函数。

    但是查看这两个函数 - 问题出现了,为什么需要运行多个数据库查询来检查通过第二个函数可用的用户。

    您可以在第二个函数中修改查询以检查is_active

    
    public static function checkUserAvailable()
    {
        return static::query()
            ->join('other_table', 'join-condition')
            ->where('is_active', 1)
            ->get();
    }
    

    或者,如果您仍然想首先获取所有用户的列表作为数组,然后检查可用性,您可以将 checkUserAvailable 定义为模型上的范围

    public function scopeCheckUserAvailable($query, $id)
    {
        $ids = Arr::wrap($id);
    
        return $query->join('other_table', 'join-condition')
            ->whereIn('user_id', $ids);
    }
    

    然后你可以从getUser()函数中获取数据并像这样进行第二次检查

    //Get the user_id of records where is_active === 1
    $checkIds = collect($data)
        ->reject(fn($user) => $user['is_active'] !== 1)
        ->pluck('user_id')
        ->all();
    
    $availableUsers = User::checkAvailableUsers($checkIds)->get();
    

    【讨论】:

      【解决方案3】:

      如果你想过滤掉那些不活跃的你可以使用array_filter回调:

      $filtered = array_filter($array, fn ($i) => $i['is_active']);
      

      如果您想使用这些值来调用函数然后获得这些结果,您可以使用array_map

      $res = array_map([$this, 'checkUserAvailable'], array_column($filtered, 'user_id'));
      

      如果您愿意,也可以使用 Collection 方法执行此操作:

      collect($array)->where('is_active', 1)
          ->pluck('user_id')
          ->map([$this, 'checkUserAvailable'])
      

      不过,我不确定除了 is_active 是真的之外,您还需要检查什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        相关资源
        最近更新 更多