Hi,
I'm trying to work out why Laravel is throwing Integrity constraint violations on my main registration form, but when I check my database it seems the user has got through to the next stage (not shown) and completed more data there
It may be some kind of ajax double submit issue but I'm just wondering if anybody can see any issues with the approach here to begin with
thanks J
Exception:
File: /var/www/vhosts/project/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php
Line: 647
Message:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'me@me.com' for key 'entrants_email_unique' (SQL: insert into `entrants` (`email`, `firstname`, `lastname`,`updated_at`, `created_at`) values (me@me.com, Joe, P, 2017-04-15 14:54:41, 2017-04-15 14:54:41))
// https://github.com/whipsterCZ/laravel-ajax
use App\Services\Ajax\Ajax;
class RegisterController extends Controller {
public function register(RegisterFormRequest $request, Ajax $ajax) {
Session::reflash();
try {
$entry = EntryService::register((object)$request->all());
} catch(\Exception $e) {
throw($e);
}
$params = [
'success' => true,
'uid' => $entry->uid
];
$headers = $this->noCacheHeaders;
if(Request::ajax()) {
$ajax->json = $params;
return $ajax->jsonResponse();
}
}
}
class EntryService {
public function register($data) {
// -----------------------------------------------------------------
DB::beginTransaction();
// -----------------------------------------------------------------
try {
$entry = EntryRepository::create($data);
if($entry) {
$entrant = $entry->entrant;
$subscribed = fMailgunService::subscribe($entry->entrant, [
'uid' => $entry->uid,
]);
}
} catch (\Exception $e) {
DB::rollBack();
throw($e);
}
// -----------------------------------------------------------------
DB::commit();
// -----------------------------------------------------------------
return $entry;
}
}
}
class EntryRepository {
public function create($data) {
$entrant = new Entrant();
$entrant->email = $data->email;
// ---------------------------------------------------------
// entrant personal details
// ----------------------------------------------------------
$entrant->firstname = $data->firstname;
$entrant->lastname = $data->lastname;
// -----------------------------------------------------------------------
// save our entrant to the database,
// so we can save other records against it
// -----------------------------------------------------------------------
$entrant->save();
// -----------------------------------------------------------------------
// create a new blank entry and save it to the entrant
// -----------------------------------------------------------------------
$entry = new Entry();
$entrant->entry()->save($entry); // assigns entrant_id to entries due to FK
// -----------------------------------------------------------------------
// add tracking details
// -----------------------------------------------------------------------
$entry->ip_address = Request::ip();
$entry->browser = Request::header('User-Agent');
// -----------------------------------------------------------------------
// save our entry
// -----------------------------------------------------------------------
$entry->save();
return $entry;
}
}
just thought I'd post my assessment for anybody who finds themselves in a similar situation
turns out I was correct in thinking this was a double submit issue,
I tested this by calling submit several times from the command line
$('#form').submit(); $('#form').submit(); $('#form').submit();....
so it wasn't affecting my users, but it was causing unnecessary queries on the back end.
solved now by preventing form submit twice, unless there's an error callback first, at which point they can submit again.. which obviously I should have done in the first place ;)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community