Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ari3l's avatar
Level 1

Relation BelongToMany problem

We need some help solving a problem with hiding data in a many to many relationship.

I have got two tables in the database user and team. I want multiple users (instructors) to be able to edit multiple teams. I created an intermediate table between the user and team table. I added a BelongToMany relationship in the code and everything works fine.

But users will be adding each other and I don't want them to see others' personal information.

That's why I thought I could make an input type field where the user enters an email address and when saving I check if the user exists in the database. If it doesn't then I return an appropriate message, if it exists then I save the relationship in the database.

We have two questions:

  1. how to change the BelongsToMany field to an input type field? When I add a Text field it does not appear in the view.
  2. how to check if the user exists in the database when saving?
0 likes
10 replies
vincent15000's avatar
  1. how to change the BelongsToMany field to an input type field? When I add a Text field it does not appear in the view.

I don't understand, can you explain please ?

  1. how to check if the user exists in the database when saving?
if (User::where('email', $typedEmail)->exists()) {
	...
}
ari3l's avatar
Level 1

Thank you for your response.

The BelongsToMany field by default is of select type, or after adding searchable() parameter it is searchable.

I would like to replace this field with a regular email address field, I don't want others to see what email addresses are in the database.

When the user enters an email address, the system checks if such an address exists in the database, if so it writes a relation to the database. If the email does not exist in the database then a message is returned, e.g. "The user does not exist".

Code snippet:

BelongsToMany::make(__('Instructors'), 'teamInstructors', 'App\Nova\User')
    ->searchable()
    ->hideFromIndex()
    ->hideFromDetail(function (NovaRequest $request) {
        $user = $request->user();
        if ($user->isAdmin() || $user->id == $this->user_id) {
            return false;
        }
        return true;
    })
    ->display('email'),
2 likes
Lumethys's avatar

there is no need to modify anything on the backend, since what you want is a visual effect

When a User adding member to a team, just show the email or whatever you want at the front-end

1 like
ari3l's avatar
Level 1

I understand, just don't know how to change the select in the frontend.

I am new to laravel Nova, but as far as I know the frontend is generated automatically.

1 like
ari3l's avatar
Level 1

How do I change the select field to input?

1 like
Lara_Love's avatar

@ari3l which select?? your means:

<select name="a">

</select>

to

<input name="a" type="text">

show your code

1 like
vincent15000's avatar

@ari3l I think that you can't transform the BelongsToMany field to an input field.

You have probably to manipulate the code before sending it to the view in order to display only what you need.

You could perhaps also set the select options field on readonly ?

1 like
ari3l's avatar
Level 1

@vincent15000 I think the readonly type field is not acceptable. Thank you for your help. I will look for another solution.

1 like

Please or to participate in this conversation.