I have Order table where user orders will be stored with their id and also have status of 0 / 1 to show if that order is paid by user or no.
Now I want to show in their profile that for example they have 10 orders and 3 paid.
I can get that number of total orders which is 10 but i have problem with getting the number of those 3 orders that has been paid.
Currently I have this:
$ordersd = DB::table('orders')->where('status', Auth::user()->username)->pluck('status')->count();
But it is not what I need cause I need to specified that count for me only orders with status 1 which belongs to logged user.
How do I do that?
Thanks.
Why are you comparing status with Auth::user()->username inside where condition ?
Does it makes any sense ?
$ordersd = DB::table('orders')->where('status', Auth::user()->username)
->pluck('status')->count();
So, change it to
$ordersd = DB::table('orders')
->where('user_id',Auth::user()->id)
->where('status', 1)
->count();
Hope you understand.
You should be able to do this to get the count of the paid orders:
$ordersd = DB::table('orders')
->where([ [ 'status', '=', 1 ],
[ 'user_id', '=', Auth::user()->id]
])
->count();
To get the total number of orders:
$ordersd = DB::table('orders')
->where('user_id', Auth::user()->id)
->count();
Related
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();
please what is the correct way of getting data from the MN table where one product has assigned multiple combinations of Mark / Model / Year.
E.g. product with ID 1 has following in mmis table:
id | product_id | mark_id | model_id | year_id
178 1 1 1 2
177 1 2 1 3
176 1 3 1 1
other tables are: marks, models, years.
It's completely fine getting all requested data with just:
$items = ProductMmi::where('product_id', $id)
->with(['mark', 'model', 'year'])
->get();
The problem is, that in this way I am not able to sort result based on mark.name ASC, model.name ASC, year.name ASC.
Thank you for any advice.
update:
forgot to add my second try - ordering still in troubles...
$items = ProductMmi::select([
'product_mmis.*',
'marks.*',
'mark_models.*',
'mark_model_years.*'
])
->join('marks', 'product_mmis.mark_id', '=', 'marks.id')
->join('mark_models', 'product_mmis.model_id', '=', 'mark_models.id')
->join('mark_model_years', 'product_mmis.year_id', '=', 'mark_model_years.id')
->where('product_mmis.product_id', $id)
->orderBy('mark', 'asc')
->orderBy('model', 'asc')
->orderBy('year', 'asc')
->get();
you can use this way
$items = ProductMmi::where('product_id', $id)->with(['mark'=>function($query){
$query->orderBy('your_column_name','ASC');
}])->with(['model', 'year'])
->get();
or one thing if you dont want to edit your query then in the model you can do also like this
public function mark()
{
return $this->hasMany(Mark::class)->orderBy('mark','asc');
}
I think the solution was to add orderBy methods between specific joins. E.g. the following case is working completely properly:
$items = ProductMmi::select([
'product_mmis.*',
'marks.*',
'mark_models.*',
'mark_model_years.*'
])
->join('marks', 'product_mmis.mark_id', '=', 'marks.id')
->orderBy('mark', 'asc')
->join('mark_models', 'product_mmis.model_id', '=', 'mark_models.id')
->orderBy('model', 'asc')
->join('mark_model_years', 'product_mmis.year_id', '=', 'mark_model_years.id')
->where('product_mmis.product_id', $id)
->orderBy('year', 'desc')
->get();
Is there a better way to have it done in a more efficient way? Thanks.
I'm working in laravel and I need to filter the users who orders with certain amount of money in orders
so I used to filter the orders count
User::where('type','client')
->has('orders', '>=', $min_orders)
->has('orders', '<=', $max_orders)
->withCount('orders')
->paginate(25)
;
Try this one
$users = User::filter(function ($user) use ($min_orders, $max_orders) {
return ($user->orders()->sum('price') >= $min_orders) && ($user->orders()->sum('price') <= $max_orders);
})->get();
Haven't tested, but I think it should work
If you want SUM with User info then you need to GROUP BY on user info. And If you want the SUM on the Many side table, then you have to JOIN to that table. Assuming you have orders.amount field, you can try this :
<?php
User::select('user.id', 'email', 'name', 'type', \DB::raw('SUM(orders.amount) as amount_sum'))
->where('type','client')
->join('orders', 'orders.user_id', '=', 'user.id')
->has('orders', '>=', $min_orders)
->has('orders', '<=', $max_orders)
->withCount('orders')
->groupBy('user.id', 'email', 'name', 'type')
->havingRaw('amount_sum > ?', [50])
->orderBy('amount_sum', 'desc')
->paginate(25);
This way you can paginate directly into One single query results. But remember that each User column you want to use in SELECT has to be used in GROUP BY too.
I have two items in a table called products with id's 1 and 2. There is a table called invite which has foreign key product_id for products.
In my controller below, i am trying to count the number of product id's that is existing in the invite table.
eg
Product invite
id name id token product_id user_id
1 cal 1 ..d 1 2
2 del 2 dd.. 2 2
3 mac 3 ..gh4 2 2
As above, id's 1 and 2 exist in the invite table. meaning the total count is 2 (although product id 2 appears twice.
When i run my code below, i get a count of 1 instead of 2. What could i be missing out, please?
NB: in this case, i am user just one user
Controller
public function dashboard()
{
$products = Products::orderBy('created_at', 'desc')
->where('user_id', Auth::user()->id)->get();
$get_products_id = [];
foreach($products as $product){
$get_products_id[] = $product->id;
}
$implode_data = implode(',',$get_products_id);
$count_existing_products =
Invite::where('product_id',$implode_data)
->where('user_id', Auth::user()- >id)->get()->count();
return view('dashboard', compact('count_existing_products'));
}
View
<h1>{{count_existing_products}}}</h1>
For WHERE IN clause laravel uses special whereIn() method where you pass array, not string. So, your query becomes:
Invite::whereIn('product_id', $get_products_id)
->where('user_id', Auth::user()->id)
->distinct() // added `distinct` call to get distinct values
->get()
->count();
If distinct does not work, try to groupBy:
Invite::whereIn('product_id', $get_products_id)
->where('user_id', Auth::user()->id)
->groupBy('product_id')
->get()
->count();
There is no need to use implode. you can use whereIn instead of where.
Invite::whereIn('product_id',$get_products_id)
->where('user_id', Auth::user()- >id)->get()->count();
I'm tring to retrieve the totals of items from a table but can't get my head around it.
What I'm trying to achieve is this:
I have a table 'users' with the following columns:
Admin - yes/no
Moderator - yes/no
I am trying to retrieve the total of the items in the table but also the total of items in the table with admin = 'yes' OR moderator = 'yes'. The reason for this is to display the totals in one table.
Below is my sample code:
$date = new DateTime('tomorrow -360 month');
$usersPerDay = User::select(array(
DB::raw('DATE(`created_at`) as `date`'),
DB::raw('COUNT(*) as `count`'),
this is where I have the problem - Is it possible to do something like
this
DB::raw('COUNT(*) as "type" WHERE admin= "yes" OR moderator= "yes"')
))
->where('created_at', '>', $date)
->groupBy('date')
->orderBy('date', 'DESC')->get();