models structure it looks like the following
class Attribute extends Model
{
public function products()
{
return $this->belongsToMany(Product::class);
}
public function attribute_options()
{
return $this->hasMany(AttributeOption::class);
}
}
class AttributeOption extends Model
{
public function attribute_option()
{
return $this->hasMany(CombinationDetail::class);
}
public function attribute(){
return $this->belongsTo(Attribute::class);
}
}
class Combination extends Model
{
public function combination_details()
{
return $this->hasMany(CombinationDetail::class);
}
}
class CombinationDetail extends Model
{
protected $fillable = ['attribute_options_id'];
public function attribute_options()
{
return $this->belongsTo(AttributeOption::class);
}
}
Relationship models like this.
combination table
combination_details table
how can i show this with selectbox or div with javascript
Related
I'm working on a complex shopping cart project. I have relationships like this
CategoryGroup model
// App\CategoryGroup
class CategoryGroup extend Model
{
public function categories()
{
return $this->hasMany(Category::class);
}
}
Category model
// App\Category
class Inventory extend Model
{
public function categoryGroup()
{
return $this->belongsTo(CategoryGroup::class);
}
public function products()
{
return $this->belongsToMany(Product::class);
}
public function listings()
{
return $this->belongsToMany(
Inventory::class,
'category_product',
null,
'product_id',
null,
'product_id'
);
}
}
Product model
// App\Product
class Product extend Model
{
public function categories()
{
return $this->belongsToMany(Category::class);
}
public function listings()
{
return $this->hasMany(Inventory::class);
}
}
Inventory model
// App\Inventory
class Inventory extend Model
{
public function products()
{
return $this->belongsTo(Product::class);
}
}
Now I'm stuck on the situation where I need to create a relationship between The CategoryGroups and the Inventory model like this:
// App\CategoryGroup
class CategoryGroup extend Model
{
public function categories()
{
return $this->hasMany(Category::class);
}
public function listings()
{
// Can't figured out the way
// A belongsToMany like the App\Category would be great
}
}
Is there a good way to achieve this kind of relationship?
Laravel has no native support for a direct relationship.
I created a package for cases like this: https://github.com/staudenmeir/eloquent-has-many-deep
You can use it like this:
class CategoryGroup extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function inventories() {
return $this->hasManyDeep(
Inventory::class,
[Category::class, 'category_product', Product::class]
);
}
}
I created a relationship like this:
Type.City.Street.House.Apartment
In this relation, the apartment must depend on the House and on the Type at the same time, so result sql query must be like this:
select * from `apartments` where `apartments`.`house_id` in ('1', '2', '3') and `type_id` = '777'
The problem is that the HasManyThrough relationship only looks at a two of levels and it's not possible to get to the very first model with it.
Please, advise how this can be done?
My models definations:
class Type extends Model {
public function city() {
return $this->hasMany('App\City');
}
}
class City extends Model {
public function street() {
return $this->hasMany('App\Street');
}
}
class Street extends Model {
public function house() {
return $this->hasMany('App\House');
}
}
class House extends Model {
public function Apartment() {
return $this->hasMany('App\Apartment');
//->where('type_id', '=' type.id) ?????
}
}
class Apartment extends Model {
public $fillable = ['house_id', 'type_id']
}
I would add a mapping for house_id in Apartment model and sets bidirectional mappings among your models
class Apartment extends Model {
public $fillable = ['house_id', 'type_id'];
public function house() {
return $this->belongsTo('App\House', 'house_id');
}
}
class Type extends Model {
public function city() {
return $this->hasMany('App\City');
}
}
class City extends Model {
public function type() {
return $this->belongsTo('App\Type', 'type_id');
}
public function street() {
return $this->hasMany('App\Street');
}
}
class Street extends Model {
public function city() {
return $this->belongsTo('App\City', 'city_id');
}
public function house() {
return $this->hasMany('App\House');
}
}
class House extends Model {
public function street() {
return $this->belongsTo('App\Street', 'street_id');
}
public function Apartment() {
return $this->hasMany('App\Apartment');
//->where('type_id', '=' type.id) ?????
}
}
then you can query apartments as per your criteria like
Apartments::whereHas('house.street.city.type', function ($query) use ($type_id) {
$query->where('id', '=', $type_id);
})
->whereHas('house', function ($query) use ($house_ids) {
$query->whereIn('id', $house_ids);
});
And i guess there is no need for type_id in Apartments model
I created a HasManyThrough relationship with unlimited levels: Repository on GitHub
After the installation, you can use it like this:
class Type extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function Apartment() {
return $this->hasManyDeep(Apartment::class, [City::class, Street::class, House::class])
->where('apartments.type_id', $this->id);
}
}
Unfortunately, this doesn't work with eager loading.
I have 3 layers of model relationships
the Grandparent Class
class GrandParent extends Model
{
protected $guarded = [];
public function parents()
{
return $this->hasMany('App\Parent');
}
public function childs()
{
return $this->hasManyThrough('App\Childs', 'App\Parent');
}
}
the parent Class
class Parent extends Model
{
public function grandParent() {
return $this->belongsTo('App\GrandParent', 'grandParent_id', 'id');
}
public function child()
{
return $this->hasMany('App\Child');
}
}
The child class
class Child extends Model
{
public function parent() {
return $this->belongsTo('App\Parent', 'parent_id', 'id');
}
}
i want to get list of Child by the grandParent ID
There is any way to get the list by simple Query
i am using laravel 5.5
thanks.
You need to add the grandparent_id and user_id from the user_table like below.
public function childs()
{
return $this->hasManyThrough('App\Childs', 'App\Parent','grandparent_id','parent_id','id','id');
}
In your Controller file.
$data = Grandparent::find($grandparent_id)->childs;
I have the following models with polymorphic relationship
class Sector extends Model {
public function image() {
return $this->morphOne('App\Models\Image', 'imageable');
}
}
class Image extends Model {
protected $touches = ['imageable'];
public function imageable() {
return $this->morphTo();
}
}
I want to touch the parent (Sector) model's timestamps when updating the image of the sector. I have added the $touches variable on Image model.
But this is not working.
See if this works.
class Sector extends Model {
public function image() {
return $this->morphOne('App\Models\Image', 'imageable');
}
}
class Image extends Model {
protected $touches = ['sector'];
public function imageable() {
return $this->morphTo();
}
public function sector() {
return $this->morphedByMany('App\Models\Sector', 'imageable');
}
}
class SomeModel extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{some_table}}';
}
public function getItemByParentId($parentId)
{
$criteria=new CDbCriteria;
//some criteria
return self::model()->findAll($criteria);
}
}
This method works properly when I call it from controller
SomeModel::model()->getItemByParentId($someVariable)
But now I have 3 very similar to SomeModel models, so I want to put common methods to one class
class CommonModel extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function getItemByParentId($parentId)
{
$criteria=new CDbCriteria;
//some criteria
return self::model()->findAll($criteria);
}
}
class FirstModel extends CommonModel
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{first_table}}';
}
}
class SecondModel extends CommonModel
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return '{{second_table}}';
}
}
But get error
The table "CommonModel" for active record class "CommonModel" cannot
be found in the database.
What is wrong?
Try to change:
public function getItemByParentId($parentId)
{
$criteria=new CDbCriteria;
//some criteria
return static::model()->findAll($criteria);
}