After setRelation callbackColumn value should return the set value

Hi Johnny,

I have done “$crud->setRelation(‘customer_id’,‘customers’,‘customer_name’)” then used
$crud->callbackColumn(‘customer_id’, function ($value, $row) {
return “{$value}”;
});

as I want to link record to quote view, but $value returns the customer_id instead of the customer_name, so how will I be able add a quote view link and display the customer name?

Hello @lev,

Yes this is a known issue. For now what you can do is the following work-around. There is a function within the model with name getRelationData . So you can do something like this:

...
$model = new \GroceryCrud\Core\Model($database);
...
$crud->setModel($model);
...
$crud->setRelation('country', 'countries', 'nicename');

$countries = [];

// For optimization reasons we are getting the relational data only when we are at the datagrid state
if ($crud->getState() === 'Datagrid') {
    $countriesModel = $model->getRelationData(
        'countries',
        'nicename'
    );

    foreach ($countriesModel as $country) {
        $countries[$country->id] = $country->title;
    }
}

$crud->callbackColumn('country', function ($value) use ($countries) {
    return ($value && isset($countries[$value]) ? $countries[$value] : $value);
});

Let me know if the above worked for you.

Regards
Johnny

Hi @johnny ,

That worked, thank you, but one more issue the the customer_id select dropdown filter field turns in to a input search field after the changes, how do I turn the search filter field back to a select2 dropdown?

You can use the function fieldTypeSearchColumn to add a dropdown search. Just to make it optimize we can have it only for 2 states. Don’t forget to add the below if statement:

if ($crud->getState() === 'Datagrid' || $crud->getState() === 'Initial') {

So the final code will look like this:

$crud->setRelation('country', 'countries', 'nicename');

$countries = [];

// 🟢 Added an extra if statement here
if ($crud->getState() === 'Datagrid' || $crud->getState() === 'Initial') {
    $countriesModel = $model->getRelationData(
        'countries',
        'nicename'
    );

    foreach ($countriesModel as $country) {
        $countries[$country->id] = $country->title;
    }

    // 🟢 Added this extra line of code here
    $crud->fieldTypeSearchColumn('country', 'dropdown_search', $countries);
}

$crud->callbackColumn('country', function ($value) use ($countries) {
    return ($value && isset($countries[$value]) ? $countries[$value] : $value);
});

Let me know if that worked for you.

Regards
Johnny

That worked perfectly, thank you!

1 Like