我想从Laravel 5.7中的不同表中获取单个数组中的数据

时间:2018-12-30 14:07:42

标签: mysql laravel-query-builder laravel-5.7

我是laravel的新手,正在从事一个项目,该项目需要从Laravel 5.7的不同表中获取数据 假设我有3张桌子:

  1. 我需要从中获取数据的主表
  2. 次级表1
  3. 次级表2

主表列

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

这是我的代码

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;

这是输出:

[
  {
    "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,
  }
]

输出我需要的东西

[
  {
    "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"
  }
]

我尝试使用array_push函数和array_merge等方法以不同的方式将两个数组合并到一个数组中,但没有一个起作用。我不知道该怎么实现。

请让我知道此问题中是否缺少任何信息,以了解并回答该问题。任何帮助将不胜感激。提前致谢。

2 个答案:

答案 0 :(得分:1)

您可以在PHP中合并不同的对象,在这种情况下,您必须将变量放在foreach中的单个数组中,您将获得所需的数据格式。

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;

答案 1 :(得分:0)

您可以尝试一下...希望它能起作用

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;