I want to get data in single array from different tables in Laravel 5.7

Viewed 289

I am new in laravel and working on a project where I need to get the data from different tables in Laravel 5.7 Suppose I have 3 tables:

  1. Main Table from which I need to fetch the data
  2. Secondary Table 1
  3. Secondary Table 2

Main Table Columns

id (auto increment primary key)
task_name (I have stored secondary table name here)
tid (task id)  
assigned_to
description

Here is my code

public function viewTasks(){
    $task_logs = TaskLog::orderBy('id','desc')->get();
    foreach($task_logs as $task_log)
    {
        $table_name = $task_log['task_name'];
        if(Schema::hasTable($table_name))
        {
            $tasks[] = DB::table($table_name)->where('id', $task_log->tid)->first();
        }
    }

    return $tasks;

And here is the output:

[
  {
    "id": 220,
    "uId": 324,
    "document_name": "Photo Id",
    "document_image": "image1.jpg",
    "created_at": "2018-12-30 09:56:24",
    "updated_at": "2018-12-30 09:56:24",
    "status": 1,
  },
  {
    "id": 114,
    "uId": 382,
    "makeModel": "Motorola 501",
    "PhoneTitle": "New launched",
    "price": "500",
    "dealerName": "",
    "created_at": "2018-12-30 09:56:24",
    "updated_at": "2018-12-30 09:56:24",
    "status": 1,
  }
]

Output what I need:

[
  {
    "id": 220,
    "uId": 324,
    "document_name": "Photo Id",
    "document_image": "image1.jpg",
    "created_at": "2018-12-30 09:56:24",
    "updated_at": "2018-12-30 09:56:24",
    "status": 1,
    "task_name": "documents",
    "assigned to": 3,
    "Description": "Description here",
  },
  {
    "id": 114,
    "uId": 382,
    "makeModel": "Motorola 501",
    "PhoneTitle": "New launched",
    "price": "500",
    "dealerName": "",
    "created_at": "2018-12-30 09:56:24",
    "updated_at": "2018-12-30 09:56:24",
    "status": 1,
    "task_name": "wishlists",
    "assigned to": 2,
    "Description": "Description here"
  }
]

I have tried different ways using array_push function and array_merge etc to merge two arrays in a single array but no one worked. I don't know how can I implement this.

Please let me know if any information is missing in this question to understand and answering the question. Any help would be greatly appreciated. Thanks in advance.

2 Answers

you can merge different objects in PHP, In this case you have to use put the variables in the single array in foreach and you will get the required format of data.

public function viewTasks(){
$array = [];
    $task_logs = TaskLog::orderBy('id','desc')->get();
    foreach($task_logs as $task_log)
    {
        $table_name = $task_log['task_name'];
        if(Schema::hasTable($table_name))
        {
            $tasks[] = DB::table($table_name)->where('id', $task_log->tid)->get();
        $array[] = array_merge($tasks,$task_log);
        }
    }    
    return $array;

Can you try this... hope it work

public function viewTasks(){
        $i = 0;
        $task_logs = TaskLog::orderBy('id','desc')->get();
        foreach($task_logs as $task_log)
        {   
            $table_name = $task_log['task_name'];
            if(Schema::hasTable($table_name)){                
                $tasks[$i] = DB::table($table_name)->where('id', $task_log->tid)->first();
                $tasks[$i]['task_name'] = $task_log['task_name'];
                $tasks[$i]['assigned_to'] = $task_log['assigned_to'];
                $tasks[$i]['description'] = $task_log['description'];
                $i++;
            }
        }

        return $tasks;
Related