簡體   English   中英

Laravel 5.2模型關系

[英]Laravel 5.2 Model Relationships

我是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) 但我非常確定有可能找到一個合適的關系來獲取新聞。

我不能做任何其他模型,因為有許多不同的頁面類型和組件。

謝謝到目前為止。

您需要為新聞模型添加關系功能。

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

並通過News模型調用它。

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

首先,我認為你的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');
    }
}

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM