php - Can I shorten this method using Ramsey Uuid -


this current create method;

public function create(request $request) {     // 1. create uuid string     $uuid1 = uuid::uuid1();     $uuid = $uuid1->tostring();      // 2. check if exists in database     $response = response::where('uuid', $uuid)->first();      // 3. if does, run method again , new uuid     if ($response) {         return $this->create($request);     }      // 4. create array uuid key , put value in     $data['uuid'] = $uuid;      // 5. create record in database     $response = response::create($data);      // 6. record have created return in order return group     $response = response::where('uuid', $response->uuid)->first();      // 7. redirect passing uuid , group     return redirect()->route('survey', ['uuid' => $response->uuid, 'group' => $response->group]); } 

parts of seems little excessive. there way safely down? particularly, step 6. there no way combine 5 , reference record have created?

also, there way can remove steps 2 , 3 , confident uuid's never duplicates? know chances extremely small still.

it impossible same uuid, can safely remove steps 2 , 3. although, if want check uuid in database may use do-while-loop instead steps 1, 2 , 3:

do {     $uuid = (string) uuid::uuid1(); } while ($response = response::where('uuid', $uuid)->first()); 

next, not have run step 6. request::create return eloquent request object can use in step 7. $response->group should fetched lazy load.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -