我正在尝试使用Laravel中的Eloquent从多对多关系中获取数据。每次执行此操作都会收到一个Data Missing
错误。
我已经尝试了以下方法来获取数据:
return $workers = Worker::first()->workers_sub_categories;
return $workers = Worker::with('workers_sub_categories')->get();
这两种情况都会导致DataMissing
错误。
Worker
模型:
protected $table = "workers";
protected $primaryKey = 'workers_id';
protected $fillable = [
'workers_username', 'email', 'password', 'FK_cities_id',
'FK_packages_id', 'workers_points', 'jwt', 'workers_img',
'workers_lat', 'workers_lang', 'workers_phone_number',
'workers_businessregister', 'workers_desc', 'remember_token',
'workers_status', 'workers_verified', 'step', 'expiry_date', 'workers_created_at', 'workers_updated_at'
];
const CREATED_AT = 'workers_created_at';
const UPDATED_AT = 'workers_updated_at';
public function related_orders()
{
return $this->hasMany('App\Models\Order', 'FK_workers_id', 'workers_id');
}
public function related_offers()
{
return $this->hasMany('App\Models\Offer', 'FK_workers_id', 'workers_id');
}
public function related_comments()
{
return $this->hasMany('App\Models\Comment', 'FK_workers_id', 'workers_id');
}
public function worker_orders()
{
return $this->belongsToMany('App\Models\Order', 'orders_workers_status', 'FK_workers_id',
'FK_orders_id')->withPivot('FK_categories_id', 'FK_sub_categories_id');
}
public function related_rates()
{
return $this->hasMany('App\Models\Rate', 'FK_workers_id', 'workers_id');
}
public function related_city()
{
return $this->belongsTo('App\Models\City', 'FK_cities_id', 'cities_id');
}
public function workers_sub_categories()
{
return $this->belongsToMany('App\Models\SubCategory', 'workers_services', 'FK_workers_id',
'FK_sub_categories_id');
}
public function setWorkersImgAttribute($file)
{
if ($file) {
$fileName = $this->createFileName($file);
$this->originalImage($file, $fileName, 'worker/profile/original');
$this->mediumImage($file, $fileName, 150, 150, 'worker/profile/meduim');
$this->thumbImage($file, $fileName, 70, 70, 'worker/profile/thumbnail');
$this->attributes['workers_img'] = $fileName;
}
}
public function setWorkersBusinessRegisterAttribute($file)
{
if ($file) {
$fileName = $this->createFileName($file);
$this->originalImage($file, $fileName, 'worker/business/original');
$this->mediumImage($file, $fileName, 150, 150, 'worker/business/meduim');
$this->thumbImage($file, $fileName, 70, 70, 'worker/business/thumbnail');
$this->attributes['workers_businessregister'] = $fileName;
}
}
SubCategories
模型:
protected $table = "sub_categories";
protected $primaryKey = 'sub_categories_id';
protected $fillable = [
'sub_categories_img', 'sub_categories_status', 'FK_categories_id',
];
const CREATED_AT = 'sub_categories_created_at';
const UPDATED_AT = 'sub_categories_updated_at';
public function workers_categories()
{
return $this->belongsToMany('App\Models\Worker', 'workers_services', 'FK_sub_categories_id', 'FK_workers_id');
}
protected function getArrayableAttributes()
{
foreach ($this->attributes as $key => $value) {
if (is_null($value)) {
$this->attributes[$key] = '';
}
}
return $this->getArrayableItems($this->attributes);
}
public function related_category()
{
return $this->belongsTo('App\Models\Category', 'FK_categories_id', 'categories_id');
}
public function getSubCategoriesImgAttribute()
{
return SiteImages_path('subcategory').'/thumbnail/'.$this->attributes['sub_categories_img'];
}
数据库迁移:
$table->bigIncrements('workers_services_id');
$table->bigInteger('FK_workers_id')->unsigned();
$table->bigInteger('FK_sub_categories_id')->unsigned();
$table->foreign('FK_workers_id')->references('workers_id')
->on('workers')
->onDelete('cascade');
$table->foreign('FK_sub_categories_id')->references('sub_categories_id')
->on('sub_categories')
->onDelete('cascade');
应该将所有带支点的工人退回。