Bonjour,

Alors je voudrais savoir comment faire pour insérer des données dans deux table différentes mais lier, Table Poste(offre, annonce) et entreprise, chaque annonce est lier à une entreprise, j'ai crée deux Model et 1 seul Controller, Poste et Entreprise et PosteController.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
Schema::create('entreprises', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nomEntreprise');
            $table->string('adresseEntreprise');
            $table->timestamps();
        });
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
 
Schema::create('postes', function (Blueprint $table) {
            $table->increments('idPoste');
            $table->unsignedBigInteger('idEntreprise');
            $table->string('nomPoste');
            $table->text('descriptionPoste');
            $table->timestamps();
            $table->foreign('idEntreprise')
                ->references('id')
                ->on('entreprises')
                ->onDelete('cascade');
 
        });
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
public function create()
    {
        $postes = Poste::all();
        $entreprises = Entreprise::all();
        return view('postes.create', compact('postes','entreprises'));
    }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
public function store(Request $request)
    {
        $data = $request->validate([
            'nomPoste'=>'required|min:3',
            'descriptionPoste'=>'required|min:3'
        ]);
        $data2 = $request->validate([
            'nomEntreprise'=>'required|min:3',
            'adresseEntreprise'=>'required|min:3'
        ]);
        Poste::create($data);
        Entreprise::create($data2);
 
 
 
        return back();
    }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
class Poste extends Model
{
 
 
    protected $fillable = ['nomPoste','descriptionPoste','idEntreprise'];
 
    public function entreprise()
    {
        return $this->belongsTo(Entreprise::class,'idEntreprise');
    }
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
protected $fillable = ['nomEntreprise', 'adresseEntreprise'];
 
    public function poste()
    {
        return $this->hasMany(Poste::class);
    }
quand j'insére des données par les factory ça marche super bien car j'arrive a afficher un Poste avec son entreprise. mais dés l'insertion par formulaire j'ai une erreur comme : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (projetetudiant.postes, CONSTRAINT postes_identreprise_foreign FOREIGN KEY (idEntreprise) REFERENCES entreprises (id) ON DELETE CASCADE).

Voilà j'ai commencer laravel il y'a 2 jours, je sais pas comment résoudre ce problème et je suis rester bloquer dessus trop longtemps donc , aidez-moi svp !