@lifesound You can try this:
$shipmentId = 1000;
$stateCounts = DB::table('products')
->join('product_state', 'products.id', '=', 'product_state.product_id')
->join('states', 'states.id', '=', 'product_state.state_id')
->where('products.shipment_id', $shipmentId)
->select('states.name', DB::raw('count(states.id) as state_count'))
->groupBy('states.name')
->get();
⚠️ You may need to tweak the column name based on your table. I jus try with the regular convention. Please keep it in mind.