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

Antonella's avatar

how do i select the field with the desired FK

I have a 1: n relationship between the Language table and the Users

    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->unsignedBigInteger('lang_id_1');
        $table->unsignedBigInteger('lang_id_2')->nullable();
        $table->unsignedBigInteger('lang_id_3')->nullable();
        $table->text('img')->nullable();
        $table->rememberToken();
        $table->string('api_token', 64)->unique()->default(Str::random(50));
        $table->timestamps();

        $table->foreign('lang_id_1')
            ->references('id')
            ->on('languages');
        $table->foreign('lang_id_2')
            ->references('id')
            ->on('languages');
        $table->foreign('lang_id_3')
            ->references('id')
            ->on('languages');

    });

Schema::create('languages', function (Blueprint $table) {
        $table->id();
        $table->text('name');
        $table->timestamps();
    });

In order to put the language in the Nova Resource User I go to fill in the fields as follows:

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        Gravatar::make()->maxWidth(50),

        Text::make('Name')
            ->sortable()
            ->rules('required', 'max:255'),

        Text::make('Email')
            ->sortable()
            ->rules('required', 'email', 'max:254')
            ->creationRules('unique:users,email')
            ->updateRules('unique:users,email,{{resourceId}}'),

        Password::make('Password')
            ->onlyOnForms()
            ->creationRules('required', 'string', 'min:8')
            ->updateRules('nullable', 'string', 'min:8'),
        Text::make('Api_token')
            ->sortable()
            ->rules('required', 'api_token', 'max:254')
            ->updateRules('unique:users,email,{{resourceId}}','api_token'),

        BelongsTo::make('Language'),
        BelongsTo::make('Language'), // always refers to the 'lang_id_1' 
        BelongsTo::make('Language'), // always refers to the 'lang_id_1' 

    ];
}

unfortunately using " BelongsTo::make('Language')," always refers to the 'lang_id_1' field, I would like it to point to the 'lang_id_2' field etc.

0 likes
4 replies
philipbaginski's avatar

I'm not sure if I understand idea of this relaion, but it looks like it should be:

BelongsTo::make('Language 1', 'App\Model\Language', 'lang_id_1') BelongsTo::make('Language 2', 'App\Model\Language', 'lang_id_2') BelongsTo::make('Language 3', 'App\Model\Language', 'lang_id_3')

, and in User model:

public function lang_id_1() { return $this->belongsTo('App\Model\Language); }

public function lang_id_2() { return $this->belongsTo('App\Model\Language); }

public function lang_id_3() { return $this->belongsTo('App\Model\Language); }

Maria30's avatar

@gianmarx I think you have the relationship wrong

Could you tell us what you want to do ?

Antonella's avatar

I wanted to associate several languages with each User, maybe a user could translate into English, French and other German and Russian and so on.

i am using the spatie transtable plugin for this.

so I wanted each user to be able to view and edit a variety of languages.

The Language table serves precisely to be able to take a series of available languages and not invent them by chance

@maracaibo

Antonella's avatar

@maracaibo

I solved it like this:

/Models/User

 public function language()
{
    return $this->belongsTo(Language::class,'lang_id_1');
}

public function language2()
{
    return $this->belongsTo(Language::class,'lang_id_2');
}

public function language3()
{
    return $this->belongsTo(Language::class,'lang_id_3');
}

Nova/User

BelongsTo::make('Language', 'Language'),
        BelongsTo::make('Language 2','language2','App\Nova\Language'),
        BelongsTo::make('Language 3','language3','App\Nova\Language')

what I was wondering if there was a better way to associate a set of languages to a User?

perhaps using https://novapackages.com/packages/ziffmedia/nova-select-plus

and using a single field to register a series of languages

Please or to participate in this conversation.