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:
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.)))