Order by is not working? - laravel

I have a problem with order by articles by priority. Where is working. Any suggestion?
$articles = Articles::whereHas('priority',function($query){
$query->orderBy('order','asc');
// $query->where('order','=',1);
})->limit(7)->get();

You have to use join to fetch the articles by the order of related table column as:
Articles::join('priority', 'articles.id', '=', 'priorities.article_id')
->orderBy('priorities.order','asc')
->select('articles.*')
->limit(7)
->get();

You can do the following:
$quotes=Articles::orderBy('priority', 'desc')->limit(7)->get();

Try this code
$articles = Articles::orderBy('order','desc')->limit(7)->get();
and if you want to add a condition you can use somthing like this:
$articles = Articles::join('priorities','articles.id','=','priorities.articale_id')->where('priority',1)->orderBy('order','desc')->limit(7)->get();

Related

Wildcard-like syntax in an eloquent Where clause to search between two strings

I saw the answer provided in this question Wildcard-like syntax in an eloquent Where clause? now I want to know if is there a way to search between two strings?.
basicasicaly in my code I want to show requests that have a status of new or scheduled.
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->where('status', 'LIKE','%New%')
->get();
you can use whereIn ,The whereIn method verifies that a given column's value is contained within the given array:
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->whereIn('status', ['new','scheduled'])
->get();
You can use:
->where('status', 'new')
->orWhere('status', 'scheduled')
you can simply use a where with an orWhere:
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->where(function($q) {
$q->where('status', 'LIKE','%New%');
$q->orWhere('status', 'LIKE','%Scheduled%');
})->get();

db::raw adding is null in query

I am trying to use laravel query builder like:-
$users = DB::table('baid_collection')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->where(DB::raw("assigned_user_id = '$userId' and DATE(date_entered)=CURDATE()"))
->groupBy('assigned_user_id')
->get();
This query should be like
select sum(total) as total_collect,collection_limit_c from `baid_collections` inner join `users_cstm` on `baid_collections`.`assigned_user_id` = `users_cstm`.`id_c` where assigned_user_id = '15426608-3ea5-f299-7a80-601bd06be2d9' and DATE(date_entered)=CURDATE() group by `assigned_user_id`
But last run query showing me
select sum(total) as total_collect,collection_limit_c from `baid_collection` inner join `users_cstm` on `baid_collections`.`assigned_user_id` = `users_cstm`.`id_c` where assigned_user_id = '15426608-3ea5-f299-7a80-601bd06be2d9' and DATE(date_entered)=CURDATE() is null group by `assigned_user_id`
In query is null giving problem i dont want is null in query
Use whereRaw instead of where like
$users = DB::table('baid_collections')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->whereRaw("assigned_user_id = '{$userId}' and DATE(date_entered)=CURDATE()")
->groupBy('assigned_user_id')
->get();
It's creating a problem due to where needs atleast two parameters and you are just passing one parameter
Check this if it helps, write whereDate instead of add date in raw query.
if you are not using Carbon, you can also use php date("Y-m-d"), method to get date
DB::table('baid_collection')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->where(DB::raw("assigned_user_id = '$userId'"))
->whereDate("date_entered", "=", Carbon::now()->toDateString())
->groupBy('assigned_user_id')->get();
You can try this
$users = DB::table('baid_collection')
->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
->where(DB::raw("assigned_user_id = '$userId' and DATE(date_entered) = '".date('Y-m-d')."'"))
->groupBy('assigned_user_id')
->get();

How to insert the value into query?

Do I need to insert the variable in sum(orderdets.quantity) into another variable?
$order = Order::with('customer','product')
->select(
'orders.id',
'orders.customer_id',
'orderdets.product_id',
DB::raw('SUM(orderdets.quantity)'))
->get();
Try This
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
dd($orders);
Use AS tempName to get the column data by specific name. this way it can be accessed.
$order = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) AS sumOrders'))->get();
$orders = Order::with('customer','product')
->select('orders.id', 'orders.customer_id', 'orderdets.product_id'))
->get()->map(function($order){
$order->put('sum', $order->orderdets->sum('quantity');
})->save();
This will sum all your debts and will also update orders table in one go, Although you question wasn't clear but I think you must be looking for something like this
Please try below script in your controller.
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
return $orders;
Thanks
PHPanchal

How to select min and max in laravel

I want to convert this code in laravel.
SELECT MAX(date_start) AS DateStart,MIN(date_end) AS DateEnd FROM DBTest
And I try this code
$data = DB::table('DBTest')
->select(max('date_start'), min('date_end')))
->get();
Return Error: max(): When only one parameter is given, it must be an array
I am using laravel 5.2, and SQLyog as database
I am confuse in syntax please help me
You can't use functions in select statement, but you can use raw SQL :
$data = DB::table('DBTest')
->select(\DB::raw('MIN(date_start) AS DateStart, MAX(date_end) AS DateEnd'));
->get();
You can do it like this:
For the max start date:
max = DB::table('DBTest')->select('date_start')->orderBy('date_start', 'desc')->first();
For min end date:
min = DB::table('DBTest')->select('date_end')->orderBy('date_end', 'asc')->first();
You have to use something called selectRaw method in Laravel in order to achieve this result. Chaining method like ->max('columnA')->min('columnB') will not work. So, here is the solution:
$data = DB::table('DBTest')
->selectRaw('MAX(date_start) AS DateStart, MIN(date_end) AS DateEnd')->get();
Try MIN() and MAX() functions in the sql query.
$data = DB::table('DBTest')
->select(\DB::raw('MIN(date_start) AS startDate, MAX(date_end) AS endDate'));
->get();

Pagination shows error when use having in query builder

I have a query like the following, and it works as expected. I used here the havingRaw option to filter the result.
$customers = Customer::select(DB::raw("`name`, `mobile`, `branch`, count(*) as total_orders"))
->groupBy('mobile')
->havingRaw('total_orders > 12')
->orderBy('total_orders', 'desc')
->get();
As the total number of returned row will vary, i need to show it as paginated. So i changed the query like below. Then it shows error that the total_orders column not found.
$customers = Customer::select(DB::raw("`name`, `mobile`, `branch`, count(*) as total_orders"))
->groupBy('mobile')
->havingRaw('total_orders > 12')
->orderBy('total_orders', 'desc')
->paginate();
I found some workarounds for the previous versions of Laravel. I am using Laravel 5.6 for my project, is there any solution for this?
Try this..
$customers = Customer::select(DB::raw("`name`, `mobile`, `branch`, count(*) as
total_orders"))
->groupBy('mobile')
->having('total_orders','>', 12)
->orderBy('total_orders', 'desc')->get();

Resources