Answer the question
In order to leave comments, you need to log in
How to create a request in Laravel?
Good afternoon, there are 2 tables p
1. - tasks
- id
- name
2. comments
- id
- task_id
- comments
I need to add the number of comments to the first table, now I'm doing this, but I don't know how to calculate, tell me.
DB::table('tasks')
->leftJoin('comments','comments.task_id', '=', 'tasks.id')
->select('tasks.*','comments.id as comments_id' )
->groupBy('tasks.id')
->get();
Answer the question
In order to leave comments, you need to log in
Is it important to make a query constructor?
When using Eloquent, I would do this
. In the Task model, we write the connection with the Comments model
public function comments()
{
return $this->hasMany('App\Comments');
}
$count = 0;
foreach(Task::All() as $task)
{
$count += $task->comments()->count();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question