1

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.sstatic.net/gq7w0.png

1
  • 1
    Do you have multiple images? Commented Aug 13, 2018 at 13:02

3 Answers 3

2

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. The inventories table doesn't contain a vehicle_id though. The id of inventories correlates with the vehicle_id of the vehicles table.
1

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

1 Comment

Thank you, I managed to solve it using this though. I posted my answer above.
0

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

Thanks

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.