我想在laravel5.7中从不同的表中获取单个数组中的数据

jfgube3f  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(333)

我是laravel的新手,正在从事一个项目,需要从laravel 5.7中的不同表中获取数据。假设我有3个表:
我需要从中获取数据的主表
二级表1
二级表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\u push函数和array\u merge等不同的方法将两个数组合并到一个数组中,但没有人成功。我不知道该怎么实现这个。
请让我知道,如果有任何信息在这个问题上丢失,以理解和回答问题。任何帮助都将不胜感激。提前谢谢。

vtwuwzda

vtwuwzda1#

您可以在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;
3b6akqbq

3b6akqbq2#

你能试试这个吗。。。希望有用

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;

相关问题