0

I have privot table that have position_id and provider_id. How delete duplicate values that table can look like second table. I would like solve this task by eloquent. First table display next code:

MyTable::with('provider')->with('position')->orderBy('position_id')->get()

position_id provider_id
7            1
7            2
7            3  
9            5
9            6  
10           7


position_id provider_id
7       1
        2
        3   
9       5
        6   
10      7
befreeforlife
  • 481
  • 2
  • 7
  • 21

1 Answers1

0

You might try:

MyTable::with('provider')->with('position')->orderBy('position_id')- 
>groupBy('position_id')->get()

You may need to reverse the orderBy and groupBy.

Mark C.
  • 378
  • 1
  • 12
  • I get next error in this case Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column – befreeforlife Aug 23 '18 at 22:23
  • Something like this: MyTable::with(array('provider'=>function($query){ $query->select('position_id'); }))->with('position')->groupBy('position_id')->get(); ..... you might need to play with the syntax a bit, but that should get you fairly close – Mark C. Aug 23 '18 at 22:34
  • Here is a post about adding closure select statements to the eloquent with method. Once you get that part working, you should be able to do a group_by: https://stackoverflow.com/questions/19852927/get-specific-columns-using-with-function-in-laravel-eloquent – Mark C. Aug 23 '18 at 22:38