You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OctoberCMS中News & Newsletter组件分类排序失效问题求助

Hey there! Let's figure out why your sorting isn't working in the OctoberCMS News & Newsletter component. I've run into similar issues before, so here are a few things to check and fix:

1. You might have a variable name conflict

The News & Newsletter component probably already injects a categories variable into your template by default. When you define $this['categories'] in your onStart() method, it's getting overwritten by the component's own data.

Try renaming your variable to something unique, like categoriesSorted:

function onStart() {
    $this['categoriesSorted'] = Db::table('indikator_news_categories')
        ->orderBy('created_at', 'desc')
        ->get();
}

Then update your template loop to use this new variable:

{% for category in categoriesSorted %}
    <a class="btn btn-primary" href="{{ category.url }}">{{ category.name }}</a>
{% endfor %}

2. Verify your table and field details

Double-check that:

  • The table indikator_news_categories actually has a created_at field
  • The field is of type datetime or timestamp (not a string that won't sort correctly)
  • The created_at values are populated properly for each category

You can quickly debug this by dumping the query results in your template:

{{ dump(categoriesSorted) }}

This will show you the raw data and let you confirm if the created_at values make sense and if the sorting is applied at the database level.

3. Use the component's built-in model (better practice)

Instead of writing raw database queries, leverage the component's own model to avoid issues and stay aligned with OctoberCMS conventions. Here's how:

function onStart() {
    // Get the instance of your News Categories component
    $newsComponent = $this->page->components['newsCategories'];
    
    // Use the component's category model to fetch sorted categories
    $this['categoriesSorted'] = $newsComponent->categoryModel->orderBy('created_at', 'desc')->get();
}

This way you're using the same model the component uses, so you won't run into unexpected issues with fields like url (which the component might generate for you).

4. Check the actual SQL being executed

If none of the above works, enable database query logging to see what's really happening. Go to config/database.php and set 'log' => true in your database connection settings. Then check the logs in storage/logs to see if the ORDER BY created_at DESC clause is actually present in the executed query.

Most of the time, the variable name conflict is the culprit—try that first and it should fix your sorting issue!

内容的提问来源于stack exchange,提问作者gagra33332

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:12:51