Wrong sort order
Created 5 years ago by nfskid

Made a custom module Products. It has a Products object that takes a multiple relationship field using the Highlights Object. In the view i use {% if count(product.highlights) > 0 %} {% for highlight in product.highlights %} To loop through the product highlight but i would like for it to use the sort_by field in products_highlights not products_products_highlights like it is now. Any ideas?

edster  —  5 years ago

This doesn't really make sense.

Can you elaborate? Are those tables? products_products_highlights When are you doing sorting? Did you mark your stream as sortable? Did you try sorting the records?

Can you show some code?

ryanthompson  —  5 years ago

For example count(product.highlights) .. that could be a number of things. Relationship? If so for example product.highlights.count() should suffice given you have a collection. I echo what @edster said - need moar info 😄

nfskid  —  5 years ago

So My tables are products_products , products_highlights, products_products_highlights with any number of assignments being relatable to any number of products. (many to many?)

in the controller just like your video module controller I pass products to the view:

public function view(ProductRepositoryInterface $products, $slug)
{
    if (!$product = $products->findBySlug($slug)) {
        abort(404);
    }

    // $this->breadcrumbs->add('Home', '/');
    // $this->breadcrumbs->add('Products', '/products');
    // $this->breadcrumbs->add($product->model_number, $product->route('view'));

    $this->template->set('meta_title', $product->model_number);

    return $this->view->make('anomaly.module.products::products/view', compact('product'));
}

}

in the view I

{% if count(product.highlights) > 0 %}

{% for highlight in product.highlights %}

to loop through highlights.

Lets say highlights has a name field and i have 2 entries. Speed and Count set up in that sort_order. When I use the admin side and select them in the product menu, I select the order Speed and then Count. The pivot table sort_order is correct, Speed getting sort_order 0 and Count sort_order getting 1. if i go back to the admin side and select update on the product, the sort_order on the pivot table will now flip because the multiple field sorts by alphabetical order so count will now be the first item to show in the view.

I would like to sort by products_highlights sort_order and not products_products_highlights sort_order in the view. Thanks for replying, I'm new to laravel and also new to pyrocms and trying to wrap my head around it all still.

Update:: this might be fixed by me using {% for highlight in product.highlights.sortBy('sort_order') %}

ryanthompson  —  5 years ago

Ok sounds like there may be unintended behavior in core but the last bit about .sortBy('sort_order', 'ASC') should do the trick however you might need to use get() and also manually use the relation method highlights() instead of the syntax you have.

{% for highlight in product.highlights().sortBy('sort_order').get() %}

Try that out.

nfskid  —  5 years ago

Thanks for your reply. I will try your code out and also look into relation methods as a whole. Thanks for your time.

ryanthompson  —  5 years ago

Sure thing! They'll be compiled on your base models in storage if they are done using field types 😄