Answer the question
In order to leave comments, you need to log in
How can you get values if you use a one-to-many relationship?
In Laravel, I use two User entities that can have several different Transport values. A one-to-many relationship is used. The Transport table contains information about the user's transport.
The transport table contains three columns:
UserId, ParamName, ParamValue:
1 | car | 'audi'
1 | bicicle | 'ufo'
2 | car | 'bmv'
2 | track | 'man'
3 | ship | 'cruize'
public function transports() : HasMany
{
$this->hasMany(Transport::class, 'UserId', 'Id');
}
$user = User::find(1);
$transports = $user->transports()->get();
foreach($transports as $transport)
{
echo $transport->ParamName;
echo $transport->ParamValue;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question