【发布时间】:2016-03-21 12:18:37
【问题描述】:
我正在使用 yajra/laravel-datatables 和 dimsav/laravel-translatable 创建角色表。
数据表结构如下。 角色表迁移:
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->softDeletes();
$table->timestamps();
});
角色转换表迁移:
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');
});
现在我正在控制器上执行此操作...
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');
...这在视图中(数据表初始化和常规设置在一个通用 js 文件中完成,特定设置作为 HTML 属性传递)...
<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>
它有效,但我对查询中的所有这些连接感到不舒服,我想做类似的事情
$roles = Role::with('translation')->select(['roles.id', 'role_translations.name', 'roles.created_at', 'roles.updated_at'])
但我没有运气。
【问题讨论】: