Php Laravel 8数据表加载不工作(Yajra)
我正在使用Yajra Datatables来获取表中的数据 我需要显示表Php Laravel 8数据表加载不工作(Yajra),php,laravel,laravel-blade,yajra-datatable,Php,Laravel,Laravel Blade,Yajra Datatable,我正在使用Yajra Datatables来获取表中的数据 我需要显示表认证机构的数据,其中所述表的非官方类别id列应显示另一个数据库表非官方类别的区域名称 以下是认证机构模型,显示其与非官方类别模型的关系: <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Da
认证机构的数据,其中所述表的非官方类别id
列应显示另一个数据库表非官方类别的区域名称
以下是认证机构
模型,显示其与非官方类别
模型的关系:
<?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');
}
}
<?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);
}
}
以下是非官方类别
模型,显示了其与认证机构的关系
模型:
<?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');
}
}
<?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);
}
}
以下是数据表的当前(和错误)显示:
__________________________________________________________________________
|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
部分的语法声明错误
- 对于
AuthoritorsController
,在indexDatatables
方法上的语法声明错误
谢谢。在检查我的错误后,我找到了解决办法
我需要在indexDatatables()
上添加另一个addColumn()
API:
然后我可以在.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
现在,我仍然可能在关系声明上出错,因为我仍然对belongsTo()/belongsToMany()
API的用法感到困惑。如果有人能纠正我,我将不胜感激
// 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