Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Laravel多重返回_Php_Mysql_Laravel - Fatal编程技术网

PHP Laravel多重返回

PHP Laravel多重返回,php,mysql,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)

你好,我有这个功能

//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
我想返回所有的数据 像

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

[{“日期”:“2019-02-10”,“诉状”:1},{“日期”:“2019-02-12”,“诉状”:1},{“日期”:“2019-02-14”,“诉状”:1}]

我怎样才能做得像这样

[{“日期”:“2019-02-10”,“答辩状”:1,“答辩状”:1,“答辩状”:1},{“日期”:“2019-02-12”,“答辩状”:1,“答辩状”:1,“答辩状”:1},{“日期”:“2019-02-14”,“答辩状”:1,“答辩状”:1,“答辩状”:1}]


非常感谢

您可以尝试加入以下两个选项:

  • PHP将循环结果并连接数据
  • 这些查询处理数据的连接(目前我无法理解)
  • 不知道哪一个更好。PHP看起来容易得多,但取决于系统和大小,我猜mysql可以更有效地处理这个问题

    PHP

    质疑


    这两种解决方案尚未完全测试。

    您可以尝试加入以下两个选项:

  • PHP将循环结果并连接数据
  • 这些查询处理数据的连接(目前我无法理解)
  • 不知道哪一个更好。PHP看起来容易得多,但取决于系统和大小,我猜mysql可以更有效地处理这个问题

    PHP

    质疑


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

    最有效的方法在很大程度上取决于数据库的体系结构。您可以提供您用于检索三种不同Lead计数的完整代码吗?非常感谢您的评论我添加了完整代码我认为您可以使用
    union
    ,查看此问题最有效的方法取决于数据库的体系结构。您是否可以提供您用于检索三个不同Lead计数的完整代码。非常感谢您的评论我添加了完整代码我认为您可以使用
    union
    ,查看此问题哇,非常感谢您的回答它工作非常好,我得到
    {“adminleads”:[{“date”:“2019-02-02”,“aleads”:1}],“managerleads”:[{“日期”:“2019-02-01”,“mleads”:1}],“个人线索”:[{“日期”:“2019-02-02”,“辩护”:2},{“日期”:“2019-02-03”,“辩护”:1},{“日期”:“2019-02-04”,“辩护”:1},{“日期”:“2019-02-05”,“辩护”:1},{“日期”:“2019-02-06”,“辩护”:1}
    ,因此,我想要的是
    ,{“日期”:“2019-02-10”,“辩护”:1},{:“2019-02-12”,“恳求”:1,“aleads”:1,“mleads”:1},{“日期”:“2019-02-14”,“恳求”:1,“aleads”:1}]
    所以我可以很容易地解析它们。非常感谢你的回答,它工作得非常好,我得到了
    {“adminleads”:[{“日期”:“2019-02-02”,“aleads”:1}],“managerleads”:[{“日期”:“2019-02-01”,“mleads”:1}],“个人Leads”:[{“日期”:“2019-02-02”,“辩护”:2},{“日期”:“2019-02-03”,“辩护”:1},{“日期”:“2019-02-04”,“辩护”:1},{“日期”:“2019-02-05”,“辩护”:1},{“日期”:“2019-02-06”,“辩护”:1}
    ,因此,我想要的是
    [{“日期”:“2019-02-10”,“辩护”:1,“aleads”:1,“mleads”:1},{“日期”:“2019-02-12”,“辩护”:1,“aleads”:1},{“日期”:请求“:1,“aleads”:1,“mleads”:1}]
    这样我就可以轻松解析它们了
    return $adminleads+personalleads+managerleads;
    
    $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
    );