This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Troiscent
Troiscent

Hello,

I want to say that I created an issue on stackoverflow, but without any answers :/

Thanks to the builder plugin, I created a "Recipe" object that have a "Category" as a relation. In the backend, I can choose the category, it works perfectly.

The problem is to display the category on the frontend. I load my Recipe object thanks to the builder component, but if I try to display {{ recipe.category }}, it return me the ID of the related category.

I don't know how to display the title of the related category instead of the ID

Any help would be appreciated.

Thanks

Ilesyt
Ilesyt

What kind of relation is it, and if it exists, what is the name of the field you used for the relation in recipe? If you used category instead of category_id, then there's your problem.

Troiscent
Troiscent

Hello,

Here is my relation :

public $hasOne = [
    'category' => ['Alex\Menus\Models\Category']
];

The category field is in a "Recipes" repeater that contain an image, title, category and price field.

As it's a hasOne relation and it's in a repeater, the category ID field is saved in the recipe json object in the database.

So, when I create a menu entry, I can add all my recipes thanks to the repeater, and I can categorize each recipes thanks to the relation. It perfectly works in the backoffice.

See attached screenshot to see how things are combined.

Thanks for your help

Alex

Troiscent
Troiscent

Still stuck with this. The solution is probably very simple but I tested all things I could without success.

If somebody know laravel and october cms system well, I'm interested !

Thanks

Troiscent
Troiscent

As I don't find a "clean" solution, I tried to get the category name directly by doing a query in the database.

In my partial, on the code section, I have :

function getCategoryName($id) {
  dump($id);
}

And here is my partial markup :

{% set records = builderList.records %}
<div class="grid">
  <div class="row">
    {% for record in records %}
    <div class="col-xl-3 col-lg-4 col-sm-6">
      <article class="record">
        <div class="content">
          <h3>{{ record.title }}</h3>
          <p class="price">{{ record.price }}</p>
          {% for recip in record.recipes %}
          {{ getCategoryName(recipe.category) }}
          {% endfor %}
        </div>
      </article>    
    </div>
    {% endfor %}
  </div>
</div>

But I get an error message :

Unknown "getCategoryName" function.

How could I make my query to retrieve the category name whereas I have the ID ? Thanks for your answer.

Ilesyt
Ilesyt

What is the repeater field for? Is it to add multiple recipes on a menu? Also, could you explain the structure of your category? Have you tried using {{ recipe.category.name }}?

Last updated

Troiscent
Troiscent

@ilesyt Yes, the repeater field exists because you can add multiple recipes on a menu ?

I tried {{ recipe.category.name }} but it return nothing. For october, {{ recipe.category }} is just an id.

Ilesyt
Ilesyt

So, the relationship would be like this: Recipe: belongsTo: 'category'

Category: hasMany: 'recipes'

And the recipe model should then have a category_id field

Set your relations up like that and then try it out.

Troiscent
Troiscent

Hello,

I have no recipe model, my recipes are just a jsonable variable (repeater) in my Menu model.

I tried to apply what you suggest, before I had a $hasOne relation in my Menu model like this :

    public $hasOne = [
        'category' => ['Troiscent\Menus\Models\Category']
    ];

It worked well in the backoffice, I could select the category in a dropdown in the "recipes" repeater, and the category ID correctly saved in the "recipes" $jsonable variable in the database.

Now, I changed the $hasOne relation like this :

    public $belongsTo = [
        'category' => ['Troiscent\Menus\Models\Category']
    ];

And I added a relation in the Category Model like this :

    public $hasMany = [
        'recipes' => ['Troiscent\Menus\Models\Menu']
    ];

But I don't see any changes compared as before. It still work in the backend, I can select my category in a dropdown in my repeater, it's correctly saved in the database. But {{ recipe.category.title }} still return nothing (and recipe.cateogy return the category ID).

I'm not sure if I defined the relations correctly. It works, but as my relation is in a repeater, I need to do some more things to "keep the relation" between my recipes and my categories.

If you have some suggestions

Thanks

Ilesyt
Ilesyt

Well, that is pretty much why, you don't have any model for recipes, so it basically doesn't have any relation defined. Create a model for recipes, don't forget to create the relationship between menu and recipes too.

Troiscent
Troiscent

Can I create a recipe model without changing the way I add recipes to menu (with a repeater) ? Cause I really like the backoffice and I don't want to change the way it's displayed.

The strange thing is that the backoffice can make the relation as it's able to display the category title in the menu dropdown, I wonder why I'm not able to do the same thing on the frontend.

rupert24306
rupert24306

I have this same problem.

When I was using $belongsToMany to link to models, with a pivot table, then it was straight forward to use a format like: {% for product in record.product %} {{ product.name }} {% endfor %}

But when I changed to a $hasMany and $belongsTo relationship then I could no longer display the content of the related model - just the id (as stored in the column in the db).

This was my Category model: public $hasMany = [ 'products' => [ 'Roojai\EutsProducts\Models\Product', 'table' => 'roojai_eutsproducts_products', 'order' => 'name'

And this was in my product model:

public $belongsTo = [ 'categories' => [ 'Roojai\EutsProducts\Models\Category', 'table' => 'roojai_eutsproducts_categories', 'order' => 'name' ],

The only solution for me was to go back to a $belongsToMany with Pivot table which works perfectly.

There must be a way to have a (one to many/many to one) relationship so that I can use a dropdown with single category selection rather than a checkbox with multiple selections. Any help appreciated.

Miguel Debruyne
Miguel Debruyne

So how did you guys fixed this? Having somewhat the same problem. In the backend it works fine but on the frontend i just get at column category_id and not the category data

Since this topic has lot's of views it seems we are not the only one scratching our heads on this.

Last updated

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.