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:
- Main Table from which I need to fetch the data
- Secondary Table 1
- 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.