BigleBomb's avatar

How to assign data from a foreign key table to the primary table

I have 2 tables on my database called 'Users' and 'Reimburse'. The relation is one-to-many.

Every time I call every Reimburse data in the database using all() I also get the data of the User table.

But, the problem is that I cannot assign each User data to the correct Reimburse value.

Example:

$reimburse = Reimburse::all();
$user;
foreach($reimburse as $userdata)
{
    $reimburse->add($userdata->user()->get());
}
$res['success'] = true;
$res['result'] = $reimburse;
return response($res);

Pastebin link for above code: https://pastebin.com/tRMXfENd

Result: https://pastebin.com/U2unZTRH

The result of above code is always resulting the reimburses data being the first to be shown followed by the user data later. What I want is to assign every User data to each Reimburse data. How can I do that?

0 likes
1 reply
RayC's avatar

How are you assigning the user to $user?

Maybe change that to:

// $id being the id of the user you want to assign to
$user = User::find($id);

Regards Ray

Please or to participate in this conversation.