protected function getBelongsToManyData($relation, $relationForeignKey, $foreignKeyIds)
    {
        $relationConnection = $relation->getConnection()->getName();
        $relationTable = $relation->getTable();
        $relationRelatedKey = $relation->getQualifiedRelatedPivotKeyName();
        //Get related ids
        $foreignKeyIdsUse = DB::connection($relationConnection)
            ->table($relationTable)
            ->whereIn($relationForeignKey, $foreignKeyIds)
            ->select([$relationRelatedKey])
            ->get()->toArray();
        $foreignKeyUse = explode('.', $relationRelatedKey);
        $foreignKeyUse = end($foreignKeyUse);
        $foreignKeyIdsUse = array_column($foreignKeyIdsUse, $foreignKeyUse);

        return [
            'foreignKeyIdsUse' => collect($foreignKeyIdsUse),
            'foreignKeyUse'    => $relation->getRelated()->getKeyName(),
        ];
    }
protected function getBelongsToManyData($relation, $relationForeignKey, $foreignKeyIds)
    {
        $relationConnection = $relation->getConnection()->getName();
        $relationTable = $relation->getTable();
        $relationRelatedKey = $relation->getQualifiedRelatedPivotKeyName();
        //Get related ids
        $foreignKeyIdsUse = DB::connection($relationConnection)
            ->table($relationTable)
            ->whereIn($relationForeignKey, $foreignKeyIds)
            ->select([$relationRelatedKey])
            ->get()->toArray();
        $foreignKeyUse = explode('.', $relationRelatedKey);
        $foreignKeyUse = end($foreignKeyUse);
        $foreignKeyIdsUse = array_column($foreignKeyIdsUse, $foreignKeyUse);

        return [
            'foreignKeyIdsUse' => collect($foreignKeyIdsUse),
            'foreignKeyUse'    => $relation->getRelated()->getKeyName(),
        ];
    }
protected function getBelongsToManyData ( $ relation , $ relationForeignKey , $ foreignKeyIds ) { $ relationConnection = $ relation -> getConnection ( ) -> getName ( ) ; $ relationTable = $ relation -> getTable ( ) ; $ relationRelatedKey = $ relation -> getQualifiedRelatedPivotKeyName ( ) ; //Get related ids $ foreignKeyIdsUse = DB :: connection ( $ relationConnection ) -> table ( $ relationTable ) -> whereIn ( $ relationForeignKey , $ foreignKeyIds ) -> select ( [ $ relationRelatedKey ] ) -> get ( ) -> toArray ( ) ; $ foreignKeyUse = explode ( '.' , $ relationRelatedKey ) ; $ foreignKeyUse = end ( $ foreignKeyUse ) ; $ foreignKeyIdsUse = array_column ( $ foreignKeyIdsUse , $ foreignKeyUse ) ; return [ 'foreignKeyIdsUse' => collect ( $ foreignKeyIdsUse ) , 'foreignKeyUse' => $ relation -> getRelated ( ) -> getKeyName ( ) , ] ; }
Get many to many related key ids and key use.

@param Illuminate\Database\Eloquent\Relations\Relation $relation
@param string                                          $relationForeignKey
@param array                                           $foreignKeyIds

@return array
Get many to many related key ids and key use .