Laravel: Eager loading specific columns - laravel

Why does
Order::with(['products'=>function($q){
$q->select('name', 'price', 'quantity')->orderBy('name','asc');
}])->paginate($length);
returns all orders with their respective product data, but
Order::with(['products'=>function($q){
$q->select('name', 'price', 'quantity')->orderBy('name','asc');
}])->select('pickup_date', 'pickup_time', 'remark')->paginate($length);
gives me all order data I want, but an empty products array?
I want to select some specific columns from the order table with some specific columns from the products table. How can I do this?
FYI:
Orders-products have a many-to-many relationship with models:
Order Model:
public function products()
{
return $this->belongsToMany('App\Models\Product')->withTimestamps();
}
Product Model:
public function orders()
{
return $this->belongsToMany('App\Models\Order')->withTimestamps();
}

You need to select like this way :
Order::with(['products'=>function($q){
$q->orderBy('name','asc');
}])->select('products.name as name','products.price as price','products.quantity as quantity','orders.pickup_date as pickup_date', 'orders.pickup_time as pickup_time', 'orders.remark as remark')->paginate($length);
Or without sub query :
Order::with('products')
->select('products.name as name','products.price as price','products.quantity as quantity','orders.pickup_date as pickup_date', 'orders.pickup_time as pickup_time', 'orders.remark as remark')
->orderBy('name','asc');
->paginate($length);

You are missing one thing,you should add product id in outer select.
Order::with(['products'=>function($q){
$q->select('name', 'price', 'quantity')->orderBy('name','asc');
}])->select('product_id','pickup_date', 'pickup_time', 'remark')->paginate($length);
I hope it would be helpful

Try:
Order::query()
->select(['pickup_date', 'pickup_time', 'remark'])
->with(['products:name,price,quantity'])
->paginate($length);

Related

Laravel eloquent many to many relation deep conditions

I have two Model named Product and Category. Admin can change the status of Product and Category.
Here Product and Category has many to many relations. So that, all Product can belongs to multiple Category
Now at my user panel,
I want to show that all Product whose all Category's status is active.
you can do this in your product model
public function categories(){
return $this->belongsToMany(Category::class, CategoryProduct::class, 'product_id', 'category_id');
//->withPivot([]); if you need anything from the pivot table for any reason you can add the column names in the array
}
this is assuming that the pivot table have the columns product_id and category_id
$products = Product::with('categories')
->whereHas('categories', function ($query) {
$query->where('categories.status', 'active');
})
->get();
inside the where the word categories is the table name
The solution is:
$products = Product::with('category')
->whereHas('category', function ($query) {
$query->where('status', 'active'); // use your column & condition
})
->get();
It works for me, got all the products that have all the categories active. First time I thought complicated so I didn't solve.
$products = Product::whereDoesntHave('catagories',function($category){
$category->where('status','!=',1);
});

Eloquent order model by its relationship hasManyTrough

I have 3 tables
products, product_categories and categories
product_categories is a pivot table that help me on the relationship between products & categories.
The relation is as follow in Product.php model:
public function categories() {
return $this->hasManyThrough(Category::class, ProductCategory::class, 'product_id', 'id', 'id', 'category_id');
}
So now I need to query the products so I am doing the following:
Product::with('options')
->with('options.price')
->with('categories')
->paginate(25);
I can not find the way to sort my products list by the category name on the categories table...
is there any way?
My products table is quite large so it wont be possible to get all the results first and then sortBy()->take(25);
EDIT
The categories table has multiple categories and subcategories... the product_categories relate products with only subcategories...
Basically what I want is to sort the products by the main category

Laravel Fetching one to Many Relationship

Hello I am learning laravel and I am having an issue retrieving data from my relations.
In my database there are Product and Groups filled with dummy data.
I defined my relationship like this in product model:
public function Group()
{
return $this->hasMany('App\Groups','product_id', 'id');
}
And in my group vice versa with :
public function Product()
{
return $this->belongsTo('App\Product','product_id', 'id');
}
The way I am referencing to my products table is :
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
Now I have column product_id in my database under groups, and it is linked to if from products id it seems.
The groups table contains of its auto incremented id and product_id foreign key column.
While products table has auto incremented id and name column.
The issue is here :
How do I return the products that are not null or have value (of products id) in groups table.
I tried something like this in my filter controller:
public function getProductsWithGroup()
{
$Products = Product::with('groups')->get();
return $Products ;
}
But that is giving me call to undefined relations.
I am not sure how to access belongsTo or hasMany methods and whether I need an extra group_id column in my products table.
You named the relationship wrong. It should be groups & define in lowercase as
public function groups()
{
return $this->hasMany('App\Groups','product_id', 'id');
}
And use ->has() to check existence
public function getProductsWithGroup()
{
$Products = Product::has('groups')->get();
return $Products ;
}
->with() is used to eager load and ->has() is used to check existence & filter.
To get the products don't have any groups,
$Products = Product::doesntHave('groups')->get();
To see other ways to use ->has() check, https://laravel.com/docs/5.7/eloquent-relationships#querying-relationship-existence

How to connect three tables in Laravel?

I have thee tables: Orders, Products, Products_images.
I need to get ll orders, for this I do:
$orders = Order::with("products")->where("user_id", Auth::guard('api')->user()->id)->orderBy('id')->get();
Where with("products") is function in model Order
public function products()
{
return $this->hasOne("App\Product", "id", "product_id");
}
So, I have connected two table. Also I need connect with table Products with table Products_images in this query.
How can I do this?
Add a product_images function to the Products Model
public function product_images() { return $this->hasMany("App\ProductImage");}
Modify the above line's App\ProductImage to reflect the model for your table. Then you can access all the product images records that belongs to your products by doing the following:
$orders = Order::with("products.product_images")->where("user_id", Auth::guard('api')->user()->id)->orderBy('id')->get();
Look under nested eager loading on this link : https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

Laravel Get products from multiple taxonomies

I have 3 tables, products, taxonomies and product_taxonomy. The 3rd table is a pivot table that contains product_id and taxonomy_id, meaning that product and taxonomy are many to many relationship. Given a list of taxonomy ids, how can I get all the products that belong to these taxonomies? NOTE: I want to have the products result set being able to paginated or order by maybe it's price or something.
You need to create many-to-many relationship and that requires relationship methods like:
// Product Model
public function taxonomies()
{
return $this->belongsToMany('Taxonomy');
}
// Taxonomy Model
public function products()
{
return $this->belongsToMany('Product');
}
The query:
$listOfTaxonomyIds = [1,2,3];
$products = Product::whereHas('taxonomies', function($query) use ($listOfTaxonomyIds){
$query->whereIn('taxonomy_id', $listOfTaxonomyIds);
})->get();

Resources