【问题标题】:Laravel 5.2 Model RelationshipsLaravel 5.2 模型关系
【发布时间】:2016-11-01 00:42:34
【问题描述】:

我是 Laravel 新手,有一个关系问题。

目标是在news.page_id = page.id AND page.pagetype_id = pagetype.id WHERE pagetype.component = news AND page.app_id = 1 获取所有新闻

class News extends Model
{
    protected $table = 'news';
    protected $fillable = ['page_id', 'title', 'description', 'active', 'created_at', 'updated_at'];
}

class Page extends Model
{
    protected $table = 'pages';
    protected $fillable = ['app_id', 'unique_id', 'pagetype_id', 'title', 'picture_url', 'short_description', 'description', 'valid_since', 'valid_until', 'extras', 'active', 'created_at', 'updated_at'];

    public function pagetype() {
        return $this->belongsTo('App\Models\PageType', 'pagetype_id');
    }
}


class PageType extends Model
{
    protected $table = 'pagetypes';
    protected $fillable = ['pagetype', 'component', 'active', 'created_at', 'updated_at'];

    public function page() {
      return  $this->belongsToMany('App\Models\Page', 'pagetypes', 'id', 'id');
    }
}

// now i need   All News Items where page.pagetype_id = pagetypes.id and patchtypes.component = news

// First Attempts are

Page::whereHas('pagetype', function ($q) {
            $q->where('component', 'news');
        })->where(['app_id' => 1])->get();

// result is all Pages which has the proper component news. 

这是我已经尝试过的,但在我的尝试中,我只会收到正确的页面,但当然不会收到新闻。

我的“当前”解决方案是获取所有页面,然后遍历News::where('page_id', $myPageId)。但我很确定它有可能获得适当的关系以获取新闻。

我不能做任何其他模型,因为还有许多不同的页面类型和组件。

到目前为止谢谢。

【问题讨论】:

  • 我不确定这是否是正确的方法,但它看起来可行: News::whereHas('page', function ($q) { $q->where('app_id', 1 ); })->with('page')->get();新闻模型: public function page() { return $this->hasMany('App\Models\Page', 'id', 'page_id'); }
  • 你也可以像这样使用leftJoin:News::leftJoin('page', function($q) { $q->on('news.page_id', '=', 'page.id'); })->leftJoin('pagetype', function($q) { $q->on('page.pagetype_id', '=', 'pagetype.id'); })->where('pagetype.component', 1)->where('page.app_id', 1);
  • 你能发布你的架构吗
  • 检查可填充的,因为它们几乎是整个架构(除了具有自动增量的 ID,唯一)

标签: php sql laravel laravel-5 relationship


【解决方案1】:

新闻模型需要添加关系函数。

public function pages() {
    return $this->belongsTo('App\Models\Page');
}

并通过News模型调用它。

News::with('pages')->where('app_id',1);

【讨论】:

    【解决方案2】:

    首先我认为你错了PageType关系

    class PageType extends Model
    {
        protected $table = 'pagetypes';
        protected $fillable = ['pagetype', 'component', 'active', 'created_at', 'updated_at'];
    
        public function page() {
          return  $this->hasMany('App\Models\Page');
          // if i understood you correctly you haven't got any pivot table
        }
    }
    

    那么你应该像这样链接你的NewsPage

    News.php

    class News extends Model
    {
        protected $table = 'news';
        protected $fillable = ['page_id', 'title', 'description', 'active', 'created_at', 'updated_at'];
        public function page() {
          return  $this->belongsTo('App\Models\Page');
        }
    }
    

    页面.php

    class Page extends Model
    {
        protected $table = 'pages';
        protected $fillable = ['app_id', 'unique_id', 'pagetype_id', 'title', 'picture_url', 'short_description', 'description', 'valid_since', 'valid_until', 'extras', 'active', 'created_at', 'updated_at'];
    
        public function pagetype() {
            return $this->belongsTo('App\Models\PageType');
        }
    
        public function news() {
            return $this->hasMany('App\Models\News');
        }
    }
    

    那么你就可以实现你的目标了

    News::whereHas('page', function($q) use($appId) {
        $q->where('app_id',$appId);
    })->whereHas('page.pagetype', function($q) {
        $q->where('component', 'news');
    })->get();
    

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2016-06-23
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多