Skip to content

In my data-provider addFieldtoFilter() is not working with url param

I have made a grid in admin that is supposed to return a filtered collection based on a URL parameter lesson_id.

DataProvider:

<?php

namespace VendorNameModuleNameUiDataProviderParticipants;

use MagentoFrameworkAppRequestHttp;
use VendorNameModuleNameModelResourceModelParticipantsCollectionFactory;

class DataProvider extends MagentoUiDataProviderAbstractDataProvider
{

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        Http $request,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->request = $request;
        $this->collection = $collectionFactory->create();
    }

    public function getData(){
        $lessonId = $this->request->getParam('lesson_id');

        if (!$this->getCollection()->isLoaded()) {
            $this->getCollection()->addFieldToFilter('lesson_id',['eq' => $lessonId]);
            $this->getCollection()->load();
        }

        return $this->getCollection()->toArray();
    }
}

When i var_dump() the Array with lesson_id=7 from the URL it returns:

array(2) { ["totalRecords"]=> int(1) ["items"]=> array(1) { [0]=> array(7) { ["participant_id"]=> string(1) "2" ["lesson_id"]=> string(1) "7" ["customer_id"]=> string(2) "98" ["name"]=> string(8) "test 420" ["completed"]=> string(1) "1" ["created_at"]=> string(19) "2022-10-06 08:41:52" ["updated_at"]=> string(19) "2022-10-07 10:28:35" } } }

However the grid does not display the entry.
But now i replace the lesson_id in addFieldToFilter with a normal int or string like so:

$this->getCollection()->addFieldToFilter('lesson_id',7);

The var_dump() shows the exact same array but this time the entry does get displayed in grid without problems.
So i have two questions, the first is what causes this and the second is there a workaround?