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`