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

Miaababikir's avatar

Polymorphic relationship with counts and filters (Nova)

I am working on a project where I have these models

  • Brand (Has many branches).
  • Branch (Belongs to a Brand).
  • Transactions (Polymorphic relation for the brand and branch) because the transaction can be created from brand or from branch.

Here is the migrations

Brands migration:

 Schema::create('brands', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->json('name');
            $table->string('slug');
            $table->string('code')->nullable();
            $table->json('description')->nullable();
            $table->timestamps();
 });

Branches migration:

Schema::create('branches', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('brand_id')->unsigned();
            $table->json('name');
            $table->string('slug')->index()->unique();
            $table->json('address')->nullable();
            $table->timestamps();
  });

Transactions Migration:

        Schema::create('transactions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('model_id')->unsigned()->index();
            $table->string('model_type');
            $table->timestamps();
        });

I want to get the top brand with the biggest transactions (using nova lens)

total = brand transactions + branch(that belongs to the same branch) transactions

0 likes
0 replies

Please or to participate in this conversation.