mercredi 27 janvier 2016

Can I shorten this method using Ramsey Uuid

This is my current create method;

public function create(Request $request)
{
    // 1. create a uuid string
    $uuid1 = Uuid::uuid1();
    $uuid = $uuid1->toString();

    // 2. check if it already exists in the database
    $response = Response::where('uuid', $uuid)->first();

    // 3. if it does, run this method again and get a new uuid
    if ($response) {
        return $this->create($request);
    }

    // 4. create an array with a uuid key and put the value in it
    $data['uuid'] = $uuid;

    // 5. create the record in the database
    $response = Response::create($data);

    // 6. look up the record I have just created to return in order to return the group
    $response = Response::where('uuid', $response->uuid)->first();

    // 7. redirect passing the uuid and the group
    return redirect()->route('survey', ['uuid' => $response->uuid, 'group' => $response->group]);
}

Parts of this seems a little excessive. Is there any way to safely get this down? Particularly, step 6. Is there no way to combine it with 5 and immediately reference the record I have created?

Also, is there a way I can remove steps 2 and 3 and be confident the Uuid's will never be duplicates? I know the chances are extremely small but still.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire