I want to add laravel paginator for my select query:
the below query is working fine but not able to add laravel paginator.
$results = DB::select('SELECT
distances.*
FROM
(SELECT
( 6371 * acos( cos( radians(23.0376279 ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(72.5102283 ) ) + sin( radians(23.0376279 ) ) * sin( radians( latitude ) ) ) ) AS distance,
shopdatabase.shop_product.stock,
shopdatabase.shop_product.price AS price,
shopdatabase.shops.id AS shopId,
shopdatabase.product.name AS name,
shopdatabase.product.id AS productId,
shopdatabase.shops.name AS shopName,
shopdatabase.shop_product.status,
shopdatabase.product_image.title AS image,
shopdatabase.product_meta.metaValue AS sourceDetails,
shopdatabase.product.sourceUrl
FROM
shopdatabase.shop_product
INNER JOIN shopdatabase.shops
ON shopdatabase.shop_product.shopId = shopdatabase.shops.id
INNER JOIN shopdatabase.product
ON shopdatabase.shop_product.productId = shopdatabase.product.id
INNER JOIN shopdatabase.product_image
ON shopdatabase.shop_product.productId = shopdatabase.product_image.productId
INNER JOIN shopdatabase.product_meta
ON shopdatabase.shop_product.productId = shopdatabase.product_meta.productId
WHERE
shopdatabase.shop_product.status = "active") distances
WHERE
distance < 20
ORDER BY
distance ASC');
Or can you tell me how can i write this select query with Eloquent?
That will be good if we can write the above code with Eloquent.
Eloquent is often not the best choice for more complex queries.
As earlier commenters have mentioned you might be able to refine this by establishing your relations.
However you should be able to paginate query builder results using paginate().
DB::select($query)->paginate(25);
Another option is to use slice() on a collection.
$collection = DB::select($query)->get();
$collection->slice(($currentPage-1) * $size, $size);
Then you need if you need a paginator to create links etc. you can create a paginator manually.
new LengthAwarePaginator(
$collection,
$total,
$size,
$currentPage
);
I suggest you read through the Laravel documentation for Eloquent and learn how to use relationships correctly.
Check out this resource: https://laravel.com/docs/5.6/eloquent-relationships
Once you set up your model relationships you should then be able to call the paginate method as specified here:
https://laravel.com/docs/5.6/pagination
You should be able to use something like this:
$foo->with('bar')->paginate();
Related
I want to make an API using laraval which will have request of latitude and longitude and the response will provide json of location of user's nearest pharmacy
how can I do or simple example or tutorial similar ?
If you already have the latitude and longitudes in your database you might as well query them yourself. This is from an older project (so may not work or be the best method) where I'm doing something similar:
$cities = City::select(DB::raw('*, ( 6367 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') ) * sin( radians( latitude ) ) ) ) AS distance'))
->having('distance', '<', 25)
->orderBy('distance')
->get();
I have this query:
$user = \App\User::where('id','=',191)
->whereHas('addresses', function($q){
$q->getByDistance(52.5293878, 13.3416309, 5);
})
->with(['addresses' => function($q){
$q->getByDistance(52.5293878, 13.3416309, 5);
}])
->get();
dd($user);
This picks the user with id 191, only if he has an address in range, and then he also eager loads the address.
This is the scopeGetByDistance function from the class addresses
public static function scopeGetByDistance($query,$lat, $lng, $max_distance)
{
return $query->selectRaw('lat, lng, ( 3959 * acos( cos( radians( '.$lat.') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(' . $lng . ') ) + sin( radians(' . $lat .') ) * sin( radians(lat) ) ) ) AS distance')
->having('distance', '<', $max_distance )
->orderBy( 'distance', 'ASC' );
}
I picked the user with id 191 because I know that he has an address with distance ~2 to the coordinates.
This will output a user, but the relation is empty. Shouldn't the whereHas make sure that the relation is not empty? I also know for sure that he must have an address in range. This is how the relation looks like:
I had to add the reference key to the selectRaw like this:
return $query->selectRaw('user_id, lat, lng, ( 3959 * acos( cos
How can i calculate distance in laravel5.5 with SQL query ?
My code mention below.
$specialProducts = Products::select('products.id', 'products.name', 'products.product_img', 'products.slug', 'products.price','products.lat','(3959 * ACOS(COS(RADIANS($lat))* COS(RADIANS(22.2765097))* COS(RADIANS(70.792523) - RADIANS(70.7583265))+ SIN(RADIANS(22.2897874))* SIN(RADIANS(22.2765097)))) AS distance')
->join('subcategories', 'subcategories.id', '=', 'products.sub_category_id')
->join('categories', 'categories.id', '=', 'subcategories.category_id')
->join('store', 'store.id', '=', 'products.store_id')
->where('products.status', 1)
->where('products.special', 1)
->orderBy('products.created_at', 'DESC')
->get();
If you have the values for the latitude and longitude in your DB, then you can get the distance between them using the SQl query as in the below example using raw SQL with DB:raw().
$lat = 41.118491 // user's latitude
$lng = 25.404509 // user's longitude
SELECT *,
( 6371 * acos( cos( radians($lat) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians($lng) ) + sin( radians($lat) )
* sin( radians( latitude ) ) ) )
AS calculated_distance
FROM settings as T
HAVING calculated_distance <= (SELECT distance FROM settings WHERE sid=T.sid)
ORDER BY distance_calc
If you want to get distance in miles instead of kilometers, replace 6371 with 3959.
The calculation process may take a long time so, you might want to cache the result for next use.
It is better to do it with php instead of mysql. Check my response here.
https://stackoverflow.com/a/50040011/1262144
SELECT * FROM sales
WHERE (sales_code, payment_code, client_id) IN ( ('1118', '1', '99'), ('1119', '1', '99') );
can i do the same thing with laravel?
One solution for this would be to use raw expressions, as follows:
$query = DB::table('sales')
->whereRaw("(sales_code, payment_code, client_id) IN ( ('1118',1, 99), ('1119',1,99))")
->get();
https://laravel.com/docs/5.5/queries#raw-expressions
Please help me want to left join two table in Codeigniter with Orm docrtrine 2 libray.
Doctrine entity model doesn't give association for one to many and vice-versa. To allow these feature you have to update your entities with valid association. Below is an example for such two tables which contains user information in users table and there favorite meals in UserFavoriteMeals
For Left JOIN update needed in both entities for association explained below :
In user entity add onetomany association of UserFavoriteMeals where mappedby is the key on basis of which association happens in db.
/**
* #var \Entity\UserFavoriteMeals
*
* #OneToMany(targetEntity="Entity\UserFavoriteMeals", mappedBy="userid" )
* #JoinColumns({
* #JoinColumn(name="id", referencedColumnName="userId", nullable=true)
* })
*/
private $UserFavoriteMeals;
Simillary, Put manyToOne association in UserFavoriteMeals entity
/**
* #var \Entity\Users
*
* #ManyToOne(targetEntity="Entity\Users")
* #JoinColumns({
* #JoinColumn(name="userId", referencedColumnName="id", nullable=true)
* })
*/
private $userid;
In these manner we have to manage associations for Left JOIN, now simply write a Left JOIN query in below format :
$left_query = $this->em->createQuery("SELECT fm,u FROM Entity\userFavoriteMeals fm LEFT JOIN fm.userid u WHERE fm.userId = 231 ")->getArrayResult();
print_r($left_query);
$left_query_inverse = $this->em->createQuery("SELECT u,fm FROM Entity\Users u LEFT JOIN u.UserFavoriteMeals fm WHERE u.id = 4")->getArrayResult();
print_r($left_query_inverse);
First you need to integrate Doctrine into CodeIgnitor.
http://docs.doctrine-project.org/en/2.0.x/cookbook/integrating-with-codeigniter.html
This is how you use custom query to left join two tables in Doctrine.
<?php
$query = $em->createQuery('SELECT u.id, a.id as article_id FROM CmsUser u LEFT JOIN u.articles a');
$results = $query->getResult(); // array of user ids and every article_id for each user
More on custom query :- http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html
If you want DQL way, which is preferred, it should be something like this.
$qb->select('u')
->from('Entity_User', 'u')
->leftJoin('u.profile','p')
->leftJoin('a.user_details','ud')
->where('u.id = :id')
->setParameter('id','100')
->getQuery();
More on DQL :- http://docs.doctrine-project.org/en/2.0.x/reference/query-builder.html
I hope this helps,
Cheers!