首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并laravel 4多个关系

合并laravel 4多个关系
EN

Stack Overflow用户
提问于 2018-01-18 22:13:55
回答 1查看 36关注 0票数 0

我在laravel v4.2中有两个关系。当我将这两个函数合并到第三个函数中,然后调用第三个函数时,我收到了未捕获的异常。下面是我的代码

代码语言:javascript
复制
   public function following_friend() {
         return $this->hasOne('Friend', 'following_id', 'id');
   }

   public function follower_friend() {
        return $this->hasOne('Friend', 'follower_id', 'id');
   }

   public function mutual_friends() {
        return $this->following_friend->merge($this->follower_friend);
   }

    public static function get_users_infomation_by_ids($login_id, $users_arr = array()) {
        $users = User::where(function($sql) use($login_id, $users_arr) {
                    $sql->whereIn('id', $users_arr);
                })                
                ->with('mutual_friends')
                ->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));
        return (!empty($users) && count($users) > 0) ? $users->toArray() : array();
    }

我不知道合并这两个关系的问题在哪里。

EN

回答 1

Stack Overflow用户

发布于 2018-01-18 22:22:25

试着像下面这样写:

代码语言:javascript
复制
public static function get_users_infomation_by_ids($login_id, $users_arr = array()) {
        $users = User::with('mutual_friends')
                ->where(function($sql) use($login_id, $users_arr) {
                    $sql->whereIn('id', $users_arr);
                })
                ->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));
        return (!empty($users) && count($users) > 0) ? $users->toArray() : array();
    }

因为我已经在我的一个项目中使用了这样的东西:

代码语言:javascript
复制
public function getShipmentDetails($shipmentId = null) {
        $response = Shipment::with('pdDetails','shipmentDocuments')
                ->where('id', $shipmentId)
                ->first();
        if($response) {
            return $response->toArray();
        }
    }

这对我很管用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48323327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档