I have 2 tables. 1 namedtasks
that has the rows[id, event_id, title]
and the other table is namedtask_items
with the rows[id, task_id, description, ...]
.
I am trying to run a query to retrieve all tasks with anevent_id
of x, then all task_items that match theid
of any result from the previous query. I currently have 2 standalone queries, but don't know how to join them to work in-tandem. The current code that I have show alltask_items
associated with the event, but they aren't corresponding with each task. Task 0 in my database has 0task_items
, but my code is displaying thetask_items
for task 0 under task 1.
This is my query code:
$tasks = DB::table('tasks')->where('event_id', $request->route('id'))->get();
foreach($tasks as $task)
{
$taskItemID[] = $task->id;
}
$taskItems = DB::table('task_items')->whereIn('task_id', $taskItemID)->get();
This is my foreach loop:
@foreach($tasks as $task)
<div><b>Task # {{$task->id}}:</b> {{$task->title}}</div>
<div style="float:right;">mark as completed</div>
</div>
<div class="row" style="border: 1px solid #efefef;padding: 20px;">
@foreach($taskItems as $taskItem)
<li>{{$taskItem->description}}</li>
@endforeach
@endforeach
I had to tweak @Kamlesh Paul's code a bit, but this worked for me:
$tasksData = DB::table('tasks')->where('event_id', $request->route('id'))->get();
foreach ($tasksData as $task) {
$temp = $task;
$temp->task_items = DB::table('task_items')->where('task_id', $task->id)->get();
$tasks[] = $temp;
}
try somthing like this to create new child data
$tasksData = DB::table('tasks')->where('event_id', $request->route('id'))->get();
$tasks = collect();
foreach ($tasksData as $task) {
$temp = $task;
$temp->task_items = DB::table('task_items')->where('task_id', $task->id)->get();
$data[] = $temp;
}
return $tasks;
then you can get$tasks
with all the$task_items
to get alltask_item
you can$tasks[0]->task_items
you can use in your blade like this
@foreach($tasks as $task)
<div><b>Task # {{$task->id}}:</b> {{$task->title}}</div>
<div style="float:right;">mark as completed</div>
</div>
<div class="row" style="border: 1px solid #efefef;padding: 20px;">
@foreach($task->task_items as $taskItem)
<li>{{$taskItem->description}}</li>
@endforeach
@endforeach
NOTE i am not recommend this but at in your can you can use this
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.