mardi 30 octobre 2018

Laravel relation with types

I have tried to find the best solution for my problem without getting exactly what i want.

I have 4 tables now, the structure looks like this.

:: Users ::
id
name
email
------

:: user_equipment ::
id
user_id
equipment_id
equipment_type

:: weapons ::
id
name
price

:: armor ::
id
name
price

My goal is to be able to use my relations like this:

$user->equipment
$user->equipment->weapons
$user->equipment->armor

Here is what i have right now:

User Model:

class UserEquipment extends Model
{    
  public function equipment()
  {
    return $this->hasOne(UserEquipment::class);
  }
}

UserEquipment Model:

class UserEquipment extends Model
{
   public function weapons()
   {
     return $this->hasMany(Weapon::class);
   }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire