Prima di tutto, voglio dirvi che ho cercato su Internet questo problema e non sono stato in grado di risolvere questo problema.CONSTRAIN Unico su Laravel 5 Schema Builder
Il mio problema è con le chiavi primarie dei miei tavoli.
Ho 2 tabelle, eventi e event_guest
+-----------+
| event |
+-----------+
| eventid |
+-----------+
+-------------+
| event_guest |
+-------------+
| guestid |
| eventid |
| firstname |
| lastname |
| email |
+-------------+
Ogni evento ha molti event_guest
, ma l'e-mail di ogni ospite deve essere unico per ogni evento.
Il mio controller voglio ottenere o creare utilizzando il metodo firstOrCreate su EventGuest Modello:
EventGuests::firstOrCreate(['eventid' => $eventid,'firstname' => 'Ivan', 'lastname'=>'Bravo', 'email'=>'[email protected]');
Se l'email non è impostata sul caso si deve registrare la voce.
+-----------+----------+-----------+-----------+----------------+
| guestid | eventid | firstname | lastname | email |
+-----------+----------+-----------+-----------+----------------+
| 1 | 1 | Ivan | Bravo | [email protected] |
+-----------+----------+-----------+-----------+----------------+
quello che mi serve è eventid
come chiave primaria e guestid
come auto_increment
campo in modo che possa ripristinare a 1 per ogni evento. Ad esempio:
+-----------+----------+-----------+-----------+----------------+
| guestid | eventid | firstname | lastname | email |
+-----------+----------+-----------+-----------+----------------+
| 1 | 1 | Ivan | Bravo | [email protected] |
| 2 | 1 | John | Doe | [email protected] |
| 1 | 2 | Ivan | Bravo | [email protected] |
+-----------+----------+-----------+-----------+----------------+
E inoltre ho bisogno di e-mail come un campo unico per evitare le righe duplicate.
Attualmente sto usando laravel 5 Migrazioni, ma quando provo a ripristinare campi primari viene richiesto questo errore:
Multiple primary key defined (SQL: alter table `event_guest` add primary key event_guest_eventid_guestid_primary(`eventid`, `guestid`))
Questo è il mio codice di migrazione:
Schema::create('event_guest', function(Blueprint $table) {
$table->integer('guestid', true);
$table->integer('eventid');
$table->integer('contactid')->nullable();
$table->string('firstname', 256);
$table->string('lastname', 256);
$table->string('email', 256);
$table->unique(array('email','name'));
$table->primary(array('eventid','guestid'));
});
ho davvero bisogno di aiuto per capirlo correttamente
non ho avuto difficoltà in passato utilizzando:
create table `event_guest` (
`guestid` int(11) NOT NULL AUTO_INCREMENT,
`eventid` int(11) NOT NULL DEFAULT '0',
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`eventid`,`guestid`),
CONSTRAINT guest UNIQUE (eventid,email)
) ENGINE=MyISAM
Ora
Sarebbe possibile suddividere l'ID ospite, il nome, il cognome e l'e-mail nella propria tabella denominata "ospite". Quindi cambia lo schema di 'event_guest' in solo event_id e guest_id? –