在Laravel的多对多关系中使用'with'

I have a table user - User(Model), which has relationship:

public function roles()
    {
        return $this->belongsToMany(Config::get('entrust.role'), Config::get('entrust.role_user_table'), 'user_id', 'role_id');
    }

public function regions() {

        return $this->belongsToMany('App\Regions', 'user_region', 'user_id', 'region_id');
    }

I am trying this query, but it doesn't me the required result

$result = User::with(['roles' => function($query) {
                                $query->select('user_id','role_id');
                                }, 
                                'regions' => function($query) {
                                $query->select('user_id','region_id', 'region_name');
                                }])
                                ->where('user_id', '=', $id)
                                ->get()->toArray();

It only gives me data from user table and doesn't give the relationship data.

What am I missing out??

The notation you are using should be used for constraints. From your code it seems you don't actually need any contraints.

$result = User::with(['roles', 'regions'])
                                ->where('user_id', '=', $id)
                                ->first()->toArray();

The relationship you defined is only a belongsTo. You should probably use a hasMany relationship.

If you're using 5.1 try this:

$result = User::whereHas(['roles' => function($query) {
                            $query->lists('user_id','role_id');
                            }, 
                            'regions' => function($query) {
                            $query->lists('user_id','region_id','region_name');
                            }])
                            ->where('user_id', '=', $id)
                            ->all();

if not remove all() and use get()

This worked for me.

$users = User::with('regions', 'roles')->get();

            $userInfo = [];

            foreach ($users as $user) 
            {
                $userInfo[] = [
                        'users' => $user,
                        'regions' => $user->regions->toArray(),
                        'roles' => $user->roles->toArray(),
                ];
            }