Sebastian Walter
Sebastian Walter

Reputation: 21

Where to put user related get functions

I have a problem I am working on for quite a while now.

I am using Laravel 5.0 and have relationships set up:

Now I am trying to set up a function which I need in many controllers. I have this function in User.php:

public function last_destination() {
    return $last_destination =
        \App\Destination::where('itinerary_id', auth()->user()->active_itinerary_id)
        ->orderBy('order_index', 'DESC')
        ->first();
}

When I try to retrieve the last destination, it works fine, but when I pass the last destination as a variable to a view, it throughs me the error: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

Should I put this function in another class? If so where should I put it to still be able to call it as $user->last_destination()?

I appreciate any help!

Thanks a lot! Sebastian

Upvotes: 0

Views: 35

Answers (1)

John
John

Reputation: 3916

You are most likely trying to call $user->last_destination somewhere, as you would if that method returned a Relation.

Try using $user->last_destination() instead. Or updating your method to return a Relation instance.

Upvotes: 1

Related Questions