How to add where between in the query? - php

in below query i need to get sum of only those product which are in between specific date how to add wherebetween query to get sum?
$order = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))
->leftJoin('orderdets', 'orders.id', '=', 'orderdets.order_id')
->where('product_id', $scheme[0]->product_id)
->groupBy('orders.customer_id')
->get();

you could use something like
$users = DB::table('users')
->whereBetween('votes', [1, 100])
->get();
check https://laravel.com/docs/6.x/queries to see any other ways to use where clauses

Related

I want to find duplicate data from the database and show sum of duplicate data in laravel 8

I want to get all the data from SALES and PURCHASES table but I want to show product name for the single time and also want to show the sum of its quanitity..
How can i do this in laravel 8 ?
What will be the query ??
$stock = DB::table('products')
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('product_id')
->get();
I dont have your whole query but use havingRaw to find the duplicates.
$dups = DB::table('tableName')
->select('col1_name','colX_name', DB::raw('COUNT(*) as `count`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('col1_name', 'ColX_name')
->havingRaw('COUNT(*) > 1')
->get();
Maybe something like that
$stock = DB::table('products')
->select('sales.*','purchases.*','products.name', DB::raw('products.name as `NbProduct`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('sales.id', purchases.id)
->get();

How do I write an eloquent query which joins, merges or unions these 2 queries into 1?

// 2 eloqent collections merged
$publicCategories = Category::where('menu', '=', 1)
->where('display_scope', 1)
->orderBy('parent_id')
->get();
$privateCategories = Category::where('menu', '=', 1)
->whereIn('id', $ids)
->orderBy('parent_id')
->get();
$categories = $publicCategories->merge($privateCategories);
// This query above does these 2 MySQL queries which are duplicated.
The result from this is correct, however, requires 2 queries.
How do I write an eloquent query which joins, merges or unions these 2 queries into 1?
Why you're getting it separately? You can use Orwhere for this.
$publicprivateCategories = Category::where('menu', '=', 1)
->whereIn('id', $ids)
->orWhere('display_scope', 1)
->orderBy('parent_id')
->get();
Update
$publicprivateCategories = Category::where('menu', '=', 1)
->where(function($q) use($ids){
$q->whereIn('id', $ids)->orWhere('display_scope', 1);
})
->where('id', '!=', 2)
->orderBy('parent_id')
->get();
By this, you'll get both(Public or private) categories.

Laravel Query Builder - Ordering on a Custom Column

I want to sort my Laravel query builder results on a custom column (concat of first_name and last_name).
What I have done is-
$summary = DB::table('service_rating')
->join('partners', 'partners.id', '=', 'service_rating.partner_id')
->join('users', 'users.id', '=', 'partners.user_id')
->select(
DB::raw("CONCAT( users.first_name,' ', users.last_name) as lawn_pro"),
DB::raw ('AVG(service_rating.rating) as rating'),
DB::raw ('COUNT(service_rating.rating) as jobs'),
DB::raw ('SUM(service_rating.rating) as payout')
)
->where('customer_id', '=', Auth::user()->id)
->whereRaw('service_rating.created_at >= DATE(NOW()) - INTERVAL '.$no_of_day_to_show.' DAY')
->groupBy('service_rating.partner_id')
->orderBy('lawn_pro', 'asc');
So, I am getting error for this line -
->orderBy('lawn_pro', 'asc');
And error is like this-
Can anyone please help ?
Apparently you are using the count() function on your query, this ignores the select attributes because we only want to know the count of the rows. Because of this, lawn_pro is not available in the query.
I would suggest to execute the query and then count the available rows.
$rows = $summary->get();
$count = count($rows);

DISTINCT method using Laravel 5 eloquent query builder

I'm struggling to get unique results when using the DISTINCT method in laravel. This is my query
//Get users that are part of this sector
$users = DB::table('users')->distinct()
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->get();
dd($users);
The result shows 3 users with the same ID, when I only want one of them in the results.
How should I change my query?
Try to use group by id
$users = DB::table('users')
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->groupBy('users.id')
->get();
dd($users);

Eloquent - join clause with string value rather than column heading

I have a question regarding join clauses in Eloquent, and whether you can join on a string value rather than a table column.
I have the code below querying a nested set joining parent/child records in a table 'destinations' via a table 'taxonomy'.
The second $join statement in the closure is the one causing an issue; Eloquent assumes this is a column, when I would actually just like to join on t1.parent_type = 'Destination' - ie, t1.parent_type should = a string value, Destination.
$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->on('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();
Is it possible to force Eloquent to do this? I've tried replacing 'Destination' with DB::raw('Destination') but this does not work either.
Thanking you kindly.
Another best way to achieve same is :
$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->where('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();
Replace your on with where
try using DB::raw("'Destination'")

Categories