Access relation in forelse on the same model - laravel - laravel

i have a question about accessing relation in laravel model. I have category model which has relation to translation model and category model(same model/itself - one category can be displayed in other categories using pivot table CategoryToCategory):
category model:
public function childCategories(){
return $this->hasManyThrough('App\Models\Category',
'App\Models\CategoryToCategory',
'id_parent_category',
'id',
'id',
'id_category');
}
public function translation($locale = null)
{
if ($locale == null) {
$locale = \App::getLocale();
}
return $this->hasOne('App\Models\CategoryLanguage', 'id_category', 'id')->where('locale', '=', $locale);
}
Now i take current category + translation + child categories:
$category = Category::with('translation', 'childCategories')->active()->where('id', $id)->first();
And display all childs names:
#forelse($category->childCategories as $category)
{{ $category->translation->name }}
#endforelse
I works but each child category makes another query to get translation(it's not loaded for elements in forelse). So how to load relation "translation" in category model which is foreaching loaded relation?
I could query model CategoryToCategory to get all child categories + load translation for that model and result be the same:
$categories = CategoryToCategory::with('translation')->where('id_parent_category', $id)->get();
And there will be one query for translations. Still i'm curious about using first solution instead query categoryToCategory.
Any ideas?
Have a nice day!

To eager load a distant relationship you can use the dot notation like so:
$category = Category::with('translation', 'childCategories.translations')
->active()
->where('id', $id)
->first();
This is known as "nested eager loading" and is referenced in the docs here:
https://laravel.com/docs/5.8/eloquent-relationships#eager-loading

Related

How do I query data from a pivot table in Laravel 7 [duplicate]

Searching from pivot table using laravel.
Here is my table structure:
Product
id
name
Categories
id
name
product_category (Pivot table)
id
category_id
product_id
//products can have multiple categories
Product model:
public function categories(){
return $this->belongsToMany(Category::class, 'product_category');
}
What is the best way to search all products by category id?
Currently I am doing this way, and it seems not an efficient way:
//Controller
$categories = product_category::where('category_id',1)->get();
Now I have to loop through categories and then get Products and pass it to views? Any idea how to do this in an efficient way?
For this you could use the whereHas() method:
$categoryId = 1;
$products = Product::whereHas('categories', function ($query) use($categoryId) {
$query->where('id', $categoryId);
})->get();
The above will return all products that are in the Category where the id is equal to $categoryId.
You can eager load products for a given category. Try:
$category = Category::with('products')->where('category_id',1)->find(1);
When you do this, only 2 database queries will be executed: one for loading the category, and one for loading related products.
Then in your Blade view you can do:
#foreach($category->products as $product
{{ $product->name }}
#endforeach
You can use this inside your method in controller..this only works when $request->$query(search) have value .then here we use wereHas for get the relationship of model and with->() using for get pivot table values
->when($request->query('search'), function ($query)use($request) {
$q= $request->query('search');
return $query->whereHas('relation name', function (Builder $query) use ($q) {
$query->with('pivot table name.column name')
->where('pivot table name.column name', 'like', "%{$q}%")
});})

Join tables in Laravel Eloquent method

How to write this code in eloquent method ?
$product = DB::table('products')
->join('purchase', 'products.id', '=', 'purchase.id')
->join('sales', 'purchase.id', '=', 'sales.id')
->select('sales.*', 'purchase.*','products.*')
->get();
Create model Product and add one to many relationship with Purchase in Product model.
public function purchases()
{
return $this->hasMany('App\Models\Purchase');
}
Create model Purchase and add one to many relationship with Sale in Purchase model.
public function sales()
{
return $this->hasMany('App\Models\Sale');
}
Create model Sale.
You can retrieve data using following statement.
$products = Product::with('purchases.sales')->get();
Note: I am assuming the relationship as one to many you can also declare as per your data, also you can define one to many inverse relationship, please refer to laravel docs https://laravel.com/docs/8.x/eloquent-relationships#one-to-many.
You will get purchases and sales data in different key so you can use below syntax to loop over it.
foreach ($products as $product) {
foreach ($product->purchases as $purchase) {
//Purchase data for current product
foreach($purchase->sales as $sale){
//Sale data for current purchase
}
}
}

Laravel where condition not working on query WITH relation

i have a simple query that looks like this:
$category = Category::with('translation')
->with(['childCategories' => function ($query) {
$query->active();
}])
->where('id', $id)->first();
Scope and relation:
public function scopeActive($query)
{
return $query->where('active', 1);
}
public function childCategories()
{
return $this->hasManyThrough('App\SupremeShop\Models\Category',
'App\SupremeShop\Models\CategoryToCategory',
'id_parent_category', //category_to_categories
'id', //categories
'id',
'id_category'); // category_to_categories.
}
So i am looking for category, some translations and child categories of given "main" category. And query returns 15 child categories. But in query is scope that should only take active ones and it's not working correctly. When i use dd it shows inactive child categories also. I've tried to remove scope and write simple WHERE but result was the same.
Have somebody any ides why condition is not working properly?
I believe the problem is the column is ambiguous since Category and its relation childCategories are both the same model and share the same table.
The active() scope is getting applied to the parent Category instead of the child Categories.
Try and see if this works
$category = Category::from('categories as c')
->with(['childCategories' => function ($query) {
$query->active();
}])
->where('id', $id)->first();

best approach to search from pivot table using laravel

Searching from pivot table using laravel.
Here is my table structure:
Product
id
name
Categories
id
name
product_category (Pivot table)
id
category_id
product_id
//products can have multiple categories
Product model:
public function categories(){
return $this->belongsToMany(Category::class, 'product_category');
}
What is the best way to search all products by category id?
Currently I am doing this way, and it seems not an efficient way:
//Controller
$categories = product_category::where('category_id',1)->get();
Now I have to loop through categories and then get Products and pass it to views? Any idea how to do this in an efficient way?
For this you could use the whereHas() method:
$categoryId = 1;
$products = Product::whereHas('categories', function ($query) use($categoryId) {
$query->where('id', $categoryId);
})->get();
The above will return all products that are in the Category where the id is equal to $categoryId.
You can eager load products for a given category. Try:
$category = Category::with('products')->where('category_id',1)->find(1);
When you do this, only 2 database queries will be executed: one for loading the category, and one for loading related products.
Then in your Blade view you can do:
#foreach($category->products as $product
{{ $product->name }}
#endforeach
You can use this inside your method in controller..this only works when $request->$query(search) have value .then here we use wereHas for get the relationship of model and with->() using for get pivot table values
->when($request->query('search'), function ($query)use($request) {
$q= $request->query('search');
return $query->whereHas('relation name', function (Builder $query) use ($q) {
$query->with('pivot table name.column name')
->where('pivot table name.column name', 'like', "%{$q}%")
});})

Link two tables in one in Laravel 5.5

$category = Category::all();
$product = Product::all()->where('category_id', $category->name)->get();
Change category id from its name and link two tables in one in Laravel 5.5.
kindly refer to Laravel documentation regarding setting up relationships:
https://laravel.com/docs/5.5/eloquent-relationships
The relation between Category & Product is one to many, so it will be:
// app/Category.php
public function products()
{
return $this->hasMany(Product::class);
}
You need to return a view and pass the data instead of returning a collection:
return view('some.view', [
$category => Category::all();
$product => Product::where('category_id', $category->name)->get();
]);
https://laravel.com/docs/5.5/views#passing-data-to-views
Also, when you do this:
$product=Product::all()->where('category_id',$category->name)->get();
You're loading all products from DB into memory and then filtering them because all() is executing the query. Do not do this.

Resources