[英]Laravel belongsToMany not returning the related data
I have the following relationship bellow the question. 关于这个问题,我有以下关系。
This is the data I have in the database: 这是我在数据库中的数据:
ID page_id app_id 96 1 2 97 1 3 98 1 6 99 1 7
this will return the page, but NOT the related apps
这将返回页面,但不返回相关的
apps
$page = App\Page::find(1)->first();
print_r($page->apps);// this has no results
this is the class maps, I have 2 combinations inside where you see "also not working": 这是类地图,我有两个组合,你看到“也没有工作”:
class Page extends Model
{
protected $table = "pages";
//protected $appends = array('apps');//also not working
protected $with = array('apps');//
public function apps(){
return $this->belongsToMany('App\App','page_apps','page_id','app_id')->withPivot('page_id');
//return $this->belongsToMany('App\App','page_apps','page_id','app_id');//also not working
}
public function getAppsAttribute($value){
return $value;
}
}
class App extends Model
{
protected $table = "apps";
public $timestamps = true;
}
class PageApps extends Model
{
protected $table = "page_apps";
public $timestamps = true;
}
Remove this function, it overrides the ->apps
for page 删除此功能,它会覆盖
->apps
for page
public function getAppsAttribute($value){
return $value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.