First of all, I want to tell you, I have searched the Internet this problem and I have not been able to solve this problem.
My problem is with the primary keys of my tables.
I have 2 Tables, events and event_guest
+-----------+
| event |
+-----------+
| eventid |
+-----------+
+-------------+
| event_guest |
+-------------+
| guestid |
| eventid |
| firstname |
| lastname |
| email |
+-------------+
Each event has many event_guest
, but the email of each guest must be unique on each event.
On my controller i want to get or create by using firstOrCreate method on EventGuest Model:
EventGuests::firstOrCreate(['eventid' => $eventid,'firstname' => 'Ivan', 'lastname'=>'Bravo', 'email'=>'ivnbrv@mac.com');
If the email is not set on the event it must record the entry.
+-----------+----------+-----------+-----------+----------------+
| guestid | eventid | firstname | lastname | email |
+-----------+----------+-----------+-----------+----------------+
| 1 | 1 | Ivan | Bravo | ivnbrv@mac.com |
+-----------+----------+-----------+-----------+----------------+
What I need it is eventid
as the primary key and guestid
as auto_increment
field so it can reset to 1 on each event. For example:
+-----------+----------+-----------+-----------+----------------+
| guestid | eventid | firstname | lastname | email |
+-----------+----------+-----------+-----------+----------------+
| 1 | 1 | Ivan | Bravo | ivnbrv@mac.com |
| 2 | 1 | John | Doe | test@mac.com |
| 1 | 2 | Ivan | Bravo | ivnbrv@mac.com |
+-----------+----------+-----------+-----------+----------------+
And and i also need email as an unique field to prevent duplicate rows.
Currently I'm using Laravel 5 Migrations, but when i try to reset primary fields it prompts this error:
Multiple primary key defined (SQL: alter table `event_guest` add primary key event_guest_eventid_guestid_primary(`eventid`, `guestid`))
This is my migration code:
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'));
});
I really need help to understand this properly.
I had no trouble in the past using:
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
Now