Hey Ludek,
You can very easily understand this with a single abstract model file. You're already stubbing out the functions for the relationships or the "casts" for the model properties... all I'm saying is that you would want to add in another "model property" or "relation" in the class declaration.
<?php
namespace App\Models\AbstractModels;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class AbstractCustomer
* @package App\Models
*
* @property int $id
* @property int $team_id
* @property string $email
* @property string $first_name
* @property string $last_name
* @property string $cell_phone
* @property string $profile_photo_path
* @property \Carbon\Carbon $subscribed_to_email_at
* @property \Carbon\Carbon $unsubscribed_to_email_at
* @property \Carbon\Carbon $subscribed_to_mail_at
* @property \Carbon\Carbon $unsubscribed_to_mail_at
* @property \Carbon\Carbon $subscribed_to_sms_at
* @property \Carbon\Carbon $unsubscribed_to_sms_at
* @property \Carbon\Carbon $subscribed_to_phone_at
* @property \Carbon\Carbon $unsubscribed_to_phone_at
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $deleted_at
* @property \App\Models\Team $team
* @property \App\Models\Company|null $company
* @property \Illuminate\Database\Eloquent\Collection $orders
* @property \Illuminate\Database\Eloquent\Collection $orderDetails
* @property \Illuminate\Database\Eloquent\Collection $addresses
* @property \Illuminate\Database\Eloquent\Collection $notes
* @property \Illuminate\Database\Eloquent\Collection $phoneNumbers
* @property \Illuminate\Database\Eloquent\Collection $companies
*/
abstract class AbstractCustomer extends Model
{
use SoftDeletes;
/**
* Primary key type.
*
* @var string
*/
protected $keyType = 'string';
/**
* Primary key is non-autoincrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'string',
'team_id' => 'string',
'email' => 'string',
'first_name' => 'string',
'last_name' => 'string',
'cell_phone' => 'string',
'subscribed_to_email_at' => 'datetime',
'subscribed_to_sms_at' => 'datetime',
'subscribed_to_mail_at' => 'datetime',
'subscribed_to_phone_at' => 'datetime',
'unsubscribed_to_email_at' => 'datetime',
'unsubscribed_to_sms_at' => 'datetime',
'unsubscribed_to_mail_at' => 'datetime',
'unsubscribed_to_phone_at' => 'datetime',
'profile_photo_path' => 'string',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime'
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'team_id',
'email',
'first_name',
'last_name',
'cell_phone',
'profile_photo_path'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
public function team()
{
return $this->belongsTo('\App\Models\Team', 'team_id', 'id');
}
public function orders()
{
return $this->hasMany('\App\Models\Order', 'customer_id', 'id');
}
public function orderDetails()
{
return $this->hasMany('\App\Models\OrderDetail', 'customer_id', 'id');
}
public function addresses()
{
return $this->morphMany('\App\Models\Address', 'Addressable', 'addressable_type', 'addressable_id');
}
public function notes()
{
return $this->morphMany('\App\Models\Note', 'Noteable', 'noteable_type', 'noteable_id');
}
public function phoneNumbers()
{
return $this->morphMany('\App\Models\PhoneNumber', 'PhoneNumberable', 'phone_numberable_type', 'phone_numberable_id');
}
public function companies()
{
return $this->belongsToMany('\App\Models\Company', 'customer_has_company', 'customer_id', 'company_id')->withPivot('primary', 'title', 'division', 'phone', 'extension', 'notes', 'email');
}
}