The table structures follows:
Table products: id,name
Table categories: id, name
Table locations: id, name
Table product_categories: products_id, categories_id
Table product_locations: products_id, locations_id
Each product can be in multiple categories and multiple locations. I need to fetch products from different categories based on a selected single location id with a limit of 10 records.
echo $db->products()->where('product_locations:locations.name', 'That location');
echo $db->products()->where('product_locations:categories.name', 'That category');
$page = 1; // please fill this var with any value >= 1
$limit = 10;
$offset = ($page-1)*$limit;
echo $db->products()->limit($limit, $offset); // if the offset is to high (invalid page) you will get zero results
page = 2; // please fill this var with >= 1
$limit = 10;
$offset = ($page-1)*$limit;
echo $db->products()->where('product_locations:locations.name', 'That location')->limit($limit, $offset);
echo $db->products()->where('product_locations:categories.name', 'That category')->limit($limit, $offset);
$page = 1; // please fill this var with >= 1
$limit = 10;
$offset = ($page-1)*$limit;
echo $db->products()->where('product_locations:categories.name', 'That category')->where('product_locations:locations.name', 'That location')->limit($limit, $offset);
$page = 1; // please fill this var with >= 1
...
$offset = ($page-1)*$limit;
$page = 1; // please fill this var with >= 1
$limit = 10;
$offset = ($page-1)*$limit;
$proudcts = $db->products()->where('product_categories:categories.id', array(1,2,3,4,5,6))->where('product_locations:locations.id', 6)->limit($limit, $offset);
foreach($products as $product){
echo $product->product_locations['price'];
}
$products = $db->products()->select('products.*, product_locations:price')->where('product_categories:categories.id', array(1,2,3,4,5,6))->where('product_locations:locations.id', 6)->limit($limit, $offset);
foreach($products as $product) {
echo $product['price'];
}