setRelationNtoN Error

Hello @johnny , how to use setRelationNtoN ? I followed the documentation and got this error when i click add :

This is my table structure :

And this is my code :

public function index()
         $crud = $this->_getGroceryCrudEnterprise();
         //$crud->where(['per_info_personal.id_payroll >= ?' => 0]);
         $crud->setSubject('Formulir PMB', 'Daftar Formulir PMB');
         $crud->columns(['nama', 'harga', 'ujian', 'keterangan', 'urutan']);
         $crud->addFields(['nama', 'harga', 'ujian', 'keterangan', 'urutan', 'program', 'inserted_by', 'inserted_time']);
         $crud->editFields(['nama', 'harga', 'ujian', 'keterangan', 'urutan', 'program', 'updated_by', 'updated_time']);
         $crud->fieldType('ujian', 'dropdown_search', [
             'Y' => 'Ya',
             'N' => 'Tidak'
         //$crud->displayAs('fk_id_projects', 'Nama Project');
         $crud->fieldType('inserted_by', 'hidden');
         $crud->fieldType('inserted_time', 'hidden');
         $crud->fieldType('updated_by', 'hidden');
         $crud->fieldType('updated_time', 'hidden');

         $crud->setRelationNtoN('program', 'akd_formulirpmb_program', 'akd_program', 'fk_id_formulir', 'fk_id_program', 'nama');

         $crud->callbackAddForm(function ($data) {
             $data['inserted_by'] = Auth::user()->id;
             $data['inserted_time'] = date('Y-m-d H:i:s');
             return $data;
         $crud->callbackEditForm(function ($data) {
             $data['updated_by'] = Auth::user()->id;
             $data['updated_time'] = date('Y-m-d H:i:s');
             return $data;
         $output = $crud->render();
         return $this->_showOutput($output);

Hello @heruprambadi ,

When is this happening? On edit or on insert? The only thing that I see suspicious is that the id of the table akd_program is not an int so not sure how this will be auto generated. Grocery CRUD is working with int and auto-increment. Maybe if you try to change the structure of your table and see if this will work?


this happening on add/insert.
I changed it to int, still not work with the same error :confused:

Ok, I’ve found the issue. You are referring to a real field ‘program’ but this is not filled and that’s why you are getting the error that there is not any default value to add to the field. Please keep in mind that the first argument is not a real field since the data are getting field at the joint table.

I hope it make sense. So for example if you remove the field “program” this should work for you.

Let me know if that worked.


so i changed my code like this :

         $crud->addFields(['nama', 'harga']);
         $crud->editFields(['nama', 'harga']);
         $crud->setRelationNtoN('program', 'akd_formulirpmb_program', 'akd_program', 'fk_id_formulir', 'fk_id_program', 'nama');

The result still like this :

There is no “program” field in there.

But if i delete this line :

$crud->addFields(['nama', 'harga']);

it works !

But deleting that line is not the solution we’re looking for right ? hahaha
is it a bug ?

By the way, i deleted “program” columns from akd_formulirpmb table

Hello @heruprambadi ,

You need to also include the ‘program’ in the fields. For example:

         $crud->addFields(['nama', 'harga', 'program']);
         $crud->editFields(['nama', 'harga', 'program']);

Let me know if that worked.


if i do that, this happens :

my code :

         $crud->addFields(['nama', 'harga', 'program']);
         $crud->editFields(['nama', 'harga', 'program']);
         $crud->setRelationNtoN('program', 'akd_formulirpmb_program', 'akd_program', 'fk_id_formulir', 'fk_id_program', 'nama');

Hello @heruprambadi ,

I can reproduce the issue that you are referring to with the exact same error:

<br/><b>Notice</b>:  Undefined property: stdClass::$defaultValue in <b>

Very weird! I will try to fix that asap and release a new version with it. I will keep you updated.


This is now fixed at the latest version but since the next released may be delayed in order to fix the issue locally go to vendor/grocery-crud/enterprise/src/GroceryCrud/Core/State/AddFormState.php at around line 112 and change the line:

if (array_key_exists($fieldName, $fieldTypes) && $fieldTypes[$fieldName]->defaultValue !== null) {


            if (
                array_key_exists($fieldName, $fieldTypes)
                && property_exists($fieldTypes[$fieldName], 'defaultValue')
                && $fieldTypes[$fieldName]->defaultValue !== null
            ) {

Let me know if that fixed the issue.


Yeah, this solution work for me @johnny , thank you :smiley:

1 Like

this fixed my issue to
thanks @johnny