Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make relation documentation clearer #2650

Open
TobiasPalludan opened this issue May 20, 2023 · 1 comment
Open

Make relation documentation clearer #2650

TobiasPalludan opened this issue May 20, 2023 · 1 comment

Comments

@TobiasPalludan
Copy link

I've struggled a little getting started with this framework, especially when it comes to documentation of the relation fields.

In [https://orchid.software/en/docs/field/#relation](the docs), relations are mentioned as standalone, like this.

Relation::make('idea')
    ->fromModel(Idea::class, 'name')
    ->title('Choose your idea');

For me, this will leave an empty relation field that fails silently.
I've since learned that in my projects, I have to do something like this:

Relation::make('user.idea')
    ->fromModel(Idea::class, 'name')
    ->title('Choose your idea');

Since the relation appears on a page belonging to another model.
I don't mind adding a PR with a suggestion for how to reflect this in the docs but wanted to gather feedback beforehand, as I don't have a lot of experience contributing to public repos.

@tabuna
Copy link
Member

tabuna commented May 21, 2023

Hi @TobiasPalludan, I think it depends on the context. To make it clearer, it can be compared as follows.

When writing:

Relation::make('idea')
    ->fromModel(Idea::class, 'name')
    ->title('Choose your idea');

The resulting HTML will be:

<label>Choose your idea</label>

<select name="idea">
    <option value="...">...</option>
</select>

When changing to Relation::make('user.idea'), the resulting HTML will change to <select name="user[idea]">.

They both work fine. However, I usually work with some entity (for example, an Eloquent model) and I want to put as little pressure on it as possible, so I use a prefix that clarifies what it is, for example, "user." and "products.".

It's not necessary to use a prefix. Instead of returning a model with a key in the query method, you can return the model attributes, for example:

public function query(): iterable
{
    return [
        'user' => User::first()
    ];
}

// and usage Input::make('user.name')

or

public function query(): iterable
{
    return User::first()->toArray();
}

// and usage Input::make('name')

I prefer the first method because when you need to modify the screen by adding new entities to it, it will be easy and won't lead to changes in layers. Let me know if you have any questions.

@tabuna tabuna transferred this issue from orchidsoftware/orchid.software Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants