Get information from a foreign key, when it has multiple entries in a table

Posted 3 years ago by snapper

I am using Laravel 4.2 and have a table called users and a column in that table labeled name. I also have a transactions table that has 2 columns which are both foreign keys that reference the primary key of the users table. One of the rows in the transactions is labeled given_to and the other is labeled received_from. How can I access the name associated with the given_to user id from the transactions table using Eloquent? I tried the following in a view and received a trying to get property of non-object error:

@foreach($transactions as $transaction) {{ $transaction->given_to->name }} @endforeach

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.