Laravel check if attributes have products - laravel

I have model Attribute, and I have model AttributeValue. I have relation products in AttributeValues belongsToMany. I want check in model Attribute if products exists in AttributeValues. I tried:
public function values()
{
return $this->hasMany(AttributeValue::class);
}
public function getProductsExistsAttribute()
{
return $this->values->each->products()->exists();
}
AttributeValue model:
public function attribute()
{
return $this->belongsTo(Attribute::class);
}
public function products()
{
return $this->belongsToMany(Product::class, 'product_attribute_values');
}
But is not work. I get error: Method Illuminate\Database\Eloquent\Collection::exists does not exist.. How I can resolve this issue?

Related

Laravel get all products from parent category or subcategory using eloquent

I would like to retrieve all products of chosen parent category.Product model have hasmany relation to product_category_mapping table.
If product have subcategory result like,
"MAinCatergory":[
{
id="",
name:"Drinks",
"ChildCategory":[
{
"id":1,
"name":"Juce",
"Products":[{
name:"apple juce",
price:10,
....
}]
}
]
}
]
}
If product under main category only return array like,
"MAinCatergory":[
{
id="",
name:"Drinks",
"Products":[{
name:"apple juce",
price:10,
....
}]
}
}
]
}
category table fields - id,name,parent_id
product table fields - id,name,price,..,
product-category-mapping table fields - id,category_id,product_id
category model
public function children()
{
return $this->hasMany('App\Models\Category', 'parent_id');
}
public function parent()
{
return $this->belongsTo('App\Models\Category', 'parent_id');
}
public function product_category()
{
return $this->hasMany('App\Models\ProductCategoryMapping', 'category_id');
}
product model
public function product_category()
{
return $this->hasMany('App\Models\ProductCategoryMapping','product_id');
}
product-category_mapping
public function product()
{
return $this->belongsTo(Product::class,'product_id','id');
}
public function category()
{
return $this->belongsTo(Category::class,'category_id','id');
}
Something like this might suffice.
App\Models\Category::with('children', 'product_category.product')->get()
Suggestion, try implement pivot many to many relation instead this product_category_mapping, then model relation would change a bit.
For pivot relation, you need to modify the Category model
public function products()
{
return $this->belongsToMany('App\Models\Product', 'product-category-mapping');
}
and in product Model
public function categories()
{
return $this->belongsToMany('App\Models\Category','product-category-mapping');
}
Note:This is not the complete integration, but to give you an idea, for full logic see https://laravel.com/docs/9.x/eloquent-relationships#many-to-many
in your product model add like this:
public function product_categorys(){
return $this->hasMany('App\Models\ProductCategoryMapping','product_id');
}
and in controller you can get inside your function like this Product::with('product_categorys')->get();

laravel eloquent: Get all groups for a member

I have 3 tables:
user: id,name
group: id,name,parent_id made with this) // this is a nested category
members: id,user_id,group_id,admin
and group_member: group_id,member_id
I want get all groups (include sub groups) for a member. Is it possible do it via laravel?
Group Modal:
public function parent()
{
$parent = $this->belongsTo('App\Group', 'parent_id');
return $parent;
}
public function children()
{
$children = $this->hasMany('App\Group', 'parent_id');
//$children->wherePublish(1);
return $children;
}
public function members()
{
return $this->hasMany(Member::class);
}
public function member()
{
return $this->belongsToMany(Member::class);
}
And Member model:
public function groups()
{
return $this->belongsToMany(Group::class);
}
public function group()
{
return $this->belongsTo(Group::class);
}
And user model:
public function members()
{
return $this->hasMany(Member::class);
}
I want something like this (to return all related groups):
auth()->user()->members()->groups()->get();
I got this error:
"Call to undefined method
Illuminate\Database\Eloquent\Relations\HasMany::groups()"
Please use this below query:
App/User::where('id',auth()->user()->id)->with('members.groups')->get();

Reach the subcategory from the Product model (Laravel)

I am trying to reach the last laravel model from the first one with eloquent relationships. How can I reach the Subcategory directly from Product?
The 3 Models I have:
Product (id, category_id, etc..)
public function categories()
{
return $this->belongsTo('App\Category', 'category_id');
}
Category (id, name)
public function products()
{
return $this->hasMany('App\Product', 'category_id');
}
public function sub_categories()
{
return $this->hasMany('App\SubCategory', 'category_id');
}
SubCategory (id, category_id, name)
public function categories()
{
return $this->belongsTo('App\Category', 'category_id');
}
I would've assumed I could reach it with
Product::find(1)->categories->sub_categories;
Am I missing something obvious here?
read this docs
Product
class Product extends Model
{
public function SubCategory()
{
return $this->hasManyThrough('App\Category', 'App\SubCategory');
}
}
In Addition add the subCategories method on Product model and also a product is belongs to a category not categories
and also always try to use camel case for the methods (subCategories)
replace your code with this
Product:
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
public function subCategories()
{
return $this->category()->subCategories;
}
Category:
public function products()
{
return $this->hasMany('App\Product', 'category_id');
}
public function subCategories()
{
return $this->hasMany('App\SubCategory', 'category_id');
}
SubCategory:
public function categories()
{
return $this->belongsTo('App\Category', 'category_id');
}
finally use this to get the subcategories
Product::find(1)->subCategories;
Product::find(1)->categories is an array. You will have to loop it to get the subcategory of each category. See below
$categories = Product::find(1)->categories;
foreach ($categories as $category {
//Get subcategories for each category
dump($category->sub_categories);
}

Laravel Retrieve related model through pivot table

So i'm a bit confused with this situation, I have the current relations:
User hasmany Favourite
favourites references to a Recipe model
favourites is a pivot table but when I retrieve the favourites for a given user like:
$user->favourites
i get the favourite with the pivot data, but I want to get the fields of the recipe
Recipe.php:
public function favourites()
{
return $this->hasMany('App\Favourite');
}
User.php:
public function favourites()
{
return $this->hasMany('App\Favourite');
}
Favourite.php:
public function user()
{
return $this->belongsTo('App\User','user_id');
}
public function recipes()
{
return $this->belongsTo('App\Recipe','recipe_id');
}
EDIT : there was no problem at all, just needed to call:
#foreach($user->favourites as $favourite)
{{ $favourite->recipe->name }}
#endforeach
You need to set your relation to Has Many Through instead of just has Many.
https://laravel.com/docs/5.2/eloquent-relationships#has-many-through
Recipe.php:
public function favourites()
{
return $this->hasManyThrough('App\Favourite', 'App\User');
}
User.php:
public function favourites()
{
return $this->hasManyThrough('App\Favourite', 'App\Receipe');
}
Favourite.php:
public function user()
{
return $this->hasManyThrough('App\User','App\Receipe');
}

Laravel Relationship problems: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

I must be missing something obvious. I've got a Invoice_detail Model:
class Invoice_detail extends Eloquent {
public function products()
{
$this->belongsTo('Product');
}
}
A Product Model:
class Product extends Eloquent {
public function invoiceDetails()
{
$this->hasMany('Invoice_detail');
}
}
but when I try to use this:
Route::get('/', function(){
$detail = Invoice_detail::whereId(27)->first();
return $detail->products;
});
I get: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
What am I missing here?
Yep - your relationship methods should have returns on them:
public function invoiceDetails()
{
return $this->hasMany('Invoice_detail');
}

Resources