Hi, please tell me in Laravel always recommend using Eloquent ORM and try to use less QUeryBuilder and / or direct SQL query inserts. Allegedly, this is the rule of good tone developer in Laravel.

Question: How can I replace

SELECT `tasks`.`title`, `tasks`.`link`, `users`.`name` FROM `tasks` JOIN `pivot_task_group` ON `tasks`.`id` = `pivot_task_group`.`task_id` JOIN `users` ON `tasks`.`author_id` = `users`.`id` ORDER BY `tasks`.`created_at` DESC 

to the Eloquent request and add pagination for 5 tasks on the page:

 $tasks = Task::all()->publishedTasks()->paginate(5); 

or so

 $tasks = $tasks->publishedTasks()->paginate(5); 

or how?

 ... 

Available: class Task extends Model {// use SoftDeletes;

  protected $dates = ['deleted_at']; public function groups() { return $this->belongsToMany('App\Models\Group', 'pivot_task_group'); } public function comments() { return $this->hasMany('App\Models\Comment'); } public function getPublishedTasks(){ return $this->belongToMany('Group', 'pivot_task_group'); } public static function author(){ return $this->belongTo('User'); } } 

And User models, Group .

Here's what I expect to get in response: phpmyadmin request response screen so that you can handle foreach ($ tasks as $ task) {echo $ task-> title; echo $ task-> link; echo $ task-> author-> name; }

I will wait for your recommendations. and thank you in advance for all the advice and cobblestones.)))

    1 answer 1

    dismantled something: in order to pull out usernames by author_id I do this:

    in the Task model I write

     public function author() { return $this->belongsTo('User'); } 

    and in the code where I will have an object-task (not a task) I address it

     $tasks= Task::with('author')->get(); 

    The question with reference is removed, it remains to understand how to do:

     SELECT 'tasks'.'title', 'tasks'.'link' FROM 'tasks' JOIN 'pivot_task_group' ON 'tasks'.'id' = 'pivot_task_group'.'task_id' GROUP BY 'pivot_task_group'.'task_id' ORDER BY 'tasks'.'created_at' DESC 

    By the way, the task_id group was task_id because the published tasks in the pivot can be repeated, and any one task can be assigned to different groups.