i'm doing "Banner Slider" and looking for how to delete column in banner_slide. Its hard to explain. So just look in my example:

So this is what i've done til now. A banner which contain 3 slide.

enter image description here

What i want is when i click in that checkbox to turn off the choose, it will count and render through HTML and then when i click Save Banner or Save and Continue Edit, it will delete columns in banner_slide table in Mysql that have checkbox turning off.

So i'm following the catalog_category_products in magento 2. And what they do is look like this: enter image description here Like you can see, it render into HTML like i said.

And when i click on Save Banner button, it will run into Controller/../Save.php and save data to banner_slide table in Mysql.

So here is the Save.php:


 * Created by PhpStorm.
 * User: Administrator
 * Date: 20/07/2018
 * Time: 4:15 CH

namespace Aht\BannerSlider\Controller\Adminhtml\Manage;

use Magento\Backend\App\Action\Context;
use Aht\BannerSlider\Model\BannerFactory;
use Aht\BannerSlider\Model\BannerSlideFactory;

class Save extends \Magento\Framework\App\Action\Action
    protected $bannerFactory;

    protected $bannerSlideFactory;

    public function __construct(
        Context $context,
        BannerFactory $bannerFactory,
        BannerSlideFactory $bannerSlideFactory
    ) {
        $this->bannerFactory = $bannerFactory;
        $this->bannerSlideFactory = $bannerSlideFactory;

     * Save action
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @return \Magento\Framework\Controller\ResultInterface
    public function execute()
        $data = $this->getRequest()->getPostValue();

        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($data) {
                $id = $this->getRequest()->getParam('id');

                $banner = $this->bannerFactory->create();

//                    lấy chuỗi json banner_slide được truyền vào từ form HTML.
                    $slide = json_decode($data['banner_slide'], true);
                    foreach($slide as $key => $value){
                        $bannerSlide = $this->bannerSlideFactory->create();

//                        check xem banner_slider này đã tồn tại hay chưa, nếu tồn tại thì không insert vào db.
                        $check = $bannerSlide->getCollection()
                        if($check = 0){
                    $data['id'] = $id;

                if ($id) {
                    $this->messageManager->addSuccessMessage(__('Update Banner Successfully.'));
                } else {
                    $this->messageManager->addSuccessMessage(__('Add Banner Successfully.'));

//              check for Save and Continue Edit Button
                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/detail', ['id' => $banner->getId(), '_current' => true]);
                } else {

                return $resultRedirect;
            catch (\Exception $e) {


Here is my banner_slide table: enter image description here

I can save into the database through foreach loop successfully the banner_slide which "checkbox" turning on, but i dont know how to do that with banner_slide which turning off. So i need an idea on how to do that...

Thanks for reading. Have a good day :)


So i've found 2 way to do this:

  1. Delete all the record in table and then insert again.
  2. Select all the record in table and compare with the json banner_slide, and what doesn't have in json will be delete.

But what if i have 1000 record in the table? So these 2 way are not really good.

1 Answer 1


Add the below code to your Grid.php:

protected function _prepareMassaction()

            'label' => __('Delete'),
            'url' => $this->getUrl('banner/*/massDelete'),
            'confirm' => __('Are you sure?')
    return $this;

Also create Delete.php controller and the below code under your module like below path:


Code for Delete.php:

namespace Vendor\Banner\Controller\Adminhtml\Banner;

class MassDelete extends \Magento\Backend\App\Action
     * @var \Magento\Framework\View\Result\PageFactory
    public function execute()

         $ids = $this->getRequest()->getParam('id');
        if (!is_array($ids) || empty($ids)) {
            $this->messageManager->addError(__('Please select product(s).'));
        } else {
            try {
                foreach ($ids as $id) {
                    $row = $this->_objectManager->get('Vendor\Banner\Model\Banner')->load($id);
                    __('A total of %1 record(s) have been deleted.', count($ids))
            } catch (\Exception $e) {

You need to change the Vendor and Module name according to your requirement.

  • hold up, so where exactly this massActionDelete should render in HTML?
    – fudu
    Commented Aug 10, 2018 at 15:25
  • if it render a Delete Action button, so i dont need it, because what i want is click the checkbox and then click "Save" button .
    – fudu
    Commented Aug 10, 2018 at 15:26
  • Just above your grid. Commented Aug 10, 2018 at 15:26
  • Mass action will show as a dropdown. from there you can select delete option and submit then all the selected banners will be deleted. Commented Aug 10, 2018 at 15:27
  • Yes i can see that, but that's not what i'm looking for .. @@ The goal here is when i click "save" , then it will delete what doesn't turn on in the checkbox.
    – fudu
    Commented Aug 10, 2018 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.