简体   繁体   中英

Laravel SQL Query Returning Duplicates

I am trying to increase the speed of my Laravel 5.4 sql query by using joins, which I've never used before. I currently have the query working and returning results, my issue is that I get like each results around 8 times. Ex: I get the same 2001 Chevrolet Silverado 8 times in my array. If anybody could help me out I would really appreciate it. Thanks

My query + collection:

$tmp = DB::table('inventories'
            )->join(
                'vehicles', 'vehicles.id', '=', 'inventories.id'
            )->join(
                'vimages', 'vimages.vehicle_id', '=', 'inventories.id'
            )->where(
                'inventories.dealer_id', '=', $dealer_id
            )->where(
                'inventories.is_active', '=', 1
            )->where(
                'inventories.is_deleted','=', 0
            )->select(
                'inventories.stock_number','inventories.vehicle_id','vehicles.year','vehicles.make','vehicles.model','vehicles.vin','inventories.vehicle_status',
                'inventories.cost','inventories.search_meta','vimages.name'
        );


        $data = collect();
        $pmt = $tmp->get();
        // return json_encode( $pmt );

        // logger( sprintf('# of rows returned: %s', $pmt->count() ) );

        $pmt->each( function($row) use(&$data) {
          // logger( sprintf('Row    : %s', $row->toJson() ));

            $data->push( array(
                'stock_number' => $row->stock_number,
                'vehicle_id' => $row->vehicle_id,
                'year' => $row->year,
                'make' => $row->make,
                'model' => $row->model,
                // 'trim' => $row->trim,
                'vin' => $row->vin,
                'status' => $row->vehicle_status,
                // 'purchase_price' => $row->purchase_price,
                'cost' => $row->cost,
                // 'retail_price' => $row->retail_price,
                'search_meta' => $row->search_meta,
                // 'images' => $row->getFirstImage()
                'images' => $row->name
                // 'interior_color' => $row->vehicle()->first()->interior_color,
                // 'exterior_color' => $row->vehicle()->first()->exterior_color,
                // 'firstImg' => $row->getFirstImage()
                // 'images' => Vimage::select('vehicle_id','name'
                //                 )->where(
                //                     'dealer_id', '=', $row->dealer_id
                //                 )->where(
                //                     'vehicle_id', '=', $row->vehicle_id
                //                 )->limit(1)->get()
            ));

        });

Example of my array: https://i.imgur.com/FOphST8.png

I don't see your db table but I think there is problem in first join statment: you should use

->join('vehicles', 'vehicles.id', '=', 'inventories.vehicle_id' )

instead of

->join('vehicles', 'vehicles.id', '=', 'inventories.id' )

also second join should be like

->join('vimages', 'vimages.vehicle_id', '=', 'vehicles.id')

instead of

->join('vimages', 'vimages.vehicle_id', '=', 'inventories.id')

It sounds trivial but this should work:

$tmp = DB::table('inventories'
        )->join(
            'vehicles', 'vehicles.id', '=', 'inventories.id'
        )->join(
            'vimages', 'vimages.vehicle_id', '=', 'inventories.id'
        )->where(
            'inventories.dealer_id', '=', $dealer_id
        )->where(
            'inventories.is_active', '=', 1
        )->where(
            'inventories.is_deleted','=', 0
        )->selectRaw("DISTINCT inventories.stock_number, inventories.vehicle_id, 
            vehicles.year, vehicles.make, vehicles.model, vehicles.vin, 
            inventories.vehicle_status, inventories.cost, 
            inventories.search_meta, vimages.name"
    );

Update : forgot to delete the single quotes in selectRaw

Figured it thanks to @Devon. Added another where statement to query to only get first image.

Thanks

)->where('vimages.sequence','=', 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM