0

OK, here's my code: my relationships from my model

// message model
    public function profile() {
        return $this->has_one('Profile', 'id');
    }

// profile model
    public function message(){
        return $this->has_many('message', 'id')->order_by('created_at', 'asc');
    }

My controller:

    public function get_MyMessages(){
        $per_page = 50; // results per page
        $messages = Message::with('profile')->where('receiver_id', '=', Auth::user()->id)->or_where('sender_id', '=', Auth::user()->id)->take($per_page);

        $messages = $messages->paginate( $per_page ); 
        $messages->appends( array($per_page) )->links(); 


        $data = array( // data to pass to view
        'title' => 'My Messages',
        'messages' => $messages,
    );
    return View::make('myProfile.myMessages',$data); // create view
}

In my view I can do:

@foreach($messages->results as $message)
    {{ $message->message }}
@endforeach

Which works fine, but when I try: {{ $messages->profile->first_name }}

I get the following error: Trying to get property of non-object If I try: {{ print_r($message->profile }} all the data is there I just can't access it.

I have tried everything what am I doing wrong???

user1543871
  • 355
  • 1
  • 6
  • 16

1 Answers1

2
// message model
    public function profile() {
        //the following statement means that every message has a profile (not true):
        //instead, let's define the other side of the profile relationship.
        return $this->has_one('Profile', 'id');
    }

// profile model
    public function message(){
        //the following statement means that a profile has many messages (true)
        return $this->has_many('message', 'id')->order_by('created_at', 'asc');
    }

This code essentially means:

The profile has many messages, and each message has a profile.

In these relationship declarations, you are creating only part of the relationship, but not the receiving end. Based on your code I'm assuming you're using Laravel 3 (Laravel 4 relationships are camel-cased instead of snake-cased).

The following is a correction based on Laravel 3:

// message model
    public function profile() {
        return $this->belongs_to('Profile', 'id');
    }

// profile model
    public function message(){
        return $this->has_many('message', 'id')->order_by('created_at', 'asc');
    }

This code means:

The profile has many messages, and the messages belong to the profile.

EDIT: Your blade syntax also needs to be edited. Using {{ $messages->profile->first_name }} is calling the method profile on the $messages variable, which returns the results of the lazy-load query, but not the message object (or model instance). Keep in mind, the model message.php or profile.php represents a single object (so one message or one profile). The query contained in $messages is a non-object, it is neither a profile nor a message, but a collection (or group) of messages.

The foreach loop works because it essentially breaks the list of objects inside $messages to be used, inside the foreach tags, you specify what you want done to each of those objects, in your case, you want to show the message of each object. In this foreach loop, you could successfully access the profile method.

i.e. you could do:

@foreach($messages->results as $message)
    {{ $message->message }}
    {{ $message->profile->first_name }}
@endforeach

If you don't want the profile displayed in each message:

To get the Authenticated User's profile, to display their profile, you could do Auth::user()->profile, assuming that you have that relationship inside the respective models. Your lazy-load query already fetched the Authenticated User's profile, so this command will not create a second query (thanks to eloquent).

  • Hi, thanks for the answer, makes perfect sense I really thought you had my problem solved. However I am still getting the same error: Trying to get property of non-object – user1543871 May 12 '13 at 00:06
  • My apologies, I did not glance through your blade syntax, I've edited my answer and it should now completely resolve the problem. :) –  May 12 '13 at 00:50
  • OK thanks so much for your help, I will review my code and see how I get on. – user1543871 May 12 '13 at 15:31