Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cosminc's avatar

Looking for the Laravel way to fetch aggregated data from a table

Hi everybody,

I'm looking for a "Laravel way" to implement the following requirement: given three tables of categories, products and stacks, I want to save all the products for a specified startup (identified by startup_id) in the stacks table. The categories and products are to be defined by the user and then for each startup the user must check one or more products.

mysql> describe categories;
+------------+---------------------+------+-----+---------+----------------+
| Field      | Type                | Null | Key | Default | Extra          |
+------------+---------------------+------+-----+---------+----------------+
| id         | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| title      | varchar(255)        | NO   | UNI | NULL    |                |
| slug       | varchar(255)        | NO   |     | NULL    |                |
| user_id    | bigint(20) unsigned | NO   |     | NULL    |                |
| created_at | timestamp           | YES  |     | NULL    |                |
| updated_at | timestamp           | YES  |     | NULL    |                |
+------------+---------------------+------+-----+---------+----------------+

mysql> describe products;
+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| category_id | bigint(20) unsigned | NO   |     | NULL    |                |
| title       | varchar(255)        | NO   | MUL | NULL    |                |
| slug        | varchar(255)        | NO   |     | NULL    |                |
| image       | varchar(255)        | NO   |     | NULL    |                |
| user_id     | bigint(20) unsigned | NO   |     | NULL    |                |
| created_at  | timestamp           | YES  |     | NULL    |                |
| updated_at  | timestamp           | YES  |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+

mysql> describe stacks;
+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| startup_id  | bigint(20) unsigned | NO   | MUL | NULL    |                |
| category_id | bigint(20) unsigned | NO   |     | NULL    |                |
| product_id  | bigint(20) unsigned | NO   |     | NULL    |                |
| user_id     | bigint(20) unsigned | NO   |     | NULL    |                |
| created_at  | timestamp           | YES  |     | NULL    |                |
| updated_at  | timestamp           | YES  |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+

Is it possible to build an Eloquent relation to allow fetching of all categories and products for a given startup?

So far my models look like this:

class Category extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'slug', 'user_id'
    ];

    /**
     * The products that belong to this category.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function products()
    {
        return $this->hasMany('App\Product', 'category_id')
            ->orderBy('title', 'asc');
    }
}

class Product extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'category_id', 'title', 'slug', 'image', 'user_id'
    ];

    /**
     * The category that the stack product belongs to.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo('App\Category');
    }
}
0 likes
2 replies

Please or to participate in this conversation.