Skip to content

Commit f5414ae

Browse files
committed
Add examples and add api/users route
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent d751c99 commit f5414ae

20 files changed

+424
-10
lines changed

app/Enums/TeamRole.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
enum TeamRole: string
6+
{
7+
case OWNER = 'owner';
8+
case ADMIN = 'admin';
9+
case MEMBER = 'member';
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
6+
7+
class PostResource extends JsonApiResource
8+
{
9+
/**
10+
* The resource's attributes.
11+
*
12+
* @var list<string>
13+
*/
14+
protected array $attributes = [
15+
'title',
16+
'content',
17+
];
18+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
7+
8+
class UserResource extends JsonApiResource
9+
{
10+
public function toRelationships(Request $request)
11+
{
12+
return [
13+
'posts',
14+
'profile',
15+
'teams',
16+
];
17+
}
18+
19+
/**
20+
* Transform the resource into an array.
21+
*
22+
* @return array<string, mixed>
23+
*/
24+
public function toAttributes(Request $request): array
25+
{
26+
return [
27+
'name' => $this->name,
28+
'email' => $this->email,
29+
'verified_at' => fn () => $this->email_verified_at,
30+
'avatar' => function () {
31+
if ($this->avatar_path) {
32+
return url("storage/{$this->avatar_path}");
33+
}
34+
35+
return null;
36+
},
37+
];
38+
}
39+
}

app/Models/Membership.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Enums\TeamRole;
6+
use Illuminate\Database\Eloquent\Relations\Pivot;
7+
8+
class Membership extends Pivot
9+
{
10+
/**
11+
* The table associated with the model.
12+
*
13+
* @var string
14+
*/
15+
protected $table = 'team_user';
16+
17+
18+
/**
19+
* Get the attributes that should be cast.
20+
*
21+
* @return array<string, string>
22+
*/
23+
protected function casts(): array
24+
{
25+
return [
26+
'role' => TeamRole::class,
27+
];
28+
}
29+
}

app/Models/Post.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Post extends Model
9+
{
10+
/** @use HasFactory<\Database\Factories\PostFactory> */
11+
use HasFactory;
12+
}

app/Models/Team.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
9+
class Team extends Model
10+
{
11+
/** @use HasFactory<\Database\Factories\TeamFactory> */
12+
use HasFactory;
13+
14+
/**
15+
* Get the attributes that should be cast.
16+
*
17+
* @return array<string, string>
18+
*/
19+
protected function casts(): array
20+
{
21+
return [
22+
'personal_team' => 'boolean',
23+
];
24+
}
25+
26+
/**
27+
* The users that belong to the team.
28+
*/
29+
public function users(): BelongsToMany
30+
{
31+
return $this->belongsToMany(User::class)
32+
->withPivot('role')
33+
->withTimestamps()
34+
->using(Membership::class)
35+
->as('membership');
36+
}
37+
}

app/Models/User.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace App\Models;
44

5-
// use Illuminate\Contracts\Auth\MustVerifyEmail;
65
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7+
use Illuminate\Database\Eloquent\Relations\HasOne;
78
use Illuminate\Foundation\Auth\User as Authenticatable;
89
use Illuminate\Notifications\Notifiable;
910
use Laravel\Fortify\TwoFactorAuthenticatable;
@@ -51,4 +52,24 @@ protected function casts(): array
5152
'two_factor_confirmed_at' => 'datetime',
5253
];
5354
}
55+
56+
/**
57+
* The profile associated with the user.
58+
*/
59+
public function profile(): HasOne
60+
{
61+
return $this->hasOne(UserProfile::class);
62+
}
63+
64+
/**
65+
* The teams that the user belongs to.
66+
*/
67+
public function teams(): BelongsToMany
68+
{
69+
return $this->belongsToMany(Team::class)
70+
->withPivot('role')
71+
->withTimestamps()
72+
->using(Membership::class)
73+
->as('membership');
74+
}
5475
}

app/Models/UserProfile.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
9+
class UserProfile extends Model
10+
{
11+
/** @use HasFactory<\Database\Factories\UserProfileFactory> */
12+
use HasFactory;
13+
14+
/**
15+
* The table associated with the model.
16+
*
17+
* @var string
18+
*/
19+
protected $table = 'user_profiles';
20+
21+
/**
22+
* The user that owns the profile.
23+
*/
24+
public function user(): BelongsTo
25+
{
26+
return $this->belongsTo(User::class);
27+
}
28+
}

bootstrap/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
return Application::configure(basePath: dirname(__DIR__))
1111
->withRouting(
12+
api: __DIR__.'/../routes/api.php',
1213
web: __DIR__.'/../routes/web.php',
1314
commands: __DIR__.'/../routes/console.php',
1415
health: '/up',

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)