First of all, I want to tell you, I searched the Internet for this problem, and I could not solve this problem.
My problem is with the main keys of my tables.
I have 2 tables, events and event_guest
+-----------+ | event | +-----------+ | eventid | +-----------+ +-------------+ | event_guest | +-------------+ | guestid | | eventid | | firstname | | lastname | | email | +-------------+
Each event has a lot of event_guest , but each guest’s email should be unique for each event.
On my controller, I want to get or create using the firstOrCreate method in EventGuest Model:
EventGuests::firstOrCreate(['eventid' => $eventid,'firstname' => 'Ivan', 'lastname'=>'Bravo', 'email'=>' ivnbrv@mac.com ');
If the message is not set in the event, it should record the record.
+-----------+----------+-----------+-----------+----------------+ | guestid | eventid | firstname | lastname | email | +-----------+----------+-----------+-----------+----------------+ | 1 | 1 | Ivan | Bravo | ivnbrv@mac.com | +-----------+----------+-----------+-----------+----------------+
I need eventid as primary key and guestid as auto_increment that it can reset to 1 in every event. For instance:
+-----------+----------+-----------+-----------+----------------+ | 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 I also need email as a unique field to prevent line duplication.
I am currently using Laravel 5 Migrations, but when I try to use the reset primary fields, it asks for this error:
Multiple primary key defined (SQL: alter table `event_guest` add primary key event_guest_eventid_guestid_primary(`eventid`, `guestid`))
This is my transition 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 get it right.
In the past, I had no problems:
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