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

dadub's avatar
Level 1

Insert into 3 tables (pivot) from a form (Thank you)

Hello everyone and thank you in advance for your help,

I continue my learning of Laravel.

I have an "event" table, a "years" table and an event_years (pivot) table because an event can end up in several years and a year can have several events.

I would like to insert a new event via a form, for that I get what was encoded by the user.

Before, I had only one table, because an event was only in one year, so I had this code:

            'name' => 'required|string|max:255',
            'mnemonique' => 'required|string|max:255',
            'color' => 'required|string|max:255',
            'year_id' => 'required|integer'
        ]);

        $evenement = new Evenement();
        
        $evenement->create($validatedData);

I see that in the official Laravel documentation, we must go and retrieve the id, but here, I did not see any that it is a new event that did not exist:

Sample code:

$ user = App \ User :: find (1);

$ user-> roles () -> attach ($ roleId); 

How can I please do to insert the data into the 3 tables?

Here are my models:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Evenement extends Model
{
    protected $fillable = ['name', 'year_id','mnemonique','color'];
      
    public function years()
    {
        return $this->belongsToMany(Year::class, 'evenement_year','evenement_id','year_id'); 
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Year extends Model
{
    protected $fillable = ['name'];

    public function evenements()
    {
        return $this->belongsToMany(Evenement::class, 'evenement_year','year_id','evenement_id');
        
    }
}

Thank you for your help.

0 likes
9 replies
SilenceBringer's avatar
Level 55

@dadub remove year_id from fillable fields (also you can simplify years relationship declaration as far as you follow conventions)

class Evenement extends Model
{
    protected $fillable = ['name', 'mnemonique','color'];
      
    public function years()
    {
        return $this->belongsToMany(Year::class); 
    }
}

and then in controller

$evenement = Evenement::create($validatedData);

$evenement->years()->sync([$validatedData['year_id']]);

Also (as far as event can have maany years, you need to change your form to accept many year_id, change in validation

'year_id' => 'required|array',
'year_id.*' => 'required|integer|exists:years,id',

and in controller

$evenement = Evenement::create($validatedData);

$evenement->years()->sync($validatedData['year_id']);
1 like
dadub's avatar
Level 1

Hi SilenceBringer,

So THANK YOU for your quick help.

It works great.

More, you give me code to use it with several years, that's too kind.

Thank you again and have a great day.

Edit : I don't understand this :


'year_id.*' => 'required|integer|exists:years,id',

Mainly exists:years,id'

What it means please ?

Thank you again

dadub's avatar
Level 1

I edited my reply because I don't understand something. Thank you in advance.

dadub's avatar
Level 1

Waow, you're a genius.

Thank you so much to you.

Hope I will be able to help you too...

Thanks for all.

SilenceBringer's avatar

@dadub this question is out of original scope, and thread is closed. So, next time create new thread.

I think in you html form field name is year_id. To have multiple values it should be year_id[]

dadub's avatar
Level 1

Very sorry.

name="year_id[]" is not working, but I will see on Google.

Thank you again for your help.

Please or to participate in this conversation.