Laravel - raw query with pagination:
class InventoryController extends Controller
{
public function index(Request $request)
{
$sql = "SELECT *,products.name as product_name, product_categories.name as category_name, products.id as productid,
( SELECT SUM(buy_products.qty) as purcahse_qty
FROM buy_products
WHERE buy_products.product_id=products.id GROUP BY buy_products.product_id ) as bqty,
( SELECT SUM(sell_products.qty) as order_qty
FROM sell_products
WHERE sell_products.product_id=products.id GROUP BY sell_products.product_id ) as sqty
FROM products LEFT JOIN product_categories ON products.product_category_id=product_categories.id
";
$results = DB::select($sql);
$productInventoryData = $this->arrayPaginator($results, $request);
return view('auth.inventory.inventory_list')->with('productInventoryData', $productInventoryData);
}
public function arrayPaginator($array, $request)
{
$page = is_null($request->get('page')) ? 1 : $request->get('page');
$perPage = 2;
$offset = ($page * $perPage) - $perPage;
return new LengthAwarePaginator(array_slice($array, $offset, $perPage, true), count($array), $perPage, $page,
['path' => $request->url(), 'query' => $request->query()]);
}
}
And in views you can use links as like DB query build we use.
Thanks and happy coding.
No comments:
Post a Comment