laravel eloquent postgres two rows to one in result

I read a little bit and dont want to use json_agg, i would like to have another fild in my result like name2. My call with the Join works etc. but i would like to hae a little bit other result and I dont know how.

I have two tables:

table1 and table2

i make a join

$join= DB::table('table1')
            ->join('table2', 'table1.setcardid', '=', 'table2.id')

after that i i make a Select with diferent Where

->select('........................')
    ->where('........................' ,1 )
    ->where('........................', 1)
    ->where('........................' ,$var2 )
    ->where('........................' , $var1 )
    ->whereIn('images.bilderstes',[3,4])
    ->get();
return $join;

i got only one problem with the result: I become two resulst for one userid

#items: array:2 [
    0 => {#535
      +"bilderstes": 3
      +"userid": 1
      +"name": "789576277.jpg"
    }
    1 => {#537
      +"bilderstes": 4
      +"userid": 1
      +"name": "704905321.jpeg"
    }
  ]

I would like to have a result like this:

#items: array:2 [
    0 => {#535
      +"userid": 1
      +"name": "789576277.jpg"
      +"name2": "704905321.jpeg"
    }
  ]

my image table structure is like this

id | userid | name | bilderstes

1 | 1 | x | 3

2 | 1 | x | 4

3 | 2 | x | 3

4 | 2 | x | 4

5 | 3 | x | 3

6 | 3 | x | 4

I need a result like this:

userid | name | name2

1 | x | x

2 | x | x

3 | x | x

1 answer

  • answered 2019-09-16 01:13 Tanvir Ahmed

    But where is your name2 field???? Please post the both pf the tables field and let us see what we can do