Laravel查询-获取所有在管理员用户文章上发布优惠的用户


Laravel query - get all users who post offer on admin user article

所以当我写标题时,我需要让所有向管理员用户文章发布报价的用户。。。所以我写:

public function userbase()
{
    return Offer::select('users.*')
                ->where('users.admin', 9)
                ->join('articles','articles.id','=','offers.article_id')
                ->join('users','users.id','=','articles.user_id')
                ->where('articles.user_id', Auth::user()->id)
                ->orderBy('offers.created_at', 'desc')
                ->groupBy('users.id')
                ->get(); 
}

但我只是[](这不是真的)。。。

我也尝试过:

public function userbase()
{
     $user_ids = Article::where('user_id', Auth::id())
                ->join('offers')->on('offers.article_id', '=', 'articles.id')
                ->join('users')->on('offers.user_id', '=', 'users.id')
                ->select('users.id')
                ->distinct()
                ->get();
  $users = User::whereIn('id', $user_ids);
  return $users;
}

但我得到:Illuminate''Database''Query''Builder::join()缺少参数2

  1. 尝试是:

    公共函数userbase(){$articles=Auth::user()->articles()->get();foreach($articles作为$article){

     $offers = Offer::where('article_id', $article['id'])->orderBy('created_at', 'desc')->get();
    }
    

    foreach($offers as$offer){

     $users = User::where('id', $offer['user_id'])->orderBy('created_at', 'desc')->get();
    }
    return $users;
    

    }但是即兴表演不起作用。。。

现在我真的不知道下一步该怎么办。。。请帮忙。。。

也是我的模特:文章:

public function offers() {
  return $this->hasMany('App'Offer');
}

报价:

public function user() {
  return $this->belongsTo('App'User');
}
public function article() {
  return $this->belongsTo('App'Article');
}

所以我如何才能让用户谁张贴优惠给其他用户(管理员)文章。。。

尝试将此函数放入用户模型中:

public function scopeBase($query){
    $sql = <<<SQL
id IN(
    SELECT DISTINCT users.id FROM articles
    INNER JOIN offers
        ON articles.id=offers.article_id
    INNER JOIN users 
        ON users.id=articles.user_id
    WHERE articles.user_id=? AND users.admin=?
)   
SQL;
    return $query->whereRaw($sql, [$this->id, 9]);
}

然后在控制器中让用户使用以下语法:

class MyController extends Controller{
    public function example(){
        $users = User::base()->get();
        //....
    }
}

但是,如果你能更好地解释表格结构,你会很感激的。

你试过这个吗?

foreach (User::all() as $user) {
foreach ($user->offers as $offer) {
        if ($offer->article->user->isAdmin()) 
            $select[] = $user;
   }
}