Skip to content

Commit

Permalink
Changed api/user endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TiiFuchs committed Nov 2, 2024
1 parent 82c90eb commit 053fcd6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
24 changes: 24 additions & 0 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/** @mixin User */
class UserResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->email,
'email_verified_at' => $this->email_verified_at,
'image_url' => $this->image_url,
'roles' => $this->whenLoaded('roles'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->text('full_name')->virtualAs('CONCAT(first_name, " ", last_name)')
$table->text('full_name')->storedAs('CONCAT(first_name, " ", last_name)')
->after('last_name');
});
}
Expand Down
5 changes: 4 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

use App\Http\Resources\UserResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/user', function (Request $request) {
return $request->user();
return new UserResource(
$request->user()
);
})->middleware('auth:api');

0 comments on commit 053fcd6

Please sign in to comment.