我正在使用 Yajra 数据表 来获取我的表中的数据。
我需要显示表 accreditors
的数据,其中该表的 nonofficial_category_id
列应该显示 area_name
等价于另一个数据库表 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');
}
}
这是 indexDatatables
中的 AccreditorsController
方法,用于显示数据表:
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>
以下是数据表的当前(和错误)显示:
__________________________________________________________________________
|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 | | | |
__________________________________________________________________________
我的错误可能如下:
.blade
部分的 columns
数组中的 script
部分的语法声明有误。indexDatatables
的 AccreditorsController
方法的语法声明有误。谢谢。
答案 0 :(得分:1)
检查我的错误后,我找到了解决方案。
我需要在 addColumn()
上添加另一个 indexDatatables()
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);
}
然后我可以在 script
文件的 .blade
部分使用以下行:
// 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 的使用感到困惑。如果有人能纠正我,我将不胜感激。