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

92
December 31, 2018, at 12:10 PM

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.

Answer 1

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;
Rent Charter Buses Company
READ ALSO
How do I insert spatial data into a table contains road column

How do I insert spatial data into a table contains road column

I am getting this error message while I am trying to insert my data into line table, using coordinates for points between road segments

135
MYSQL ORDER BY strtotime(column) ASC possible?

MYSQL ORDER BY strtotime(column) ASC possible?

I have a column with varchars like 27

169
MySQL: SUM of two COUNT (by distinct value)

MySQL: SUM of two COUNT (by distinct value)

My MySQL-table saves citationsEach row is one citation, as in:

111
Implement MySQL replication cause high connections

Implement MySQL replication cause high connections

I've tried to implement replication on MySQL 56 DB

86