orderBy is not working in Laravel 5 - php

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

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

PHP & Laravel | Use OrderBy after a GroupBy

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

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

Laravel: Query with where clause gone wrong

What I am trying to achieve is to allow teachers to import a student into different classes.
Note: A student can be multiple classes.
The problem is that when I show the list of students in a select dropdown it should return all students except for students that are not in this class (the class being the page that I am on, app.com/classes/5 for example).
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'asc')
->get();
This works and shows all students that are not in this specific class BUT if a student that's in this class and another class their name appears in the list and as duplicate names.
What can I do?
When MySQL's only_full_group_by mode is turned on, it means that strict ANSI SQL rules will apply when using GROUP BY
You should try to select fields from schema on which you can apply group by instead of select *.
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->select('users.id', 'other fields you used')
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
Not IN is also useful in your case
User::select('fields you used')
->role('student')
->whereNotIn('id', DB::table('group_user')->where('group_id', $id)->pluck('user_id')) // $id = 5
->orderBy('name', 'ASC')
->get();
Modify your query to use distinct() like so;
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->distinct()
->get();
You could also groupBy('users.id')
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();

Perform JOIN on three or four different tables in Laravel syntax

TABLES X(ID,A_ID)A(ID,B_ID),B(ID,C_ID),C(ID,D_ID)D(id,VALUE)
I want to retrieve the value of D table using laravel syntax on basis of X table Id and perform a JOIN with other tables.
Please post answers only in laravel syntax. In other format I can do. I am new to it so.
X::select('value')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
But it is not working. please provide me proper solution. Right now I am using PHP logic to get the value, rather than optimise the query.
There are many ways, but this is the basic:
$rows = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('follows', 'follows.user_id', '=', 'users.id')
->where('follows.follower_id', '=', 3)
->get();
doc
Try this:
DB::table('X')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->select('D.value')
->where('x.id', '=', $val)
->get();
$result = X::select('a.value', 'd.*')
->join('a', 'x.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
foreach ($result as $row) {
# code...
}

Categories