How to achieve this sceario in Laravel Nova? DB json column or 1-to many relationship?
I'm trying to achieve a context in Nova but not sure how to properly do it. The context is like this:
I have a Post Nova Resource that has some fields. Most of the fields are just html fields to show some information that is coming from an API request, like for example:
Html::make('Slug')
->html(function () use ($postInformation) {
if (!empty($postInformation)) {
return '<div>
<h1>Info from API: Slug</h1>
<p>' . $postInformation[‘slug'] . '</p>
</div>';
}
})->hideWhenCreating(),
But there is also one field “comments”, where is possible to add some generic notes to posts. The Posts table has a ‘notes’ field that is a json db column:
new Panel(‘Notes’, [
Flexible::make(‘Notes’)
->addLayout(‘Notes’, ‘note’, [
Textarea::make('Message'),
])
->button('Add MNoe’),
]),
Now I need to get the categories that are coming from the same API request $postInformation:
$postInformation = (new postInformation)->handle(['field' => 'slug', 'value' => $this->slug]);
dd($postInformation['categories']['data']);:
that are on this format below where there is an array that comes from an API request that has a 'categories' key with the top categories:
categories:
^ array:40 [
"id" => 2
...
"categories" => array:2 [
"data" => array:15 [
0 => array:3 [
"id" => 6
"name" => array:1 [
"en" => "General"
]
"on_request" => 0
]
1 => array:3 [
"id" => 14
"name" => array:1 [
"en" => "Tuts"
]
"on_request" => 0
]
2 => array:3 [
"id" => 3
"name" => array:1 [
"en" => "Laravel"
]
"on_request" => 0
]
...
]
...
]
And I need to have a panel on the posts resource with all categories that are coming from the api ($postInformation). And for each category name it should be possible to associate an icon_name to it through the nova cms.
So I need have to have this field on nova cms on the Posts resource to add an icon_name to each category that is coming from the API request. But before I need to get all the categories from the api request and store them in the database.
But Im not understanding how to achieve this in Nova. Im not sure if I should create a json column ‘top_categories’ on the posts table or a 1 to many relationships to store the top categories that are returned from the API, where can exist a new table like “top_amenities” with columns: id, post_id, category_name, icon_name.
Do you have any idea how to achieve it?
Please or to participate in this conversation.