hasOne
Used when one model owns one related record.
class User extends Model
{
public function profile()
{
return $this->hasOne(Profile::class);
}
}
Database:
users
profiles
user_id
Usage:
$user->profile;
hasMany
Used when one model owns multiple records.
public function orders()
{
return $this->hasMany(Order::class);
}
Usage:
$user->orders;
Scenario
If a user has exactly one profile:
User -> Profile
use hasOne.
If a user can place hundreds of orders:
User -> Orders
use hasMany.
0 comments:
Post a Comment
Thanks