A related list allow you to link some records to another one. For example, a customer can be linked to several invoices.
Uccello allows to displayed all related records using Related Lists.
You can use 2 types of related lists : n-1 and n-n.
Related List types
n-1
A Related List n-1 in a module, is linked to an entity field from another module. To be more explicit, let's take an example: we want to see all invoices related to a customer.
To be able to link an invoice to a customer you must create an entity field into the customer module.
// database/migrations/xxxx_xx_xx_xxxxxx_create_invoice_module.php<?phpuseIlluminate\Support\Facades\Schema;useIlluminate\Database\Schema\Blueprint;useUccello\Core\Database\Migrations\Migration;useUccello\Core\Models\Field;...classCreateInvoiceModuleextendsMigration{publicfunctionup() {...$this->createTable();$this->createTabsBlocksFields($module); }protectedfunctioncreateTable() {Schema::create('invoices',function (Blueprint $table) {... $table->unsignedInteger('customer_id')->nullable();... }); }protectedfunctioncreateTabsBlocksFields($module) {...// Field customer $field =Field::create(['module_id'=> $module->id,'block_id'=> $block->id,'name'=>'customer','uitype_id'=>uitype('entity')->id,'displaytype_id'=>displaytype('everywhere')->id,'sequence'=> $block->fields()->count(),'data'=> [ "rules"=>"required","module"=>"customer" ] // Linked to the customer module ]); }...}
We will create a Related List n-1 and link it to the field freshly created.
// database/migrations/xxxx_xx_xx_xxxxxx_create_customer_module.php<?phpuseIlluminate\Support\Facades\Schema;useIlluminate\Database\Schema\Blueprint;useUccello\Core\Database\Migrations\Migration;useUccello\Core\Models\Module;useUccello\Core\Models\Field;useUccello\Core\Models\Relatedlist;...classCreateCustomerModuleextendsMigration{publicfunctionup() {...$this->createRelatedLists($module); }protectedfunctioncreateRelatedLists($module) { $relatedModule =Module::where('name','invoice')->first();Relatedlist::create(['module_id'=> $module->id,'related_module_id'=> $relatedModule->id, 'related_field_id' => $relatedModule->fields->where('name', 'customer')->first()->id, // Retrieve the field created before
'tab_id'=>null,'label'=>'relatedlist.invoices',// Will be translated'type'=>'n-1','method'=>'getDependentList','sequence'=> $module->relatedlists()->count(),'data'=> [ 'actions'=> [ 'add' ] ] ]); }...}
Now, if you visit a customer record, a new tab will display all the invoices related to it. You can create new ones and they will be directly linked to the customer.
n-n
A Related List n-n allows to link a same record to others records from different modules. To be more explicit, let's take an example: we want to link several documents to several customers.
To be able to do this we need to create an association table:
Now, if you visit a customer record, a new tab will display all the document related to it. You can select existant documents to link to the record or create new ones.
Options
Display in a Tab or a Block?
By default, a Related List is displayed in a Tab. If you prefer, you can display it in an existant tab. The Related List will be displayed like a new block at the bottom of the page.
To do this, simply fill in tab_id with a valid tab's id from the current module.
protectedfunctioncreateRelatedLists($module){ $documentModule =Module::where('name','document')->first();Relatedlist::create(['module_id'=> $module->id,'related_module_id'=> $documentModule->id,'tab_id'=> $module->tabs->first()->id,// The Related List will be displayed as a new block in the first tab'label'=>'relatedlist.documents',// Will be translated'type'=>'n-n','method'=>'getRelatedList','sequence'=> $module->relatedlists()->count(),'data'=> [ 'actions'=> [ 'add','select' ] ] ]);}
Properties
You can create your own method for retrieving the related records. To do this you have to create 2 functions in the model in which add the related list.
myOwnMethod: You can use the name of you want
myOwnMethodCount: The same name as the method above, suffixed by Count