I have a table of orders, which contain line items, which are stored in a pivot table.
Once all of the line items have been successfully processed, the order will be marked as "processed" and needs to be displayed on the page.
I therefore want to fetch all orders which have been marked as "processed" as well as the line items which are included in their respective orders.
My query looks like this:
$orders = DB::table('order_product')
->join('products', 'order_product.product_id', '=', 'products.product_id')
->join('orders', 'order_product.order_id', '=', 'orders.order_id')
->join('customers', 'orders.customer_id', '=', 'customers.customer_id')
->where('order_product.variation_status', '=', 'dispatch')
->where('orders.store', '!=', 'null')
->groupBy('order_product.order_id')
->get();
return response()->json($orders);
My thinking is to fetch all the pivot table items which are processed and then group the result by order_id but this unfortunately doesn't work.
I receive the following output:
Unfortunately the variation property only contains one line item from the pivot table, instead of two.
Can somebody help as to what I might be doing wrong?
UPDATE
Here are my models
Order.php
/**
* The products that belong to the Order.
*/
public function products()
{
return $this->belongsToMany('App\Product','order_product','order_id','product_id')
->withPivot(['qty', 'variation', 'variation_status'])
->withTimeStamps();
}
/**
* The customer that belongs to the Order.
*/
public function customer()
{
return $this->belongsTo('App\Customer', 'customer_id');
}
Product.php
/**
* The orders that belong to the product.
*/
public function orders()
{
return $this->belongsToMany('App\Order')
->withPivot(['qty', 'variation_status'])
->withTimeStamps();
}
I can't guarantee that this is correct right away because I normally don't work with ->withPivot and have no test environment running yet so this is from the top of my head at the moment. But this might give you an insight of how to handle this usecase.
Let's start with order as a base
Order::get();
Now let's extend this functionality to retrieve an order with a customer and products
Order::with('customer', 'products')->get();
What we've to do now is resolving your where conditions in the above eloquent query:
->where('order_product.variation_status', '=', 'dispatch')
->where('orders.store', '!=', 'null')
What you could do is the following:
Order::with(['products' => function($query){
$query->where('variation_status', 'dispatch');
}, 'customer'])
->where('store','!=','NULL')
->get();
Related
I am trying to make a query using Laravel eloquent but at the moment I have not had good results.
My query is about the scope of relationships in Laravel. We have two tables:
table 1 : orders
table 2 : products in orders (depends on table 1)
We have a relationship in the model.
public function products()
{
return $this->hasMany(OrderProduct::class);
}
OrderProduct (detail of products in orders) has the following fields:
id
order_id
product_id
qty
line_total
What we are trying to achieve is a query that returns the sum of line_total when the product_id is 139.
We tried the following options without success in the controller:
$orderspaid = Order::with('products')
->where('customer_id', '=', Auth::id())
->where('status', '=', 'completed')
->withSum ('products','line_total')
->where('product_id', '=', '139')
->get();
Error: Column not found: 1054 Unknown column 'product_id'
$orderspaid = Order::withCount(['products as orderproducts' => function($query) {
$query->where('orderproducts.product_id', '=', 139)
->select(DB::raw('sum(line_total)'));
}])->get();
But with no success.
My main question is, it is possible to use sum(line_total) or withSum('products','line_total') to directly sum the amount of money that a particular product_id have?.
Additional Info: Tinker information displaying the relationship between orders and orderproducts.
You can try this one. I don't have those tables ready to test so I could be wrong
So basicly, the method being tried is that products with wanted id will be preloaded, in this case, it's 139. When withSum is called on products table, it will use eagerly products that have been specified beforehand.
$product_id = 139;
$orderspaid = Order::with(['products' => function ($query) use ($product_id) {
$query->where(`products.id`, $product_id);
}])
->where('customer_id', '=', Auth::id())
->where('status', '=', 'completed')
->withSum('products', 'line_total')
->get();
dd($orderspaid);
Tell me if that works for you.
I have a model called unit that has this relationship
/**
* Get the users associated with the unit
*/
public function users()
{
return $this->hasMany('App\Models\User\UserData');
}
In the UserData model there is a column called user_id which I am trying to put in my condition in my query. I am trying to do a query like this
Unit::where('user_id', Auth::id())->first()
but there is no user_id column in the Unit table, only though the users relationship
Ended up doing this
Unit::whereHas('users', function($q) {
$q->where('user_id', Auth::id());
})->first();
I'm stuck with a problem where I have to sort / order a collection of models by their relationship's data.
I've got it setup like this:
Models:
User, Team, TeamUser, Role
The TeamUser model is a pivot model / table (containing user_id and team_id.
If it's worth mentioning I am also using spatie/laravel-permissions for the roles.
How would I go forth when I want to sort the users in a team by their role.name?
I'm talking about the users() relation in the Team model (see further down for code sample).
Some users have the role team-leader and most have the role team-seller. I've tried doing a ordinary ..->sortBy('role.name') but that doesn't seem to work. Thanks in advance if anyone could help me out.
User.php
/**
* Team relation
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function team()
{
return $this->belongsToMany('App\Team', 'team_users', 'user_id', 'team_id');
}
Team.php
/**
* User relation
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany('App\User', 'team_users', 'team_id', 'user_id')->withTimestamps();
}
if you want to order the result based on nested relation column, you must use a chain of joins:
$values = Team::query()
->leftJoin('users', 'users.team_id', '=', 'teams.id')
->leftJoin('model_has_roles', function ($join) {
$join->on('model_has_roles.model_id', '=', 'users.id')
->where('model_has_roles.model_type', '=', 'app\Models\User');
})
->leftJoin('roles', 'roles.id', '=', 'model_has_roles.role_id')
->orderBy('roles.name')
->get();
i have tried it, it work just fine.
please note that if you want to order by multiple columns you could add 'orderBy' clause as much as you want:
->orderBy('roles.name', 'DESC')->orderby('teams.name', 'ASC') //... ext
I have this database structure
table users table office_user table offices
----------- ----------------- -------------
id * id * id *
full_name user_id name
office_id
joined_at
So in my project every office has many users and user can be joined to many offices in date (joined_at)
User.php model
public function offices()
{
return $this->belongsToMany('App\Office)->withPivot('joined_at');
}
Office.php model
public function users()
{
return $this->belongsToMany('App\User)->withPivot('joined_at');
}
OfficeController.php
public function show(Office $office)
{
$users = User::with(array('phones', 'offices' , function($query)
{
$query->orderBy('joined_at', 'desc');
}))->get();
return view('dashboard.offices.show', compact(['office', 'users']));
}
I need two things :
1- Get current users list for every office
2- Count of current users in every office
I already achieve this:
<h3>{{ $office->name }}</h3><span>{{ $office->users->count() }}</span>
#foreach ($office->users as $user)
<li>{{ $user->full_name }}</li>
#endforeach
But the result is not as expected it gives me all users in certain office and count of them regardless there joined date
I want the list of last joined users to this office and count of them according joined_at field in pivot table
Thank you and Sorry for my english
But the result is not as expected it gives me all users in certain office and count of them regardless there joined date
When you do $office->users->count() that is the expected behavior because you are retrieve all the associated users of every office at any time, so given that you returned all this users, the count() executed in the collection will count all of them.
Your pivot attribute is just a timestamp, so how would you reduce the number of users returned? users that joined the office today/in the last hour/in the last 15 min maybe?
If so, you can add constrains to your count() method to get the results you want.
As an example, in the following lines we are gonna constraint the associated offices that has a joined_at that belongs to today:
public function show(Office $office)
{
$users = User::with([
'phones',
'offices' => function ($offices) {
$offices->whereDate('joined_at', '>', now()->startOfDay());
},
])->get();
return view('dashboard.offices.show', compact([office, 'users']));
}
Check this section of the documentation:
Constraining Eager Loads
Sometimes you may wish to eager load a relationship, but also specify
additional query conditions for the eager loading query. Here's an
example:
$users = App\User::with(['posts' => function ($query) {
$query->where('title', 'like', '%first%');
}])->get();
In this example, Eloquent will only eager load posts where the post's
title column contains the word first. You may call other query
builder methods to further customize the eager loading operation:
$users = App\User::with(['posts' => function ($query) {
$query->orderBy('created_at', 'desc');
}])->get();
I have three table one is orders table and another is order_status table and another is status table. Purpose of order_status table is to keep a track order's events. My table has the following column.
Order table
----------------------------
id | ref_num | name | email |
-----------------------------
Order status table has
---------------------------
order_id | status_id
---------------------
My models are like this
Order model
public function orderStatus(){
return $this->hasMany(OrderStatus::class');
}
Order status model
public function detail(){
return $this->belongsTo(Status::class,'status_id','id');
}
public function order(){
return $this->belongsTo(Order::class);
}
Now i want to get all those order which are still pending.
How can i do so ?
I tried to retrive like this but it failed
$data['orders']= Order::with(['orderStatus' =>function($q){
$q->with('detail')->latest()->where('status_id',2);
}])->latest()->take(10)->get()->toArray();
This return only one after that it does not.
Can anyone please tell me how can i sort this one ??
Thanks
PS:: one order can have multiple status such as unpaid, pending, packageging, in transit and so on but in sequence ofcouse
I added order status table image.. As u can see E7E7FF0EB7 order number has two records 1,and 2 means it was pending and then later stage got delivered.or you can say got processed. where as E02EAEA4BE has only one record of status 1. which means it is still pending.
So i want to get only those which are still pending.Not delivered.
This kinda sound complicated, hope i able to explain properly what i am trying to do.
Your model relations should be changed to a proper many to many. The schemas look correct so I'd make the following changes:
// Order model
public function statuses(){
return $this->belongsToMany(Status::class);
}
// Status model
public function orders(){
return $this->belongsToMany(Order::class);
}
This will pivot correctly on order_status.
To get pending orders the query would be:
Order::whereHas('statuses', function ($query) {
// assuming a 'name' column on statuses table
$query->where('name', 'pending');
// or using dynamic where
// $query->whereName('pending');
})->get();
Alternatively, add a scope to Order model:
public function scopePending($query) {
return $query->with(['statuses' => function ($query) {
$query->where('name', 'pending');
});
});
Usable then as: Order::pending();
Update
Try this to get all those order which are still pending..
$data['orders'] = Order::has('orderStatus', '=', 2)->whereHas('orderStatus', function ($q) {
$q->where('status_id', 2);
})->get()->toArray();
If there is two status records related to one order and one of the status value is 1 then this query will return the order record. You may update it with your exact conditions.(If you are sure that there will be only 2 status related to a order which is still pending then you may remove the second whereHas.
$data['orders'] = Order::has('orderStatus', '=', 2)->get()->toArray();
You may use the many to many relation as #DigitalDrifter suggested. I would also suggest that you should follow many to many relation.
If you are using the many to many relation then you may try the below query..
Order::has('statuses', '=', 2)->WhereHas('statuses', function ($query) {
$query->where('name', 'pending');
})->get();
or
Order::has('statuses', '=', 2)->get();