简体   繁体   中英

Laravel chain Eloquent models

How can I turn this Eloquent chain

User::find(Sentry::getUser()->id)
    ->friends()
    ->join('users', 'friends.friend_id', '=', 'users.id')
    ->get();

into something like

User::find(Sentry::getUser()->id)
       ->friends()
       ->user()
       ->get();

There is a User model and a Friends model. The goal here is to get all friends of the current User and attach each the respective User model to each Friend. The User model has a hasMany('Friends') relationship on it.

What I have works I would just like to simplify the call if possible into something cleaner.

如果您的关系设置正确,则可以

User::with('friends')->where('id', Sentry::getUser()->id)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM