I am using yajra/laravel-datatables and dimsav/laravel-translatable to create a table of roles.
The datatable structure is as follows. Roles table migration:
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->softDeletes();
$table->timestamps();
});
Role translations table migration:
Schema::create('role_translations', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned();
$table->string('name')->index();
$table->string('locale')->index();
$table->unique(['role_id', 'name', 'locale']);
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
Now I am doing this at the controller...
public function indexData()
{
$roles = Role::join('role_translations', 'roles.id', '=', 'role_translations.role_id')
->select(['roles.id', 'role_translations.name', 'roles.created_at', 'roles.updated_at'])
->groupBy('roles.id');
...and this at the view (Datatable initialization and general settings are done in a common js file and particular settings are passed as HTML attributes)...
<table class="table table-striped table-bordered" data-table data-ajax="{{ url('/admin/role/index-data') }}" data-responsive="true">
<thead>
<tr>
<th data-priority="1">{{ trans('messages.name') }}</th>
<th>{{ trans('messages.created') }}</th>
<th>{{ trans('messages.modified') }}</th>
<th data-priority="1" data-sortable="false" data-class-name="actions">{{ trans('messages.actions') }}</th>
</tr>
</thead>
</table>
It works, but I feel uncomfortable with having all those joins on the query and I would like to do something like
$roles = Role::with('translation')->select(['roles.id', 'role_translations.name', 'roles.created_at', 'roles.updated_at'])
But I am not having any luck.