I'm working with a query where the results finish on page 21, but I'm getting 29 pages of links. My problem might be a groupBy() problem, but I don't know how to do it.
$afiliates = DB::table('ad_afiliado as af')
->select('af.logo_url', 'af.NombreComercial', 'af.Destacado',
'af.id_afiliado', 'af.Clave')
->join('af_promocion as promo', function ($join) {
$join->on('af.Clave', '=', 'promo.id_afiliado');
})
->where('promo.v_fin', '>', $FechaActual)
->where('af.Activo', '=', 'S')
->where('af.Categoria', 'like', $categoryStr)
->orderBy('af.NombreComercial')
->orderBy(DB::raw('RAND()'))
->distinct()
->paginate(9);
I found the answer. It seems to be an issue with laravel distinct() and pagination, especialy when making joins.
The thread is here:
distinct() with pagination() in laravel 5.2 not working
I had to add the field name was causing repetition of results, to the distinct() and paginate().
In my case 'promo.id_afiliado'
as you'll see in the code next
$afiliates = DB::table('ad_afiliado as af')
->join('af_promocion as promo', 'af.Clave', '=', 'promo.id_afiliado')
->select('af.logo_url', 'af.NombreComercial', 'af.Destacado', 'af.id_afiliado', 'af.Clave')
->where('promo.v_fin','>',$FechaActual)
->where('af.Activo','=', 'S')
->distinct('promo.id_afiliado')
->orderBy('af.Destacado', 'desc')
->orderBy('af.NombreComercial')
->paginate(9, 'promo.id_afiliado');
Thaks #TimLewis for caring, hope this will usefull to others.
Related
There's a problem about laravel paginate with orderby.
$products->with(['cover'])
->orderBy('priority', 'asc')
->paginate(12);
Code above returns different result then code below;
$products = $products->with(['cover'])
->orderBy('priority', 'asc')
->get();
All the first 12 records are different.
Whats wrong with it?
I try to implement ajax based filtering in my ecommerce project homepage for searching a product. I am using Laravel Query Builder. My Query for filtering products is given below-
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->leftjoin('products_to_categories','products.products_id','products_to_categories.products_id')
->leftjoin('categories','products_to_categories.categories_id','categories.categories_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->take(5)
->get();
I get every product twice in search result. don't know why. A sample response is given in this picture.
Can anyone help me to optimize my query for getting the desired result?
join with categories table only when you have to.
and select your columns strictly. then group by selected columns.
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->leftjoin('products_to_categories','products.products_id','products_to_categories.products_id')
->leftjoin('categories','products_to_categories.categories_id','categories.categories_id')
->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->select(['products_name','products.id','image_path'])
->groupBy(['products_name','products.id','image_path'])
->take(5)
->get();
you can use from groupBy to avoid comming repeated data.
try this one:
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->groupBy('products_name','products.products_id')
->take(5)
->get();
Is there way to fetch latest posts with pagination And I also want a specific post to first position in returned collection.
I tried this...
Post::where(function ($query) {
$query->where('status', 'draft')->orWhere('status', 'published');
})
->orWhere('id', 21)
->with(['author.profile'])
->orderBy('created_at', 'desc')
->paginate(3);
In this query I do get the 21 post id but it is on 3rd page. I want to get it on first place. Please guide How can I do this.
Thanks
You can achieve this by using a raw statement in your orderBy
Post::orderBy(DB::raw('id = 5'), 'DESC')
->orderBy('created_at', 'desc')
This is because mysql can use boolean expressions in order by statements
By using eloquent you do like this
Post::where('id', '=', 21)->orderBy('created_at','desc')->first();
I have a Query Builder problem on Laravel 5.4,
I want to select only certain field on where statement.
if I don't put WHERE statement function the SELECT function performed nicely, but when I put WHERE LIKE function then seems the SELECT function not working and it automatically selecting all (*)
Fyi, both user and address table has 'name' field. it only select name from user table. it work nicely.
DB::table('user')
-leftJoin('address', 'user.id', '=', 'address.user_id')
->select('user.name')
->paginate(1);
but this is not working
DB::table('user')
-leftJoin('address', 'user.id', '=', 'address.user_id')
->select('user.name')
->where('user.name', 'LIKE', '%jhon%')
->paginate(1);
how do I accomplish this?
Any anwer will be highly appreciated!
Please write this. This will solve your problem
DB::table('user')
->leftJoin('address', 'user.id', 'address.user_id')
->where('user.name', 'LIKE', '%jhon%')
->select('user.name')
->get();
Its working. Give it a test.
DB::table('user')
->leftJoin('address', 'address.user_id', '=', 'user.id')
->where('user.name', 'like', '%jhon%')
->select('user.name')
->get();
So I have ads in a database.
There are 3 types for the ads. Featured, bold and free.
I want to display them in a page. First based on what category has been selected.
I want to show all featured first by added date and then after that all bold and free items by added date. And I want to paginate them.
I have tried this:
$adsFeatured = Ads::where('category', '=', $category)->where('status', 'enabled')->where('type', 'featured')->get();
$adsOther = Ads::where('category', '=', $category)->where('status', '=', 'enabled')->where('type', '=', 'free')->orWhere('type', '=', 'bold')->get();
$adsOther->merge($adsFeatured);
$adsFeatured->paginate(15);
But ofcourse it cannot be paginated if I use get(), but if i dont use it then i cant merge them, i want to merge them because i hope it would place them in that order.
Thanks for help, and I hope I have explained everything properly. :)
You should be able to do that in one query. Try this
$ads = Ads::selectRaw('*, IF(type = "featured",1,0) AS is_featured')
->where('category', $category)
->where('status', 'enabled')
->orderBy('is_featured', 'desc')
->orderBy('created_at', 'desc')
->paginate(15);