How to return multiple array in a function using helper in Laravel

Posted By : Prahalad Singh Ranawat | 29-Aug-2022

Step: 1 - Create a helper file (app/Helper.php) this file will call using autoload file for all controllers.
 

<?php

use App\Models\User;

use Illuminate\Support\Facades\Auth;

function merge_2_array($array1,$array2){

$result = array();

foreach($array1 as $k => $v){

$result[$k] = array_merge($array1[$k],$array2[$k]);

}

return($result);

}

 

 

Step: 2 - Call merge function in controller. 

Here we are calling array() method from the helper to merge all arrays in one variable and on the next step will render each array data separately to view.
 

public function chartAvgEmailData(){

$data_email_avg = [];

$data_recipients_avg = [];

$data_char_avg = [];

$data_email_time = [];

$data_email_cost = [];

$data_email_co2e = [];

$months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

for ($k = 1; $k <= 12; $k++) {

$data_email_avg[$k]['month'] = $months[$k - 1];

if (!empty($datacount[$k])) {

$data_email_avg[$k]['value'] = "your data variable add here";

}else {

$data_email_avg[$k]['value'] = 0;

}

}

//avg recipients

for ($k = 1; $k <= 12; $k++) {

$data_recipients_avg[$k]['month'] = $months[$k - 1];

if (!empty($datacount[$k])) {

$data_recipients_avg[$k]['value'] = $datacount[$k]['avg_recipients'];

}else {

$data_recipients_avg[$k]['value'] = 0;

}

}

//avg character

for ($k = 1; $k <= 12; $k++) {

$data_char_avg[$k]['month'] = $months[$k - 1];

if (!empty($datacount[$k])) {

$data_char_avg[$k]['value'] = "your data variable add here";

}else {

$data_char_avg[$k]['value'] = 0;

}

}

//time

for ($k = 1; $k <= 12; $k++) {

$data_email_time[$k]['month'] = $months[$k - 1];

if (!empty($datacount[$k])) {

$data_email_time[$k]['value'] = "your data variable add here";

}else {

$data_email_time[$k]['value']= 0;

}

}

//cost

for ($k = 1; $k <= 12; $k++) {

$data_email_cost[$k]['month'] = $months[$k - 1];

if (!empty($datacount[$k])) {

$data_email_cost[$k]['value'] = "your data variable add here";

}else {

$data_email_cost[$k]['value'] = 0;

}

}

//co2e

for ($k = 1; $k <= 12; $k++) {

$data_email_co2e[$k]['month'] = $months[$k - 1];

if (!empty($datacount[$k])) {

$data_email_co2e[$k]['value'] = "your data variable add here";

}else {

$data_email_co2e[$k]['value'] = 0;

}

}

$all_data_dashboard = array($data_email_avg,$data_recipients_avg,$data_char_avg,$data_email_time,$data_email_cost,$data_email_co2e);

return ($all_data_dashboard);

}

 

Step: 3 - call method that has all merged data.

 

public function dashboard(){

//call here all merge array and store in each variable to render

$all_data_dashboard = $this->chartAvgEmailData();

 

$data_dash_email_avg = $all_data_dashboard[0];

$data_dash_recipients_avg = $all_data_dashboard[1];

$data_dash_char_avg = $all_data_dashboard[2];

$data_dash_email_time = $all_data_dashboard[3];

$data_dash_email_cost = $all_data_dashboard[4];

$data_dash_email_co2e = $all_data_dashboard[5];

return view('dashboard.dashboard',compact('data_dash_email_avg','data_dash_recipients_avg','data_dash_char_avg','data_dash_email_time','data_dash_email_cost','data_dash_email_co2e'));

}

 

conclusion:  we have created a helper method to merge array data and used a method where to merge multiple array data. then we have rendered each array of data separately to send to view for each chart. This process will help you when you need to manage multiple chart data to render on a single view. 


Contact us at  [email protected] , our experts will get back to you.
 

About Author

Author Image
Prahalad Singh Ranawat

Prahalad Singh Ranawat is a highly skilled backend developer with extensive experience in PHP, Laravel, Magento, Headless Magento, RESTful API, Node.js, and Vue.js. He also possesses knowledge in Shopify. Prahalad has a solid background in working with Postman, Swagger, Git, MySQL, MongoDB, and the LAMP stack. With his passion for learning and creativity, he is constantly exploring new technologies to enhance his skills. He has provided DevOps support and contributed his expertise to a range of projects, including Yumi Paws, OACustomer-Dashboard, Vlad Application, Information Sharing Website, Eating Disorder Intervention, TRO Platform, and SimplyNoted.

Request for Proposal

Name is required

Comment is required

Sending message..