0

I'm having trouble accessing a relationship in Laravel. Relevant to this question, I have two tables, messages and users.

The messages table looks like:

Messages: id, user_from, user_to, read, message, created_at, updated_at

The users table looks like:

Users: id, email, password, name, created_at, updated_at, bio, reputation, last_login_ip, last_seen, active

The user_from and user_to columns are both ints that correspond to the id of the user (primary key) that sent the message or who the message was sent to.

The relevant portion of my Message model looks like:

class Message extends Eloquent {

    //relationships
    public function user_from() {
        return $this->belongs_to('User', 'user_from');
    }

    public function user_to() {
        return $this->belongs_to('User', 'user_to');
    }
}

The relevant portion of my User model looks like:

class User extends Eloquent {

    //relationships
    public function messages_from() {
        return $this->has_many('Message', 'user_from');
    }

    public function messages_to() {
        return $this->has_many('Message', 'user_to');
    }
}

In a view, I'm trying to access the user that wrote a message, writing code that looks like:

{{ $message->user_from->name }}

Which throws an error Trying to get property of non-object on that one line. I know the $message object is defined because all other properties except user_from and user_to are defined and print out perfectly. I've read through the documentation several times and I can't figure out what I have done wrong here... does anyone stick out to anyone? This seems like a textbook example but it doesn't work.

The way I've passed the data to the view is:

$messages = Message::where('user_to', '=', Auth::user()->id)->take(20)->get();
wnajar
  • 747
  • 1
  • 7
  • 27
  • To clarify, this is Laravel 3. – wnajar Jun 06 '13 at 18:04
  • So if you echo out $message->user_to do you get an id at least? Cuz then you could just workaround it like this....echo (User::find($message->user_from)->name) ....Although it would be nice to get the relationship working – Kylie Jun 06 '13 at 18:09
  • Trying that right now. *Edit:* yes, echoing out `$message->user_from` gives me the ID of the user it's from. I guess just doesn't make the connection that this corresponds to a `User` model. Not ideal at all with the SQL query in the view, but trying the workaround now... – wnajar Jun 06 '13 at 18:10
  • I can do some really ugly stuff at the beginning of the view doing `$message->user_from = User::find($message->user_from);` and `$message->user_to = User::find($message->user_to);` to define them correctly.. but there has to be a better way? – wnajar Jun 06 '13 at 18:14
  • That seems like a lot of SQL overhead running the query each time (for each message, that is) especially if there are a lot of messages. – wnajar Jun 06 '13 at 18:15
  • relationships is the better way, but Im at work right now, so I can't dive to deeply into why yours isnt working...hopefully someone else can :) – Kylie Jun 06 '13 at 18:19
  • Co-developer is also currently at work (Facebook). Let's just wait... – wnajar Jun 06 '13 at 18:20
  • If this is still unanswered when I get home, Ill give it a real try. :) – Kylie Jun 06 '13 at 18:23
  • You're not calling a method, you're calling a property. Try `{{ $message->user_from()->name }}` - or `{{ $message->user_from()->get()->name }}` , etc. – Rob W Jun 06 '13 at 18:24
  • @RobW that gave an altogether different error, an error with SQL. I tried that first. – wnajar Jun 06 '13 at 18:30

1 Answers1

0

Making the method names in the Message model from_user() and to_user() instead of user_from() and user_to() (and changing the view to correspond) fixes the problem completely. No other code was changed.

Laravel fetches the database field over the method name, when you specify a custom field (I think). Weird.

wnajar
  • 747
  • 1
  • 7
  • 27