To use this SQL code in Laravel, you can create a model for each table involved in the query and use the Eloquent ORM to build the query. Here's an example:
- Create models for the "warehouses", "warehouse_locations", "settings", and "stock_balances" tables:
// Warehouse model
class Warehouse extends Model
{
protected $table = 'warehouses';
}
// WarehouseLocation model
class WarehouseLocation extends Model
{
protected $table = 'warehouse_locations';
}
// Setting model
class Setting extends Model
{
protected $table = 'settings';
}
// StockBalance model
class StockBalance extends Model
{
protected $table = 'stock_balances';
}
- Use the models to build the query:
$warehouses = Warehouse::select('wh.code', 'wh.name', 'loc.location_type_id', 's.name', DB::raw('count(distinct loc.id) as total_location'), DB::raw('(select count(distinct stk.location_id ) from stock_balances stk where stk.warehouse_id = wh.id and stk.inventory_status ="20") as total_occupied'))
->join('warehouse_locations as loc', 'wh.id', '=', 'loc.warehouse_id')
->join('settings as s', 'loc.location_type_id', '=', 's.id')
->groupBy('wh.code', 'wh.name', 'wh.id', 'loc.location_type_id')
->get();
- You can then loop through the results to display them:
@foreach ($warehouses as $warehouse)
<p>{{ $warehouse->code }} - {{ $warehouse->name }} - {{ $warehouse->location_type_id }} - {{ $warehouse->name }} - {{ $warehouse->total_location }} - {{ $warehouse->total_occupied }}</p>
@endforeach