I have these models:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function parentCategory()
{
return $this->belongsTo(self::class, 'parent_id');
}
public function subCategories()
{
return $this->hasMany(self::class, 'parent_id');
}
public function courses()
{
return $this->hasMany(Course::class);
}
public function SubcategoryCourses()
{
return $this->hasManyThrough(Course::class, self::class, 'parent_id', 'category_id', 'id');
}
...
and my Course model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
public function category()
{
return $this->belongsTo(Category::class);
}
public function students()
{
return $this->belongsToMany(User::class, 'enrollments', 'course_id', 'user_id')->withTimestamps();
}
}
...
I would like to get the most popular parent categories
, ie parent categories ordered by the number of students enrolled in subcategories. I know it is easy to get the subcategories ordered by count of student, but what I would like is actually get the parent categories ordered by the number of students. Eloquent or Query builder, doesn't matter to me.
I am on Laravel 5.5 and MySQL btw
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2DDhGtT
via IFTTT
Aucun commentaire:
Enregistrer un commentaire