PHP & Laravel | Use OrderBy after a GroupBy - php

This is my Php code
$elements= DB::table('transactions')
->select('product_id', DB::raw('count(*) as total'))
->groupBy('product_id')
->get();
I want to apply an orderby and a limit('5') on count
Any orientation ? Thank you.

add orderBy() ..and take()
$elements= DB::table('transactions')
->select('product_id', DB::raw('count(*) as total'))
->groupBy('product_id')
->orderBy('your_column')
->take(5)
->get();

Related

Laravel orWhere with left join

I'm not that good with Laravel Eloquent, so be cool pls.
I have 1 product table and 4 "target" tables (sales, novelties, liquidations and exposure).
I'm trying to get all products that have at least one of the four "targets" with this query:
return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
->where('noveltys.novelty_end_at', '!=', null)
->orwhere('sales.sale_end_at', '!=', null)
->orWhere('liquidations.liquidation_end_at', '!=', null)
->orWhere('exposures.exposure_end_at', '!=', null)
->get();
Problem is, it only return products that have the first "where" clause true. (In other word, currently, this query only return produtcs with a not null novelty_end_at, it doesn't return products with a sale_end_at and others).
How can I achieve that it also return products with a not null sale_end_at, exposure_end_at and a liquidation_end_at ?
Thank you!
seems a syntax mistake
->orwhere('sales.sale_end_at', '!=', null)
should be
->orWhere('sales.sale_end_at', '!=', null)
also you can try using orWhereNotNull
like
return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
->whereNotNull('noveltys.novelty_end_at')
->orWhereNotNull('sales.sale_end_at')
->orWhereNotNull('liquidations.liquidation_end_at')
->orWhereNotNull('exposures.exposure_end_at')
->get();
I found the solution, it's almost the same thing, execpt that I'm using orWhereNotNull and that I'm using DB::table at the start instead of Product::leftjoin directly.
return DB::table('products')
->leftjoin('sales', 'sales.product_id', '=', 'products.id')
->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
->whereNotNull('sales.sale_end_at')
->orwhereNotNull('noveltys.novelty_end_at')
->orwhereNotNull('liquidations.liquidation_end_at')
->orwhereNotNull('exposures.exposure_end_at')
->get();
Thank you!
Perhaps you could use ->orWhereHas() method. To use this function, you must define the relationship in your Sales and the four target Model classes. Documentation: https://laravel.com/docs/9.x/eloquent-relationships
Then in your query,
return Product::whereHas('noveltys', function(Builder $query) {
// your where clause
})
->orWhereHas('sales', function(Builder $query) {
// your where clause
})
->orWhereHas('liquidations', function(Builder $query) {
// your where clause
})
->orWhereHas('exposures', function(Builder $query) {
// your where clause
})
->get();

Lavarel - Nested query conditional

I have a problem in this code
$query
->where('start_at', '>=', max($campaign->start_at, now()))
->where('end_at', '<=', $campaign->end_at)
->whereNull('deleted_at')
->where('num_posti', '>', function ($query){
$query
->from((new Reservation())->getTable())
->whereColumn('id', '=', 'slots.id')
->whereNull('deleted_at')
->count();
});
The 'slots.id' field doesn't see, but if I execute only the query's first part i see this value.
What is the problem?

Errors on Laravel pagination

I need to get specific fields from the database and then paginate the results but I am not sure where to place ->paginate().
My code is as below:
Upload::where('sponsor_id', Request::segment(2))
//->paginate(20)
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->get([
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
])
I'm getting the correct results without paginating but I would like to paginate the results.
First, you need to use select then pass in pagination.
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select('uploads.filename','users.first_name','users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at')
->pagiate(10);
You can use select method for filtering columns. And then put paginate method end of your query.
Finaly your query will be like this:
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select(
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
)
->paginate(20);

Select max values from query - LARAVEL

I have a table with fields:
|| id || name || c_number ||
I have a query:
$query ->select('c_number', DB::raw('count(*) as total'))
->groupBy('c_number')
->get();
output:
[{"c_number":1,"total":4},{"c_number":2,"total":2},{"c_number":3,"total":2},{"c_number":4,"total":2}]
I need select from output only value(-s) of c_number where total is max.
Can you tell me, please, how to do that with query?
You could using a mixture or orderBy() and first().
->select('c_number', DB::raw('count(*) as total'))
->groupBy('c_number')
->orderBy('total', 'desc')
->first();
Hope this helps!

orderBy is not working in Laravel 5

I am using the below query. orderBy is not working in below query. This query is working in localhost but it is not working in Online Server.
return DB::table('reports')
->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
->select('*')
->orderBy('report_id', 'desc')
->take(10)
->get();
Try setting an alias for each table and then using the required alias on the orderBy
If there is a report_id in both tables it will not know which one to use and is probably throwing an error if you look for it.
return DB::table('reports as r')
->leftJoin('sources as s', 'r.report_source_id', '=', 's.id')
->select('*')
->orderBy('r.report_id', 'desc')
->take(10)
->get();
Try this..
use "reports.report_id";
return DB::table('reports')
->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
->select('*')
->orderBy('reports.report_id', 'desc')
->take(10)
->all();
Try to use reports.report_id like
return DB::table('reports')
->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
->select('*')
->orderBy('reports.report_id', 'desc')
->take(10)
->get();

Categories