public static function filterRows($rows, $withPagination = true, $justForInTable = false, $filterColumns = array())
    {
        $filteredList = array();
        $temporaryList = array();
        $language = App::getLocale();

        if($withPagination) {
            if(!is_array($rows)) {
                $rows = $rows->toArray();
            }
            $list = $rows['data'];
        }else{
            if(is_a($rows, 'Illuminate\Support\Collection')) {
                $rows = $rows->toArray();
            }
            $list = $rows;
        }

        // loop throw the list
        foreach ($list as $rowKey => $row){
            if(gettype($row) == "object" && in_array('Illuminate\Database\Eloquent\Model', class_parents($row))) {
                $row = $row->getAttributes();
            }
            foreach ($row as $key => $value){
                $jsonValue = true;
                // Casts columns
                if(is_object($value)) {
                    $jsonValue = $value;
                }elseif(is_string($value) ) {
                    // non-casts columns
                    $jsonValue = json_decode($value);
                }

                if($justForInTable) {
                    //fields like ID, postID, title and createdByUserID should always be shown in the list
                    array_push($filterColumns, 'postID', 'belongsToID', 'title', 'createdByUserID');

                    if (in_array($key, $filterColumns)) {
                        if ($jsonValue && isset($jsonValue->$language)) {
                            $temporaryList[$key] = $jsonValue->$language;
                            $temporaryList["original_object_".$key] = $value;
                        }else{
                            // empty if there is no translation in the selected language
                            $temporaryList[$key] = $value;
                        }
                    }
                }else{
                    if ($jsonValue && isset($jsonValue->$language)) {
                        $temporaryList[$key] = $jsonValue->$language;
                        //store original object because we need it in vue js!
                        $temporaryList["original_object_".$key] = $value;
                    }else{
                        $temporaryList[$key] = $value;
                    }
                }
            }

            $filteredList[$rowKey] = $temporaryList;
        }

        if ($withPagination) {
            $rows['data'] = $filteredList;
        }else{
            $rows = $filteredList;
        }
        return $rows;
    }
public static function filterRows($rows, $withPagination = true, $justForInTable = false, $filterColumns = array())
    {
        $filteredList = array();
        $temporaryList = array();
        $language = App::getLocale();

        if($withPagination) {
            if(!is_array($rows)) {
                $rows = $rows->toArray();
            }
            $list = $rows['data'];
        }else{
            if(is_a($rows, 'Illuminate\Support\Collection')) {
                $rows = $rows->toArray();
            }
            $list = $rows;
        }

        // loop throw the list
        foreach ($list as $rowKey => $row){
            if(gettype($row) == "object" && in_array('Illuminate\Database\Eloquent\Model', class_parents($row))) {
                $row = $row->getAttributes();
            }
            foreach ($row as $key => $value){
                $jsonValue = true;
                // Casts columns
                if(is_object($value)) {
                    $jsonValue = $value;
                }elseif(is_string($value) ) {
                    // non-casts columns
                    $jsonValue = json_decode($value);
                }

                if($justForInTable) {
                    //fields like ID, postID, title and createdByUserID should always be shown in the list
                    array_push($filterColumns, 'postID', 'belongsToID', 'title', 'createdByUserID');

                    if (in_array($key, $filterColumns)) {
                        if ($jsonValue && isset($jsonValue->$language)) {
                            $temporaryList[$key] = $jsonValue->$language;
                            $temporaryList["original_object_".$key] = $value;
                        }else{
                            // empty if there is no translation in the selected language
                            $temporaryList[$key] = $value;
                        }
                    }
                }else{
                    if ($jsonValue && isset($jsonValue->$language)) {
                        $temporaryList[$key] = $jsonValue->$language;
                        //store original object because we need it in vue js!
                        $temporaryList["original_object_".$key] = $value;
                    }else{
                        $temporaryList[$key] = $value;
                    }
                }
            }

            $filteredList[$rowKey] = $temporaryList;
        }

        if ($withPagination) {
            $rows['data'] = $filteredList;
        }else{
            $rows = $filteredList;
        }
        return $rows;
    }
public static function filterRows ( $ rows , $ withPagination = true , $ justForInTable = false , $ filterColumns = array ( ) ) { $ filteredList = array ( ) ; $ temporaryList = array ( ) ; $ language = App :: getLocale ( ) ; if ( $ withPagination ) { if ( ! is_array ( $ rows ) ) { $ rows = $ rows -> toArray ( ) ; } $ list = $ rows [ 'data' ] ; } else { if ( is_a ( $ rows , 'Illuminate\Support\Collection' ) ) { $ rows = $ rows -> toArray ( ) ; } $ list = $ rows ; } // loop throw the list foreach ( $ list as $ rowKey => $ row ) { if ( gettype ( $ row ) == "object" && in_array ( 'Illuminate\Database\Eloquent\Model' , class_parents ( $ row ) ) ) { $ row = $ row -> getAttributes ( ) ; } foreach ( $ row as $ key => $ value ) { $ jsonValue = true ; // Casts columns if ( is_object ( $ value ) ) { $ jsonValue = $ value ; } elseif ( is_string ( $ value ) ) { // non-casts columns $ jsonValue = json_decode ( $ value ) ; } if ( $ justForInTable ) { //fields like ID, postID, title and createdByUserID should always be shown in the list array_push ( $ filterColumns , 'postID' , 'belongsToID' , 'title' , 'createdByUserID' ) ; if ( in_array ( $ key , $ filterColumns ) ) { if ( $ jsonValue && isset ( $ jsonValue -> $ language ) ) { $ temporaryList [ $ key ] = $ jsonValue -> $ language ; $ temporaryList [ "original_object_" . $ key ] = $ value ; } else { // empty if there is no translation in the selected language $ temporaryList [ $ key ] = $ value ; } } } else { if ( $ jsonValue && isset ( $ jsonValue -> $ language ) ) { $ temporaryList [ $ key ] = $ jsonValue -> $ language ; //store original object because we need it in vue js! $ temporaryList [ "original_object_" . $ key ] = $ value ; } else { $ temporaryList [ $ key ] = $ value ; } } } $ filteredList [ $ rowKey ] = $ temporaryList ; } if ( $ withPagination ) { $ rows [ 'data' ] = $ filteredList ; } else { $ rows = $ filteredList ; } return $ rows ; }
Filter json arrays by current language.

Due to the fact that language values are stored in JSON using mysql 5.7 (ex. {"en":"Name","fr":"Prénom"}),
this function filters rows and returns only values of the current language.

This function is only supposed to be used in Administration area, as it offers a wide list of options
for different scenarios.

@param array   $rows           The result of the query, the rows from database
@param boolean $justForInTable If it should filters columns for use in tables
@param array   $filterColumns  Show only certain columns
@param boolean $withPagination If rows are generated from the pagination class, it gets rows from pagination's array

@return array   Return filtered array
Filter json arrays by current language .