Eloquent order model by its relationship hasManyTrough - laravel

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

Related

Creating a "through" relationship when both foreign keys are on the same table

I have a products table. A product belongs to a category and a country.
I would like to find out what countries a category has by leaning on this relationship.
I've considered hasManyThrough and https://github.com/staudenmeir/belongs-to-through but this is sort of a belongsTo relationship, with both foreign keys on an intermediate table, I suspect the products table is sort of acting like a pivot.
I would like to know how I can set up a relationship that relates categories to countries through the products table - is this possible?
Tables:
Categories
id
title
Countries
id
title
Products
id
title
category_id
country_id
Relationships:
A product belongs to a country / A country has many products.
A product belongs to a category / A category has many products.
You've pretty much described a Many to Many relationship with Product being the pivot table.
# Category model
public function countries()
{
return $this->belongsToMany(Country::class, 'products')
->withPivot(['id', 'title']); // optional
}
# Country model
public function categories()
{
return $this->belongsToMany(Category::class, 'products')
->withPivot(['id', 'title']); // optional
}

How to get data from third table in eloquent relationships eloquent many to many relationship

I am using Eloquent ORM and I have Book model which connect to BookCategory model and BookCategory connect to Category. The l'm problem facing is how to include data from third table in eloquent relationships?
Book
id
name
Category
id
name
type
BookCategory
id
book_id
category_id
Lets say for example you want to get all the books of a certain category:
assuming your pivot table name is Book_Category
in your Category model:
public function books()
{
return $this->belongsToMany('App\Models\Book', 'Book_Category', 'category_id', 'book_id');
}
and you can eager load category books like :
$categories = Category::get()->load('books');
//OR
$categories = Category::with('books')->get();
If I understand you right, you are looking for the pivot attribute. This makes additional columns of the intermediate table available.
https://laravel.com/docs/8.x/eloquent-relationships#retrieving-intermediate-table-columns

Laravel: Eager loading specific columns

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);

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