Laravel属于ToMany没有返回相关数据

时间:2016-11-05 06:36:58

标签: php laravel eloquent

我对这个问题有以下关系 这是我在数据库中的数据:

ID page_id app_id 96 1 2 97 1 3 98 1 6 99 1 7

这将返回页面,但不会返回相关的apps

 $page = App\Page::find(1)->first();
 print_r($page->apps);// this has no results

这是班级地图,我有2个组合,你看到“也不行”:

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;
}

1 个答案:

答案 0 :(得分:0)

删除此功能,它会覆盖页面的->apps

public function getAppsAttribute($value){
    return $value;
}