PHP Laravel 多次返回

标签 php mysql laravel

你好,我有这个功能

//Get Total Number of Personal Leads By User ID & Requested week Through Each Day
//Get Total Number of Personal Leads By User ID & Requested week Through Each Day
$personalleads = \DB::table('leads') 
->where('owned_by_id', $id) // User ID
->where('lead_source_id', 7) // 7 = Personal Lead
->whereBetween('created_at', [$weeks[$week]]) // get week through GET request & Query the data
->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(*) as pleads'))
->groupBy('date')
->get(); // Get All Data

//Get Total Number of leads Created by Managers By User ID & Requested week Through Each Day
$managerleads = \DB::table('leads') 
->where('owned_by_id', $id) // User ID
->where('lead_source_id', 3) // 3 = Manager Lead
->whereBetween('created_at', [$weeks[$week]]) // get week through GET request & Query the data
->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(*) as mleads'))
->groupBy('date')
->get(); // Get All Data

//Get Total Number of leads Created by Admins By User ID & Requested week Through Each Day
$adminleads = \DB::table('leads') 
->where('owned_by_id', $id) // User ID
->where('lead_source_id', 4) // 4 = Admin Lead
->whereBetween('created_at', [$weeks[$week]]) // get week through GET request & Query the data
->select(\DB::raw('DATE(created_at) as date'), \DB::raw('count(*) as aleads'))
->groupBy('date')
->get(); // Get All Data

我想返回所有的数据 就像

return $adminleads+personalleads+managerleads;

我知道这是无效的,但我想立即显示所有数据 这是我仅返回 $personalleads 时得到的输出

[{"date":"2019-02-10","pleads":1},{"date":"2019-02-12","pleads":1},{"date":"2019-02-14","pleads":1}]

我怎样才能使它像

[{"date":"2019-02-10","pleads":1,"aleads":1,"mleads":1},{"date":"2019-02-12","pleads":1,"aleads":1,"mleads":1},{"date":"2019-02-14","pleads":1,"aleads":1,"mleads":1}]

非常感谢

最佳答案

您可以尝试连接这些值的两个选项:

  1. PHP 将循环结果并连接数据。
  2. 查询处理数据的连接(目前我无法理解)。

不知道哪一个更好。 PHP 看起来更容易,但是,取决于系统和大小,我的猜测是 mysql 可以更有效地处理这个问题。

PHP

$pLeads = ...
$mLeads = ...
$aLeads = ...

$allLeads = $pLeads->merge($mLeads)->merge($aLeads); // Maybe there is a shorter variant.
$leads = [];

$types = ['pleads', 'mleads', 'aleads'];

$allLeads->each(function ($lead) {
    // Make sure there is an array present with the date property.
    data_fill($leads, $lead->date, [
        'date' => $lead->date,
    ]);

    // At this point our array looks like:
    // ['2019-11-06' => ['date' => '2019-11-06']]

    // Get the the type of lead.
    $type = $types[array_search(array_keys($lead))];

    // Set the lead amount for the type.
    // Not sure if "$lead->date.$type" works. Make sure you end up with a key like 2019-11-06.pleads
    data_set($leads, "$lead->date.$type", $lead->$type);

    // At this point our array looks like:
    // ['2019-11-06' => ['date' => '2019-11-06', 'pleads' => 1]]
});

// Remove the keys from the array.
$leads = array_flatten($leads);

// At this point our array looks like:
// [['date' => '2019-11-06', 'pleads' => 1, 'mleads' => 2, 'aleads' => 3]]

查询

SELECT
    IFNULL(p.date, m.date)
    pleads,
    mleads
FROM (
    (
        SELECT
            DATE(created_at) as date,
            COUNT(*) pleads
        FROM
            leads
        WHERE
            ...
        GROUP BY
            date
    ) p
    RIGHT JOIN
    (
        SELECT
            DATE(created_at) as date,
            COUNT(*) mleads
        FROM
            leads
        WHERE
            ...
        GROUP BY
            date
    ) m ON p.date = m.date
);

两种解决方案尚未经过充分测试。

关于PHP Laravel 多次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55267456/

相关文章:

php - 如何从同一类中的对象调用基于变量的函数?

php - mysql 数据库未显示用户详细信息

php - MySQL 无法通过远程 php 脚本访问进行连接

php - 更新中间表 Laravel 关系

php - 如何配置 Laravel mail.php 使用内置邮件功能?

php - 如何更改此日期查询以以 mysql 典型格式以外的格式显示

php - YouTube数据API返回的结果多于maxResults

mysql - 使用 union 时如何组合具有不同列数的 SELECT 语句?

php - 如何在 mysql laravel 中存储 JSON

php - 需要帮助优化 laravel - php 代码