合并laravel 4多重关系

时间:2018-01-18 14:13:55

标签: php laravel

我在laravel v4.2中有两次重新关系。当我将这两个重新合并到第三个函数然后我调用第三个函数然后我收到Uncought异常。以下是我的代码

   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();
    }

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

1 个答案:

答案 0 :(得分:0)

尝试写如下: -

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();
    }

因为我在我的项目中使用了这样的: -

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

它对我有用。