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

tchicotti's avatar

Given the follow structure, would you use PIVOT or Model?

We (me and my teammates) got the mission to create the models and relationships for a older designed database and discuted how should be the best approach to write them.

Below this is just 3 tables of many and many others relationships in the project.

table: tax
- id
- uuid
- name
- created_by_user_id
- created_by_timestamp (custom timestamp name)
- updated_by_user_id
- updated_by_timestamp (custom timestamp name)

table: service
- id
- uuid
- name
- created_by_user_id
- created_by_timestamp (custom timestamp name)
- updated_by_user_id
- updated_by_timestamp (custom timestamp name)

table: service_tax
- id
- uuid
- created_by_user_id
- created_by_timestamp (custom timestamp name)
- updated_by_user_id
- updated_by_timestamp (custom timestamp name)
- service_id
- tax_id
- amount
- is_active

Were are divided with we should create class ServiceTax extends Model or class ServiceTax extends Pivot

And how to write the classes and relationships.

The discussion started because of 'id and uuid', the database handle it.

So which side would you guys be?

use Illuminate\Database\Eloquent\Relations\Pivot;
class ServiceTax extends Pivot
{
    protected $incrementing = true;

    protected $table = "service_tax";

    const CREATED_AT = "created_by_timestamp";
    const UPDATED_AT = "updated_by_timestamp";
}

// or 
se Illuminate\Database\Eloquent\Model;
class ServiceTax extends Model
{
    protected $table = "service_tax";

    const CREATED_AT = "created_by_timestamp";
    const UPDATED_AT = "updated_by_timestamp";
}

And how Tax::class and Service::class should be?


class Tax extends Model
{

			protected $table = "tax";

			const CREATED_AT = "created_by_timestamp";
			const UPDATED_AT = "updated_by_timestamp";

			public function service()
			{
						return $this->belongsToMany(Service::class)
									->using(ServiceTax::class)
									->withTimestamps()
									->withPivot('is_active', 'amount');
			}

}

`ps: suppressed the user_id at the moment just for the discussion`
0 likes
4 replies
tchicotti's avatar

@Tray2 naming conventions is one of the first problems that we are facing, first because the database was already designed when we started working with it and can't change it, so we have to adapt the laravel on the fly to keep a new instance of the project working on, cause every single query was using Builder queries instead models (DB::select()) just had the default User model and now the new CTO want keep the same db but with a laravel driven project as much as possible.

The second one are the relationships, because there are to many tables which should be simple pivot table yet they decide to put id, uuid, timestamps, inserted_by_user_id, update_by_user_id to have the bare minimal control of who did what

Tray2's avatar

@tchicotti Ah man, that is going to bite you in the ass sooner or later. I would definitely push for a remake of the database model, but if that isn't an option, then I would use the overrides built in to the models, to define the names of things. The same with the relationships. I would also push for Using Eloquent to make it easier to read.

I would suggest giving this one a read.

https://tray2.se/posts/use-a-view-instead-of-a-complex-eloquent-query-in-your-laravel-application

1 like
martinbean's avatar

@tchicotti If it has its own primary key, then create an actual model (not a pivot model) for that table.

1 like

Please or to participate in this conversation.