Coder Social home page Coder Social logo

Comments (13)

staudenmeir avatar staudenmeir commented on June 14, 2024

The relationship requires routeParams to be an array:

{"icon": "img/categorias/automotivo.png", "route": "website.listaLocais",
  "routeParams": [{"slug": "automotivo"}]}
                 ^                      ^

Is payload an attribute in the Category model?

from eloquent-json-relations.

thiago-slb avatar thiago-slb commented on June 14, 2024

That's right, created like this $table->json('payload');

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

You'll have to use the inverse relationship:

public function categories() {
    return $this->hasManyJson(Category::class, 'payload->routeParams[]->slug', 'slug');
}

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

Does it work for you?

from eloquent-json-relations.

thiago-slb avatar thiago-slb commented on June 14, 2024

from eloquent-json-relations.

thiago-slb avatar thiago-slb commented on June 14, 2024

It didn't work, I copied your code at first, but also change to this:

public function option() {
    return $this->hasManyJson(Category::class, 'payload->routeParams[]->slug', 'slug');
}

and this

public function option() {
    return $this->hasMany(Category::class, 'payload->routeParams[]->slug', 'slug');
}

from eloquent-json-relations.

thiago-slb avatar thiago-slb commented on June 14, 2024

What i'm trying to achive is:

Each option has one category (slug at the moment), but the category slug is stored inside a json attribute, is that possible to do? I can store the category id instead of the slug if that helps... would that change anything?

I'm trying to loop through the data like this =>

foreach($options as $option){
    $option->payload->icon;
    $option->category->title;
    etc...
}

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

I see, I thought there can be multiple categories.

Use this and the original JSON data ("routeParams": {"slug": "automotivo"}):

public function category() {
    return $this->hasMany(Category::class, 'payload->routeParams->slug', 'slug');
}

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

Does it work for you?

from eloquent-json-relations.

thiago-slb avatar thiago-slb commented on June 14, 2024

No, the error is gone, but it doesn't find the relationship.

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

How are you using the relationship?

from eloquent-json-relations.

thiago-slb avatar thiago-slb commented on June 14, 2024

I had to give up since the deadline is soon, when I finish I'll get back to this and try again, but thanks anyway it help me understand a bit more about how the package works.

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

Can I close this?

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.