-
composer.json
Open in GitHub// ... "require": { "php": ">=7.3.9", "aws/aws-sdk-php": "^3.112", "elasticsearch/elasticsearch": "^6.1", // ... "ramsey/uuid": "^3.6", }, // ...
-
database/migrations/2020_01_06_203615_create_payments_table.php
Open in GitHub// Notice that UUID is created not as the primary key, but in addition to auto-increment, as a secondary string field called "external_id" public function up() { Schema::create('payments', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('external_id'); $table->integer('amount'); // ... other fields }); } }
-
app/Http/Controllers/PaymentsController.php
Open in GitHub// The field "external_id" is filled with Uuid::uuid4() method // Notice that there is still a field "id" with auto-increment use Ramsey\Uuid\Uuid; class PaymentsController extends Controller { public function addPayment(PaymentRequest $request, Invoice $invoice) { $payment = Payment::create([ 'external_id' => Uuid::uuid4()->toString(), 'amount' => $request->amount * 100, 'payment_date' => Carbon::parse($request->payment_date), 'payment_source' => $request->source, 'description' => $request->description, 'invoice_id' => $invoice->id ]); } }
-
app/Http/Controllers/ProjectsController.php
Open in GitHub// Example from another Controller, where "external_id" is used in the same way // Also take a look at how the record is queried "whereExternalId", it's a short way for ->where('external_id', ...) use Ramsey\Uuid\Uuid; class ProjectsController extends Controller { public function store(StoreProjectRequest $request) { if ($request->client_external_id) { $client = Client::whereExternalId($request->client_external_id); } $project = Project::create( [ 'title' => $request->title, 'description' => clean($request->description), 'user_assigned_id' => $request->user_assigned_id, 'deadline' => Carbon::parse($request->deadline), 'status_id' => $request->status_id, 'user_created_id' => auth()->id(), 'external_id' => Uuid::uuid4()->toString(), 'client_id' => $client ? $client->id : null, ] ); $insertedExternalId = $project->external_id; Session()->flash('flash_message', __('Project successfully added')); event(new \App\Events\ProjectAction($project, self::CREATED)); return response()->json(['project_external_id' => $project->external_id]); } }