我有这张桌子,名为"分享":
这是"用户"表:
所以我通过"分享"获取共享项目列表模型和与每个条目相关联的用户:
class Share extends Model
{
public function UserDetail()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
}
我需要的是使用parent_id
3:
$user = Share::where('parent_id', '=', 3)->paginate(15);
它返回所有内容,但我需要返回这样的唯一用户:
感谢您的帮助。
答案 0 :(得分:0)
您可以使用$user = Share::where('parent_id', '=', 3)->groupBy('item_id')->paginate(15);
方法获取唯一身份用户。请参阅此链接
https://laravel.com/docs/5.4/queries
{{1}}
希望回答了你的问题。
答案 1 :(得分:0)
根据您的评论,您想要使用类似parent_id
(3)的所有股票:
$shares = Share::where('parent_id', 3)->get();
和他们的用户。每个共享属于一个用户。但是每个用户用户可以共享多次,但只想让用户使用一次。我们可以做到
//Using eloquent
$users = Share::where('parent_id', 3)
->join('users', 'users.id', '=', 'shares.user_id')
->select('users.id', 'users.email', 'users.first_name', 'users.last_name')
->groupBy('users.id', 'users.email', 'users.first_name', 'users.last_name')
->get();
//using Query builder
$users = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->where('shares.parent_id', 3)
->select('users.id', 'users.email', 'users.first_name', 'users.last_name')
->groupBy('users.id', 'users.email', 'users.first_name', 'users.last_name')
->get();
这应该为您提供共享parent_id = 3
的用户的明确列表。