Laravel pagination with orderby doesnt work well - laravel

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?

Related

Laravel shows more pages in results

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.

Product filtering problems with Laravel Query Builder

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();

How to fetch latest posts and specific post id at first in laravel eloquent

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();

How to add paginate to laravel eloquent?

I am new to laravel and I am having error in adding paginate using laravel eloquent.
This code work without paginate(). If paginate was added, got error
Method paginate does not exist.
$articles = Article::orderBy('updated_at', 'DESC')
->findOrFail([1,2,3,4,5])
->where('status','p')
->paginate(7);
As people have mentioned in the comments, you can not use findOrFail() with paginate() since they are both ways to execute a query. You can instead use whereIn().
To get what you're after you can do:
$articles = Article::orderBy('updated_at', 'DESC')
->whereIn('id', [1,2,3,4,5]) //assuming "id" is the primary key for the table
->where('status','p')
->paginate(7);

Laravel 5.3 Eager Loading Selected Fields

I have this code:
$products = \App\Product::whereIn('id', $arrayKeys)
->with('photo')
->get();
And the result is:
If I use select code like this one:
$products = \App\Product::whereIn('id', $arrayKeys)
->select(['desc'])
->with('photo')
->get();
I can't see the photo. Is there a way to it? So that I can get the selected fields as well as the photo relationship.

Resources