This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

zm31919
zm31919

I need to create a list in the backend that only lists records that match a relation. For example. I have a list of restaurants, those records are associated to another table of Categories. In the categories view list, I have it to where it gives the post count for how many restaurants are associated to said category. I want to be able to set that number count you see as a link so that when you click on it, it will list all restaurants associated to that category in the backend. Wordpress can do this. Trying to figure out how to do this in October.

Thanks for the assistance on this.

Vojta Svoboda
Vojta Svoboda

I've done the same in my Brands plugin. Steps:

a) Show restaurants count in the category list. In the Category model:

public function getRestaurantsCountAttribute()
{
    return $this->restaurants()->count();
}

In the categories columns.yaml:

restaurants_count:
    label: Count
    sortable: true
    clickable: false

"Clickable false" is important for creating a clickable link in the next step.

b) Create a clickable link. In controllers/Categories.php add this method:

public function listOverrideColumnValue($record, $columnName, $definition = null)
{
    if ($columnName == 'restaurants_count') {
        $link = Backend::url('my-name/plugin-name/restaurants?category=' . $record->id);
        return '<a class="btn btn-xs btn-primary" href="' . $link . '">' . $record->restaurants_count . '</a>';
    }
}

c) Create restaurants list filtering. In controllers/restaurants create file config_filter.yaml:

scopes:
  category:
    label: Category
    modelClass: MyName\MyPlugin\Models\Category
    conditions: id in (select restaurant_id from myname_myplugin_restaurant_category where category_id in (:filtered))
    nameFrom: name

Add this filtering file to the controllers/restaurants/config_list.yaml:

# List filtering
filter: config_filter.yaml

d) Prefill filtration by a GET parameter by overriding configuration saved in the session. I must admit, this is a little bit of magic/hack, but for now, I'm not sure how to do it better.

In controllers/Restaurants.php constructor:

// update filters by GET parameter
if ($id = Input::get('category')) {
    // get original filtering
    $widgetSession = Session::get('widget');
    $key = 'myname_pluginname-Restaurants-Filter-listFilter';

    // create new filter
    $filter['scope-category'] = [
        $id => Category::find($id)->name,
    ];

    // save new filter
    $encoded = base64_encode(serialize($filter));
    $withoutFiltering = !isset($widgetSession[$key]);
    if ($withoutFiltering || $widgetSession[$key] !== $encoded) {
        $widgetSession[$key] = $encoded;
        Session::put('widget', $widgetSession);
    }
}

Check my plugin for details. Hope it helps.

Last updated

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.