Php Laravel查询-获取在管理员用户文章中发布报价的所有用户
所以,当我写在标题,我需要让所有用户谁后提供给管理员用户文章。。。 所以我写:Php Laravel查询-获取在管理员用户文章中发布报价的所有用户,php,laravel,model,eloquent,Php,Laravel,Model,Eloquent,所以,当我写在标题,我需要让所有用户谁后提供给管理员用户文章。。。 所以我写: public function userbase() { return Offer::select('users.*') ->where('users.admin', 9) ->join('articles','articles.id','=','offers.article_id') ->joi
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;
}
但我得到:
Illumb\Database\Query\Builder::join()缺少参数2
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]);
}
不过,如果你能更好地解释一下表格结构,你会很感激的。你试过这个吗
foreach (User::all() as $user) {
foreach ($user->offers as $offer) {
if ($offer->article->user->isAdmin())
$select[] = $user;
}
}
我得到的结果是:[……还有语法错误返回$query->whereRaw($sql,[$this->id,9]);更正了语法
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;
}
}