vendredi 20 octobre 2017

Laravel use dynamic mutators in where clause

So i have two columns - SizeX & SizeY. For front end users I use Laravel mutator

public function getSizeAttribute(){
    /**
     * Set Size
     */
    $size = $this->SizeX. " x ". $this->SizeY;
    /**
     * Return
     */
    return $size;
}

To format the sizes like this SizeX x SizeY. The column Sizes does not exists because its dynamic. Is it possible to use mutators or alternative within the Elaquent model to detect that this is a dynamic attribute, and use some sort of method to convert the SizeX x SizeY to individual columns? Once the user submits the attribute Size back to laravel application for filtering?

Edit:

Right, this is the method I'm using to retrieve filtered Items

public function scopeFilteredMaterials($query,$params = array()){ 
    /**
     * Get filters
     */
    $filters = array();
    /**
     * Extract Info
     */
    if(!empty($params) && is_array($params)){
        /**
         * Get Available Filters
         */
        $filters = $this->getAvailableFilters();

        foreach ($filters as $key => $filter){
            if(isset($params[$filter])){

                $filters[$filter] = $params[$filter];

                unset($filters[$key]);
            }else{

                unset($filters[$key]);
            }
        }
    }

    foreach ($filters as $key => $filter){

        $query->whereIn(key ,$filter);
    }

    $result = $query->get();
}

This is the filters variable which holds available filters for user to see

protected $filters = array( "Name", "url", "Size", );

I'm using the above to show the specific values to the user. Once the user selects those values I'm using the same array to check against those filters and fire the query. My problem is the Size attribute is made up of two columns which I have not problem using the following Mutator and $appends variable to automatically bring the value to the user.

/**
 * Get Size Attribute
 */
public function getSizeAttribute(){
    /**
     * Set Size
     */
    $size = $this->SizeX. " x ". $this->SizeY;
    /**
     * Return
     */
    return $size;
}

But i ca't figure out a way to convert the Size variable back to SizeX & SizeY



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire