在Laravel Fluent中使用Distinct

Gle*_*ams 13 php fluent laravel laravel-3

我有这个加入:

Return DB::table('volunteer')
            ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
            ->select(array('*','volunteer.id AS link_id'))
            ->where('is_published', '=', 1)
Run Code Online (Sandbox Code Playgroud)

但不出所料地返回重复记录,所以我尝试使用distinct():

Return DB::table('volunteer')
            ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
            ->select(array('*','volunteer.id AS link_id'))
                        ->distinct()
            ->where('is_published', '=', 1)
Run Code Online (Sandbox Code Playgroud)

但我想distinct() 在一个特定的单个字段上使用,我可以在SQL中轻松完成.似乎distinct()不带参数,即我不能说distinct('volunteer.id').

任何人都可以指出我如何删除我的重复记录?我打赌这是我的另一个额头.

tot*_*dli 26

在项目中,我试图distinct()groupby()也和他们两个的工作:

//Distinct version.
Company_Customer_Product::where('Company_id', '=', $companyid)->distinct()->get(array('Customer_id'));
//Goup by version.
Company_Customer_Product::where('Company_id', '=', $companyid)->groupby('Customer_id')->get(array('Customer_id'));
Run Code Online (Sandbox Code Playgroud)

根据这个,也distinct()应该在你的情况下工作,只需使用它get():

Return DB::table('volunteer')
   ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
   ->select(array('*','volunteer.id AS link_id'))
   ->distinct()
   ->where('is_published', '=', 1)
   ->get(array('volunteer.id'));
Run Code Online (Sandbox Code Playgroud)

否则你distinct()在使用时不需要,groupby()所以你可以使用:

Return DB::table('volunteer')
   ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
   ->select(array('*','volunteer.id AS link_id'))
   ->group_by('volunteer.id')
   ->where('is_published', '=', 1)
   ->get(array('volunteer.id'));
Run Code Online (Sandbox Code Playgroud)