You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Laravel Restify provides a convenient profile endpoint that allows authenticated users to retrieve and update their profile information using the same repository system that powers the rest of your API.
9
+
8
10
## Prerequisites
9
11
10
-
Make sure you followed the [Authentication](/auth/authentication) guide first, as one common mistake is not adding this middleware:
12
+
Make sure you followed the [Authentication](/auth/authentication) guide first, as you need the authentication middleware configured:
11
13
12
14
```php
13
15
// config/restify.php
14
16
'middleware' => [
15
-
// ...
17
+
// ...
16
18
'auth:sanctum',
17
-
// ...
19
+
// ...
18
20
]
19
21
```
20
22
21
23
## Get profile
22
24
23
-
Before retrieving the user's profile, you need to log in and obtain an authentication token. You can refer to the [login documentation](/auth/authentication#login) for details on how to authenticate a user. Make sure to include `Bearer {$token}` in the `Authorization` header for subsequent API requests, either using Postman or cURL.
24
-
25
-
When retrieving the user's profile, it is serialized by using the `UserRepository`.
25
+
The profile endpoint uses your `UserRepository` to serialize the authenticated user's data, giving you full control over what fields are exposed and how relationships are handled.
26
26
27
27
```http request
28
28
GET: /api/restify/profile
@@ -72,28 +72,27 @@ public function fields(RestifyRequest $request): array
72
72
}
73
73
```
74
74
75
-
Since the profile is managed using the UserRepository, you can now benefit from the power of related entities. For example, if you want to return user roles:
75
+
Since the profile uses the UserRepository, you can include related entities using Restify's relationship system. For example, to include user roles:
Also, make sure the`User` model has this method that returns a relationship from another table, or you can simply return an array:
89
+
Make sure your`User` model defines the proper Eloquent relationship:
86
90
87
91
```php
88
-
//User.php
89
-
90
-
public function roles(): array
92
+
// User.php
93
+
public function roles(): BelongsToMany
91
94
{
92
-
// In a real project, here you will get this information from the database.
93
-
return [
94
-
'owner',
95
-
'admin'
96
-
];
95
+
return $this->belongsToMany(Role::class);
97
96
}
98
97
```
99
98
@@ -115,8 +114,20 @@ The result will look like this:
115
114
},
116
115
"relationships": {
117
116
"roles": [
118
-
"owner",
119
-
"admin"
117
+
{
118
+
"id": "1",
119
+
"type": "roles",
120
+
"attributes": {
121
+
"name": "owner"
122
+
}
123
+
},
124
+
{
125
+
"id": "2",
126
+
"type": "roles",
127
+
"attributes": {
128
+
"name": "admin"
129
+
}
130
+
}
120
131
]
121
132
},
122
133
"meta": {
@@ -128,89 +139,34 @@ The result will look like this:
128
139
}
129
140
```
130
141
131
-
### Without repository
132
-
133
-
In some cases, you might choose not to use the repository for profile serialization. To do this, you should add the `Binaryk\LaravelRestify\Repositories\UserProfile` trait to your `UserRepository`:
134
-
135
-
```php
136
-
// UserProfile
137
-
138
-
use Binaryk\LaravelRestify\Repositories\UserProfile;
139
-
140
-
class UserRepository extends Repository
141
-
{
142
-
use UserProfile;
143
-
144
-
public static $model = 'App\\Models\\User';
145
-
146
-
//...
147
-
}
148
-
```
149
-
150
-
The profile will return the model directly:
151
-
152
-
### Relations
153
-
<alerttype="warning">
154
-
Note that when you're not using the repository, the `?include` parameter will not work.
This instructs Restify to use the repository only for users who are admins of your application.
167
+
This method determines whether the repository should be used for profile serialization. Returning `true` enables full repository functionality including field control, validation, and relationships.
212
168
213
-
## Update profile using repository
169
+
## Update profile
214
170
215
171
By default, Restify will validate and fill only the fields defined in your `UserRepository` when updating the user's profile. Let's use the following repository fields as an example:
216
172
@@ -279,36 +235,9 @@ Since the payload is valid now, Restify will update the user's profile (a name,
279
235
}
280
236
```
281
237
282
-
### Update without repository
283
-
284
-
If you [don't use the repository](#without-repository) for the user's profile, Restify will only update the `fillable` user attributes that are present in the request payload: `$request->only($user->getFillable())`.
If you need to customize the path or disk for the storage file, check the [image field](/api/fields#file-fields) documentation.
312
+
313
+
## MCP Integration
314
+
315
+
AI agents can access the user profile using the MCP server's profile tool. When you include the `HasMcpTools` trait in your `UserRepository`, it automatically exposes a `users-profile-tool` that AI agents can use to retrieve the current authenticated user's profile including relationships.
316
+
317
+
```php
318
+
use Binaryk\LaravelRestify\MCP\Concerns\HasMcpTools;
319
+
320
+
#[Model(User::class)]
321
+
class UserRepository extends Repository
322
+
{
323
+
use HasMcpTools;
324
+
325
+
public static function canUseForProfile(Request $request): bool
0 commit comments