Eloquent supports relationships such as:
hasOnehasManybelongsTobelongsToManyhasManyThroughpolymorphic relationships
Example:
class User extends Model
{
public function orders()
{
return $this->hasMany(Order::class);
}
}
Usage:
$user->orders;
Or:
$user->orders()->where('status', 'paid')->get();
The first accesses the relationship result; the second returns a relationship/query builder.
Scenario
A shopping application might have:
User
└── Orders
└── OrderItems
└── Product
Model relationships let application code represent this structure naturally.
0 comments:
Post a Comment
Thanks