Coder Social home page Coder Social logo

Comments (7)

staudenmeir avatar staudenmeir commented on June 14, 2024 1

Define two separate relationships and combine them with an accessor:

class DeviceModel extends Model
{
    use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

    public function products()
    {
        return $this->hasManyJson('App\Product\Product', 'compatible_device_models_ids');
    }

    public function getProductVariantsAttribute()
    {
        $productVariants = $this->products->pluck('variants')->flatten();

        return new \Illuminate\Database\Eloquent\Collection($productVariants);
    }
}

class Product extends Model
{
    use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

    protected $casts = [
        'compatible_device_models_ids' => 'json',
    ];

    public function variants()
    {
        return $this->hasMany('App\Product\ProductVariant', 'product_id');
    }
}

Device::find($id)->productVariants;

Use eager loading to prevent unnecessary queries:

Device::with('products.variants')->find($id)->productVariants;

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024 1

I released a new version that adds support for HasManyThroughJson relationships (Laravel 9+):
https://github.com/staudenmeir/eloquent-json-relations#has-many-through-relationships

class DeviceModel extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function productVariants()
    {
        return $this->hasManyThroughJson(
            ProductVariant::class, Product::class, new JsonKey('compatible_device_models_ids'), 'product_id'
        );
    }
}

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

What do your tables look like?

from eloquent-json-relations.

ianfortier avatar ianfortier commented on June 14, 2024

Basically;

Model # 1 : Device Model
'App\Device\DeviceModel'
id

I need to query all the "Compatible ProductVariant Model # 3 " through the product (Model # 2)

Model # 2 : Product
'App\Product\Product'
id
compatible_devices_models_ids <--- JSON

`
public function variants()
{
return $this->hasMany('App\Product\ProductVariant', 'product_id');
}

public function compatible_device_models()
{
return $this->belongsToJson('App\Device\DeviceModel', 'compatible_device_models_ids');
}
`

Model # 3 : Product Variant
'App\Product\ProductVariant'
id
product_id

public function product() { return $this->belongsTo('App\Product\Product', 'product_id'); }

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

This is not supported at the moment. I'll look into it.

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

I don't think I'll add this relationship type anytime soon. The implementation would be quite complex and I don't see that many use cases.

In the meantime, you can imitate it by combining a HasManyJson and a HasMany relationship.

from eloquent-json-relations.

ianfortier avatar ianfortier commented on June 14, 2024

Can you provide an example of a combination of HasManyJson and HasMany for me and other people looking for a similar solution :)?

Feel free to close this issue! Thanks again for the help!

from eloquent-json-relations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.