aurelianspodarec's avatar

Laravel hasOneThrough() returns a boolean

Hi there!

So I've been reading this https://laravel.com/docs/9.x/eloquent-relationships#has-one-through and its exactly what I need I believe based on the explanation.

Now, I believe I've done this, however the posts return a '1' instead of the table content.

I have a table brand_colors that contains: id, name, hex, slug I have a table companies that contains id, brand_color_id I have a table pages that contains the id, company_id

I'm trying to get brand_color to show in pages.

I've written this in the Page mode:

 public function color()
    {
        return $this->hasOneThrough(BrandColor::class, Company::class, 'brand_color_id', 'id');
    }

I've tried a few variations, but not sure why when I display $page->color it shows me a boolean a 1. If i try to access the value like 'hex' it will say its null.

I'm bit confused on why its doing this.

0 likes
7 replies
MichalOravec's avatar
Level 75

Your table structure is not suitable for hasOneThrough.

You can get Page from BrandColor but not opposite.

Can you see the difference between your tables and tables from the documentation?

brand_colors
    id - integer
    name - string
    hex - string
    slug - string

companies
    id - integer
    brand_color_id - integer

pages
    id - integer
    company_id - integer
mechanics
    id - integer
    name - string

cars
    id - integer
    model - string
    mechanic_id - integer

owners
    id - integer
    name - string
    car_id - integer

For that you will need to use this package:

https://github.com/staudenmeir/belongs-to-through

BelongsToThrough relationship.

1 like
aurelianspodarec's avatar

@MichalOravec

Mechanic is the middle man - companies is the middle man; mechanic doesn't contain any other id than itself.

I think brand_color is meant to have an id of something in that case, but then that's not how it should work since brand_colors is basically a category in other words.

So I think in my example brand_colors would need to be associated with the page.

So I suppose I need to look for a different relationship right.

aurelianspodarec's avatar

@MichalOravec Use a package for that? Would you structure the data the way I do, or would you do it differently? I wonder if my data structure is just poor.

I'm just kinda hesitant to install that package, I mean, do I really need it or am I just writing poor data structure xd

Have you needed to use that package in your projects?

aurelianspodarec's avatar

@MichalOravec So would you say the way I have structured the data or maybe the way I query display it isn't the best way?

What do you think of this structure? https://i.imgur.com/OOpqta6.png The 'name' in 'website_pages' is meant to be a category as well, so it would have an id.

I'm building so you have a pages page that you display all page with its 'name', and then get its content. The content will be either mobile or desktop, and either dark or light, so you can have up to 4different variations for a page. Since page content is an variation like for one page.

However, the company is what decides on what brand color to have, and anything below should always have access to the company since company is the mother/parent.

I don't know. Do you see anything odd here?

The website_pages is kinda like animals, and then website_pages_content is kinda like dog, cat etc... but can show it only one. It should always be one pages content to display, and based on filter display one or the other.

Maybe I should create a new question and give full details xd

I feel like my data structure might not be the worst but something might be odd. The reason I also do it like that so everything can be versioned as well

If you didn't ever need to use that package than cleary I'm doing something wrong right xd since you worked on dozen project that were 1000more complex I bet, compared to what I'm building.

aurelianspodarec's avatar

@MichalOravec After days or a week, I figured out you can do this:

    {{ dd($props->company->color->name) }}

While having this code in the Company model:

 public function color() {
        return $this->belongsTo(BrandColor::class, 'brand_color_id');
    }

Not sure how efficient this data structure is for loading ton of data but uf.

Oh wait, but that's just showing the color, not actual filtering.

Please or to participate in this conversation.