Laravel, mysql - return same row mutiple times -
how return same row mutiple times? tried using wherein('table' , \session::get('memory'))
aswell, returned non-duplicates. far understood possible union, quite confused how implement it.
could me out?
session array:
array:5 [▼ 0 => "addon-0a65729-aak" 1 => "visiontek-900379" 2 => "addon-0a65729-aak" 3 => "addon-0a65729-aak" 4 => "addon-0a65729-aak" ]
query have far
$memory = \db::table('memory')->leftjoin('price', 'price.part_number' , '=', 'memory.part_number')->groupby('memory.part_number') ->select( 'memory.part_number part_number', \db::raw('min(price.price) price'), 'price.url url', 'memory.slug slug'); foreach (\session::get('memory') $key) { $memory = $memory->union($memory)->where('memory.slug', $key); } $memory = $memory->get();
correct output query return:
------------------------------------------------ | part-number| price | url | slug | |----------------------------------------------| |111111 | 123.3 |http://| addon-0a65729-aak| |133111 | 145.3 |http://| visiontek-900379 | |111111 | 123.3 |http://| addon-0a65729-aak| |111111 | 123.3 |http://| addon-0a65729-aak| |111111 | 123.3 |http://| addon-0a65729-aak| ------------------------------------------------
edit:
memory table :
memory table ================================================== id | part_number | slug ================================================== 1 |cmz8gx3m2a1600c9 | corsair-cmz8gx3m2a1600c9 2 |f4-3200c16d-16gvk | gskill-f4-3200c16d-16gvk 3 |f3-12800cl9d-8gbrl| gskill-f3-12800cl9d-8gbrl 4 |cmk32gx4m2a2666c16| gskill-f3-1600c9d-8gao 5 |f3-12800cl9d-8gbxl| gskill-f3-12800cl9d-8gbxl 6 |f4-2400c15d-16gvr | gskill-f4-2400c15d-16gvr 7 |f4-2666c15q-32grkb| gskill-f4-2666c15q-32grkb 8 |f3-1600c11d-8gnt | gskill-f3-1600c11d-8gnt 9 |cmx4gx3m2a1333c8 | corsair-cmx4gx3m2a1333c8 10 |f4-3000c14q-32gvk | gskill-f4-3000c14q-32gvk
price table
price table ================================================== id | part_number | price | url ================================================== 1 |cmz8gx3m2a1600c9 | 122.33 | http:store_url 2 |cmz8gx3m2a1600c9 | 222.33 | http:store_url 3 |cmz8gx3m2a1600c9 | 551.00 | http:store_url 4 |cmk32gx4m2a2666c16| 1999.99 | http:store_url 5 |f3-12800cl9d-8gbxl| 222.01 | http:store_url 6 |f4-2400c15d-16gvr | 800 | http:store_url 7 |cmz8gx3m2a1600c9 | 19.99 | http:store_url 8 |f3-1600c11d-8gnt | 16.05 | http:store_url 9 |f4-3000c14q-32gvk | 990.15 | http:store_url ==================================================
session array
session array - array ( cmz8gx3m2a1600c9 cmz8gx3m2a1600c9 cmz8gx3m2a1600c9 cmz8gx3m2a1600c9 f3-12800cl9d-8gbxl cmz8gx3m2a1600c9 f4-3000c14q-32gvk f3-12800cl9d-8gbxl cmx4gx3m2a1333c8 )
output, how should like
output ========================================================================== part number | price | url | slug ========================================================================== cmz8gx3m2a1600c9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 //session val 0 cmz8gx3m2a1600c9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 //session val 1 cmz8gx3m2a1600c9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 //session val 2 cmz8gx3m2a1600c9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 f3-12800cl9d-8gbxl| 222.01 | http:store_url | gskill-f3-12800cl9d-8gbxl cmz8gx3m2a1600c9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 f4-3000c14q-32gvk | 990.15 | http:store_url | gskill-f4-3000c14q-32gvk f3-12800cl9d-8gbxl| 222.01 | http:store_url | gskill-f3-12800cl9d-8gbxl cmx4gx3m2a1333c8 | null | null | corsair-cmx4gx3m2a1333c8 //session val n
edit: found solution looped through session array , query result , added values new array , converted array object.
$count = 0; $newmemory = []; foreach (\session::get('memory') $k => $v) { foreach ($memory $mem => $value) { if ($v == $value->slug) { foreach ($value $key => $val) { $newmemory[$count][$key] = $val; } } } $count++; } $newmemory = json_decode(json_encode($newmemory));
now new query using laravel 5.2 expected result.
$final=""; $cnt=0; foreach ($array $key => $value) { $memory = db::table("price p") ->leftjoin("memory m","m.part_number","=","p.part_number") ->select('m.part_number part_number', db::raw('min(p.price) price'),'p.url url', 'm.slug slug') ->where("m.slug","like","%".$value."%"); if($cnt==0) { $cnt+=1; $final=$memory; } else { $final=$final->unionall($memory); } } $final=$final->get();