Laravel 8 数据表预加载不工作(Yajra)
Laravel 8 Datatables Eager Loading Not Working (Yajra)
我正在使用 Yajra Datatables 来获取我的 table.
中的数据
我需要显示 table accreditors
的数据,其中 table 的 nonofficial_category_id
列应该显示它的 area_name
等价物来自另一个数据库 table non_officials_categories
.
这里是 Accreditors
模型显示它与 NonOfficialsCategories
模型的关系:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Accreditors extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = "accreditors";
protected $fillable = [
'name',
'official_category_id',
'subcategory',
'position',
'photo',
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
public function nonOfficialsCategories()
{
return $this->belongsTo(NonOfficialsCategories::class, 'nonofficial_category_id');
}
}
这是 AccreditorsController
中的方法 indexDatatables
,它会显示数据table:
namespace App\Http\Controllers;
use App\Models\Accreditors;
use App\Models\NonOfficialsCategories;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class AccreditorsController extends Controller
{
public function indexDatatables()
{
$model = Accreditors::with('nonOfficialsCategories')->select('accreditors.*');
if (request()->ajax()) {
return datatables()->of($model)
->addColumn('action', function ($data) {
return $this->getActionColumn($data);
})
->rawColumns(['action'])
->make(true);
}
$categoryCount = NonOfficialsCategories::count();
return view('personnels.nonofficials.accreditors.datatables.datatables')->with('categoryCount', $categoryCount);
}
}
这里是 NonOfficialsCategories
模型显示它与 Accreditors
模型的关系:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class NonOfficialsCategories extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = "non_officials_categories";
protected $fillable = [
'area_number',
'area_name',
'subcategory',
'is_accreditor_head',
'is_taskforce_head',
'is_auditor',
'use_counter',
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
public function taskforces()
{
return $this->has(TaskForces::class);
}
public function accreditors()
{
return $this->has(Accreditors::class);
}
}
这里是 .blade
认证者文件的摘录:
<div class="container-fluid d-flex justify-content-center">
<div class="row">
<table class="poppins-normal text-md" id="accreditorsDatatables">
<thead>
<tr>
{{-- <th class="text-white">Id</th> --}}
<th class="text-white">Name</th>
<th class="text-white">Area Name</th>
<th class="text-white">Subcategory</th>
<th class="text-white">Position</th>
<th class="text-white">Actions</th>
</tr>
</thead>
</table>
</div>
</div>
<script>
$(document).ready( function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#accreditorsDatatables').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route("accreditors-datatables") !!}',
columns: [
// {data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'nonofficial_category_id', name: 'nonOfficialsCategories.area_name'},
{data: 'subcategory', name: 'subcategory'},
{data: 'position', name: 'position'},
{data: 'action', name: 'action', orderable: false},
]
});
});
</script>
这是数据table的当前(和错误)显示:
__________________________________________________________________________
|name |area name |subcategory |position |actions |
__________________________________________________________________________
| |1 //wrong | | | |
__________________________________________________________________________
| |2 //wrong | | | |
__________________________________________________________________________
| |3 //wrong | | | |
__________________________________________________________________________
其中显示应该是:
__________________________________________________________________________
|name |area name |subcategory |position |actions |
__________________________________________________________________________
| |Area I //right | | | |
__________________________________________________________________________
| |Area II //right | | | |
__________________________________________________________________________
| |Area III //right | | | |
__________________________________________________________________________
我的错误可能如下:
- 模型文件的 eloquent 关系错误。
script
部分的 columns
数组中 .blade
部分的语法声明错误。
AccreditorsController
的 indexDatatables
方法语法声明错误。
谢谢。
在检查了我的错误后,我找到了解决方案。
我需要在 indexDatatables()
上添加另一个 addColumn()
API:
public function indexDatatables()
{
$model = Accreditors::with('nonOfficialsCategories')->select('accreditors.*');
if (request()->ajax()) {
return datatables()->of($model)
->addColumn('action', function($data) {
return $this->getActionColumn($data);
})
->addColumn('area_name', function (Accreditors $accreditors) { // the added column
return $accreditors->nonOfficialsCategories->area_name;
})
->rawColumns(['action'])
->make(true);
}
$categoryCount = NonOfficialsCategories::count();
return view('personnels.nonofficials.accreditors.datatables.datatables')->with('categoryCount', $categoryCount);
}
然后我可以在 .blade
文件的 script
部分使用以下行:
// format {data: 'name_parameter_in_add_column', name: 'relationshipName.column_name_to_be_searched'},
{data: 'area_name', name: 'nonOfficialsCategories.nonofficial_category_id'},
// the 'area_name' in the script section should be the same name declared in the first parameter of the "addColumn()" API
Here is the link for the documentation reference.
现在,我在关系声明上仍然可能是错误的,因为我仍然对 belongsTo()/belongsToMany()
API 的用法感到困惑。如果有人能指正我,我将不胜感激。
我正在使用 Yajra Datatables 来获取我的 table.
中的数据我需要显示 table accreditors
的数据,其中 table 的 nonofficial_category_id
列应该显示它的 area_name
等价物来自另一个数据库 table non_officials_categories
.
这里是 Accreditors
模型显示它与 NonOfficialsCategories
模型的关系:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Accreditors extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = "accreditors";
protected $fillable = [
'name',
'official_category_id',
'subcategory',
'position',
'photo',
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
public function nonOfficialsCategories()
{
return $this->belongsTo(NonOfficialsCategories::class, 'nonofficial_category_id');
}
}
这是 AccreditorsController
中的方法 indexDatatables
,它会显示数据table:
namespace App\Http\Controllers;
use App\Models\Accreditors;
use App\Models\NonOfficialsCategories;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class AccreditorsController extends Controller
{
public function indexDatatables()
{
$model = Accreditors::with('nonOfficialsCategories')->select('accreditors.*');
if (request()->ajax()) {
return datatables()->of($model)
->addColumn('action', function ($data) {
return $this->getActionColumn($data);
})
->rawColumns(['action'])
->make(true);
}
$categoryCount = NonOfficialsCategories::count();
return view('personnels.nonofficials.accreditors.datatables.datatables')->with('categoryCount', $categoryCount);
}
}
这里是 NonOfficialsCategories
模型显示它与 Accreditors
模型的关系:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class NonOfficialsCategories extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = "non_officials_categories";
protected $fillable = [
'area_number',
'area_name',
'subcategory',
'is_accreditor_head',
'is_taskforce_head',
'is_auditor',
'use_counter',
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
public function taskforces()
{
return $this->has(TaskForces::class);
}
public function accreditors()
{
return $this->has(Accreditors::class);
}
}
这里是 .blade
认证者文件的摘录:
<div class="container-fluid d-flex justify-content-center">
<div class="row">
<table class="poppins-normal text-md" id="accreditorsDatatables">
<thead>
<tr>
{{-- <th class="text-white">Id</th> --}}
<th class="text-white">Name</th>
<th class="text-white">Area Name</th>
<th class="text-white">Subcategory</th>
<th class="text-white">Position</th>
<th class="text-white">Actions</th>
</tr>
</thead>
</table>
</div>
</div>
<script>
$(document).ready( function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('#accreditorsDatatables').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route("accreditors-datatables") !!}',
columns: [
// {data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'nonofficial_category_id', name: 'nonOfficialsCategories.area_name'},
{data: 'subcategory', name: 'subcategory'},
{data: 'position', name: 'position'},
{data: 'action', name: 'action', orderable: false},
]
});
});
</script>
这是数据table的当前(和错误)显示:
__________________________________________________________________________
|name |area name |subcategory |position |actions |
__________________________________________________________________________
| |1 //wrong | | | |
__________________________________________________________________________
| |2 //wrong | | | |
__________________________________________________________________________
| |3 //wrong | | | |
__________________________________________________________________________
其中显示应该是:
__________________________________________________________________________
|name |area name |subcategory |position |actions |
__________________________________________________________________________
| |Area I //right | | | |
__________________________________________________________________________
| |Area II //right | | | |
__________________________________________________________________________
| |Area III //right | | | |
__________________________________________________________________________
我的错误可能如下:
- 模型文件的 eloquent 关系错误。
script
部分的columns
数组中.blade
部分的语法声明错误。AccreditorsController
的indexDatatables
方法语法声明错误。
谢谢。
在检查了我的错误后,我找到了解决方案。
我需要在 indexDatatables()
上添加另一个 addColumn()
API:
public function indexDatatables()
{
$model = Accreditors::with('nonOfficialsCategories')->select('accreditors.*');
if (request()->ajax()) {
return datatables()->of($model)
->addColumn('action', function($data) {
return $this->getActionColumn($data);
})
->addColumn('area_name', function (Accreditors $accreditors) { // the added column
return $accreditors->nonOfficialsCategories->area_name;
})
->rawColumns(['action'])
->make(true);
}
$categoryCount = NonOfficialsCategories::count();
return view('personnels.nonofficials.accreditors.datatables.datatables')->with('categoryCount', $categoryCount);
}
然后我可以在 .blade
文件的 script
部分使用以下行:
// format {data: 'name_parameter_in_add_column', name: 'relationshipName.column_name_to_be_searched'},
{data: 'area_name', name: 'nonOfficialsCategories.nonofficial_category_id'},
// the 'area_name' in the script section should be the same name declared in the first parameter of the "addColumn()" API
Here is the link for the documentation reference.
现在,我在关系声明上仍然可能是错误的,因为我仍然对 belongsTo()/belongsToMany()
API 的用法感到困惑。如果有人能指正我,我将不胜感激。