Making-Websites-With-October-CMS icon indicating copy to clipboard operation
Making-Websites-With-October-CMS copied to clipboard

Ajax Frontend Filter with multiple Relations

Open nnnext opened this issue 2 years ago • 2 comments

Expected behavior

Dear October Community, i followed the tutorial by Ivan to make a ajax frontend filter. But all i can find is with only one relation and other infos are directly from the Movies / Vehicles entry. In my filter i try to use multiple relations like categories, manufacturers, locations etc… i use a scopeListFrontEnd query with options in my Vehicle Model.

Actual behavior

Actually the filter only works when all Dropdowns are selected but i want to filter step by step to reduce the results shown in the vehicle partial. For example: When Manufacturer is Chosen all entries with the manufacturer should be displayed. When only Tonnagen, Categories or Locations is selected all Tonnage or location entries should be displayed.

I think my mistake is in the Vehicle Model query or the prepareVars but cant figure out exactly where the problem is. I hope anyone can help me to get this filter work.

Best regards and thanks for all your hints and help.

Reproduce steps

Model: (tried it already with NULL and not NULL) ` public function scopeListFrontEnd($query, $options = []){ extract(array_merge([ 'page' => 1, 'perPage' => 999, 'sort' => 'sort_order', 'manufacturers' => null, 'categories' => null, 'industries' => null, 'tonnagen' => null, 'locations' => null, 'rooffinishes' => null, ], $options));

    /*
     * Manufacturers
     */
    if($manufacturers !== null ){

        if(!is_array($manufacturers)){
            $manufacturers = [$manufacturers];
        }

        foreach ($manufacturers as $manufacturer){
            $query->whereHas('manufacturers', function($q) use ($manufacturer){
                $q->where('slug', '=', $manufacturer);
            });
        }
    }
    

    /*
     * Categories
     */
    if($categories){

        if(!is_array($categories)){
            $categories = [$categories];
        }

        foreach ($categories as $category){
            $query->whereHas('categories', function($q) use ($category){
                $q->where('slug', '=', $category);
            });
        }
    }
    /*
     * Tonnagen
     */
    if($tonnagen){

        if(!is_array($tonnagen)){
            $tonnagen = [$tonnagen];
        }

        foreach ($tonnagen as $tonnage){
            $query->whereHas('tonnagen', function($q) use ($tonnage){
                $q->where('id', '=', $tonnage);
            });
        }
    }

`

PAGE: ` function onStart() { $this->prepareVars(); } function onFilterVehicles() { $this->prepareVars(); }

function prepareVars() { $options = post('Filter', []);

    $this['vehicles'] = Vehicle::listFrontEnd($options);
    $this['manufacturers'] = Manufacturer::all();
    $this['vehicletypes'] = VehicleType::all();
    $this['tonnagen'] = Tonnage::all();
    $this['categories'] = Category::all();
    $this['locations'] = Location::all();
    $this['rooffinishes'] = RoofFinish::all();
    $this['industries'] = Branche::all();
    $this['pages'] = $this['vehicles']->lastPage();
}

`

FORM in HTM: `

Many code with dropdown fields that all are displaying the correct database entries of all relations.
{% partial 'vehicles/vehicles' %}
`
October build

v3.1

nnnext avatar Jan 17 '23 17:01 nnnext

Hi. Better ask your question on official October forums. There is no real community here, and I havnen't touched October in years. You got a better chance of getting your question answered on Octobers official channels (Slack, October Forums).

ivandoric avatar Jan 18 '23 09:01 ivandoric

Thank you Ivan, i already tried that and hoped to find the answer from one of your followers or yourselfe. Here is the question in the new October Forum

i thought its maybe a laravel problem. not special to octobercms

nnnext avatar Jan 18 '23 09:01 nnnext