簡體   English   中英

如何在 laravel 中獲取關系計數的關系?

[英]How to get relation of relation Count in laravel?

我有 laravel 型號

類別:身份證,姓名

public function posts(){
    return $this->hasMany(PostCategory::class,'category_id','id');
}

PostCategory: post_id, category_id

public function post(){
    return $this->belongsTo(Post::class,'post_id');
}

POST:id,..等等

public function solutions(){
    return $this->hasMany(PostSolution::class,'post_id','id');
 }

我需要計算一個類別下的所有帖子以及一個類別下的解決方案.. 類別和解決方案沒有直接關系,所以如何計算一個類別中的解決方案。

$categories = Category::withCount('posts')->get();

我認為使用 hasManyThrough 關系

// Category Class

public function solutions()
{
    return $this->hasManyThrough(PostSolution::class, Post::class);
}

// Then get the data in the same old manner
$categories = Category::->withCount('posts')->get();

我希望這個能幫上忙

暫無
暫無

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

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