Laravel BAP, MulitCRM — Creating Relations — Part 2—ManyToOne
Laravel BAP is a Modular Backend Application Platform build on top of Laravel 5.6, Twitter Bootstrap and SCSS. The software contains over 20 core features, module generator, relations in tabs. BAP is Easy to extend, customize, developer, can build any back-office application with short time.
You can read more about platforms here:
Tutorial
In this tutorial, I'm going to show how to extend application and create relations between modules.
ManyToOne Relation
Example ManyToOne relation in CRM in when in single Ticket you can choose from many existing Accounts and bind one inside Account record in Ticket.
In application, many to many relations are represented by “Picker field”. When user will click on search icon application will show modal with list of available records to bind.
Creating ManyToOne Relation
In this example, we are going to add Account Picker in Ticket.
1.Creating migration
run artisan command
php artisan module:make-migration update_ticket_add_account Tickets
This function will create new migration in Tickets module.
Update migration file.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateTicketsAddAccount extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tickets', function (Blueprint $table) {
$table->integer('account_id')->unsigned()->nullable();
$table->foreign('account_id')->references('id')->on('contacts');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tickets', function (Blueprint $table) {
$table->dropColumn('account_id');
});
}
}
Explanation
We are adding new field “account_id” inside the tickets table. The new field is a relation with foreign key to accounts table.
2. Update Ticket Entity
Add new field to $fillable
public $fillable = [
'name',
'due_date',
'ticket_priority_id',
'ticket_status_id',
'ticket_severity_id',
'ticket_category_id',
'description',
'resolution',
'notes',
'contact_id',
'account_id',
'parent_id'
];
Add new relation
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function account()
{
return $this->belongsTo(Account::class);
}
3.Update TicketController
in $showFields add this
'account_id' => [
'type' => 'manyToOne',
'relation' => 'account',
'column' => 'name',
'dont_translate' => true
],
Explanation:
type: manyToOne
relation: account (same name as in Entity)
column: column from “account” that you want to display in this field.
dont_translate: true — this field shouldn’t be translated.
4.Update Form
Find TicketForm class and update.
$this->add('account_id', 'manyToOne', [
'search_route' => route('contacts.contacts.index', ['mode'=>'modal']),
'relation' => 'account',
'relation_field' => 'name',
'model' => $this->model,
'attr' => ['class' => 'form-control manyToOne'],
'label' => trans('core::core.form.account_id'),
'empty_value' => trans('core::core.empty_select')
]);
5.Update translations
Find module translation file and update variables.
/Tickets/Resources/lang/en/tickets.php
And that's all.