【问题标题】:PHP Laravel Multiple returnsPHP Laravel 多次返回
【发布时间】:2019-03-20 18:04:36
【问题描述】:

你好我有这个功能

//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 时得到的输出

[{"日期":"2019-02-10","请求":1},{"日期":"2019-02-12","请求":1},{"日期":" 2019-02-14","恳求":1}]

我怎样才能让它像

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

非常感谢

【问题讨论】:

  • 最有效的方法高度依赖于您的数据库架构。您能否提供您用于检索三个不同潜在客户数量的完整代码。
  • 非常感谢您的评论我添加了完整的代码
  • 我认为您可以使用union 做到这一点,请参阅此问题stackoverflow.com/questions/54659587/…

标签: php mysql laravel


【解决方案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
);

这两种解决方案都没有经过全面测试。

【讨论】:

  • 哇,非常感谢您的回答,它工作得很好,结果我得到了{"adminleads":[{"date":"2019-02-02","aleads":1}],"managerleads":[{"date":"2019-02-01","mleads":1}],"personalleads":[{"date":"2019-02-02","pleads":2},{"date":"2019-02-03","pleads":1},{"date":"2019-02-04","pleads":1},{"date":"2019-02-05","pleads":1},{"date":"2019-02-06","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}],所以我可以轻松解析它们
猜你喜欢
  • 2020-06-21
  • 2021-03-02
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 2015-08-20
  • 2015-08-22
相关资源
最近更新 更多