`<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
protected $fillable = [
'firstname',
'lastname',
'phone',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function cars()
{
return $this->hasMany('App\Models\Cars');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Cars extends Model
{
protected $fillable = [
‘user_id’, ‘brand’,‘model’, ‘year_of_production’, ‘vin_number’, ‘fuel_type’, ‘oil_type’];
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function services()
{
return $this->hasMany('App\Models\Services');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Service extends Model
{
protected $fillable = [
‘car_id’, ‘date_of_service’,‘service_name’, ‘service_description’, ‘service_cost’, ‘parts_used’];
protected $guarded = ['id'];
public function servicedCar()
{
return $this->belongsTo('App\Models\Cars');
}
public function servicingMechanic()
{
return $this->belongsTo('App\Models\Mechanic');
}
}
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Mechanic extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'companyname',
'phone',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'companyname_verified_at' => 'datetime',
];
public function servicedCars()
{
return $this->hasMany('App\Models\Cars');
}
public function doneServices()
{
return $this->hasMany('App\Models\Services');
}
public function carOwner()
{
return $this->hasOneThrough(
Users::class,
Cars::class,
'mechanic_id', // Foreign key on the cars table...
'car_id', // Foreign key on the owners table...
'id', // Local key on the mechanics table...
'id' // Local key on the cars table...
);
}
}
`