我是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等不同的方法将两个数组合并到一个数组中,但没有人成功。我不知道该怎么实现这个。
请让我知道,如果有任何信息在这个问题上丢失,以理解和回答问题。任何帮助都将不胜感激。提前谢谢。
2条答案
按热度按时间vtwuwzda1#
您可以在php中合并不同的对象,在这种情况下,您必须使用将变量放入foreach中的单个数组中,这样您将获得所需的数据格式。
3b6akqbq2#
你能试试这个吗。。。希望有用