Hello again, following a different post I had here, I decided to change my database structure and now I'm struggling again to get the data I need.
So now I have these tables:
Products table will hold fields that will be mutual to all products in the system and will have polymorphic relationships to other tables.
products table: name, product_desc etc. and productable_type, productable_id
The Product model has this function:
public function productable()
{
return $this->morphTo();
}
One type of a product will be Bean:
beans table: id, green_bean_id, roast_type_id, etc.
Model:
public function products()
{
return $this->morphMany(Product::class, 'productable');
}
green_beans table: id, name, country_id
Model:
public function country()
{
return $this->belongsTo(Country::class);
}
public function beans()
{
return $this->hasMany(Bean::class);
}
countries table: id, name, origin_id
Model:
public function origin()
{
return $this->belongsTo(Origin::class);
}
public function greenBeans()
{
return $this->hasMany(GreenBean::class);
}
origins table: id, name
Origin model:
public function countries()
{
return $this->hasMany(Country::class)->orderBy('name');
}
public function greenBeans()
{
return $this->hasManyThrough(GreenBean::class, Country::class);
}
I need to find origins that have beans that were not soft_deleted.
I tried all kinds of codes, but I get lost all the time, the last code I tried:
$o = Origin::with([
'greenBeans' => fn($gb) => $gb->with(['beans' => fn($b) => $b->with(['products', fn($p) => $p->whereNull('deleted_at')
->whereHas('products', fn($q) => $q->whereNull('deleted_at'))
])
])
])
->get();
And I get this error:
mb_strpos() expects parameter 1 to be string, object given
I tried to find a tutorial that will help me with all these with and whereHas etc. but I can't find something that helps me with my specific needs and I get lost for hours now.
Please, can someone help?