Last active
February 1, 2022 12:42
-
-
Save dominiquevienne/785c20dcdb9e79f478dde19bb858eab6 to your computer and use it in GitHub Desktop.
Monomorphic relation between User and Country in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
class Country extends Model | |
{ | |
use HasFactory; | |
protected $fillable = [ | |
'name', | |
'capital', | |
]; | |
public function users() | |
{ | |
return $this->hasMany(User::class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
class User extends Model | |
{ | |
use HasFactory; | |
protected $fillable = [ | |
'firstname', | |
'lastname', | |
]; | |
public function country() | |
{ | |
return $this->belongsTo(Country::class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment