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

planetadeleste
planetadeleste

This is a small guide for newbies like me ;)
I don't know if this is the best way to make custom lists, but work ok for me.
In my case, the Model name is Zone and the controller is Zones

How to creating a trashed list

Config files

Duplicate the file config_list.yaml to config_trashed.yaml and change the necesary settings. In my case, only the toolbar buttons config.

Controller code

Them add the $listConfig like this:

public $listConfig = ['list' => 'config_list.yaml', 'trashed' => 'config_trashed.yaml'];

Add trashed action

public function trashed() {
    $this->pageTitle = trans('author.plugin::lang.global.label.trashed');
    $this->bodyClass = 'slim-container';
    $this->makeLists();
}

Add onRestore and onDelete events

  public function trashed_onRestore() {
    if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
      $restored = [];
      foreach ($checkedIds as $itemId) {
        // Thanks @alxy
        if (!$item = Zone::onlyTrashed()->where('id', $itemId)) {
          continue;
        }
        $restored[$itemId] = $item->restore();
      }
      Flash::success(trans('author.plugin::lang.global.success.restore'));
    }
    return $this->listRefresh('trashed');
  }

  public function trashed_onDelete()
  {
    if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
      foreach ($checkedIds as $itemId) {
        // Thanks @alxy
        if (!$item = Zone::onlyTrashed()->where('id', $itemId)) {
          continue;
        }
        $item->forceDelete();
      }
      Flash::success(trans('author.plugin::lang.global.success.delete'));
    }
    return $this->listRefresh('trashed');
  }

View

Make a trashed.htm file with this:

<?= $this->listRender('trashed') ?>

Testing

Go to /backend/{author}/{plugin_name}/{controller}/trashed and test id.

Last updated

d.negativa
d.negativa

Thank you for sharing this

alxy
alxy

You should consider rewrting this part:

 if (!$item = Zone::onlyTrashed()->find($itemId)) {
          continue;
        }

It exectues one query per loop. You could use Zone::onlyTrashed()->whereIn('id', $itemIds)->get() instead.

planetadeleste
planetadeleste

Thanks @alxy. Fixed the code.

alxy
alxy

Heads up: my code returns a collection, not a single model instance. Having said this, you need another slight change to your code for the delete and restore methods. Collection::each() could be used to apply a function on all items of the collection.

Last updated

planetadeleste
planetadeleste

@alxy After change my code and make some tests, I see the problem. I don't use Collection::each(), instead, change the lines to this:

Zone::onlyTrashed()->where('id', $itemId)

Can you write a sample code using Collection::each() for this case ?

Thanks

1-6 of 6

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