The fastest way to make a powerful JSON:API compatible Rest API with Laravel.
MIT License
Bot releases are hidden (Show)
Published by binaryk about 4 years ago
Sometimes you don't need to have an action with models. Let's say for example the authenticated user wants to disable his account. For this we have standalone
actions:
// UserRepository
public function actions(RestifyRequest $request)
{
return [
DisableProfileAction::new()->standalone(),
];
}
Usually the URL for the action is make based on the action name. You can use your own URI key if you want:
class DisableProfileAction extends Action
{
public static $uriKey = 'disable_profile';
//...
}
Published by binaryk about 4 years ago
mainQuery
on repositories, now you can use this for show and index endpointsBinaryk\LaravelRestify\Models\CreationAware
interface. Now you can extend it, and you will take the control over the model savingPublished by binaryk about 4 years ago
Published by binaryk about 4 years ago
Published by binaryk over 4 years ago
Fixing filter.
Published by binaryk over 4 years ago
You can use the UserRepository
(if you have one), to resolve the profile and perform the updates over the profile by using the UserProfile
trait in your repository:
class UserRepository extends Repository
{
use Binaryk\LaravelRestify\Repositories\UserProfile;
}
Checkout the documentation for more details.
Published by binaryk over 4 years ago
class PostRepository extends Repository
{
public static $match = [
'published_at' => RestifySearchable::MATCH_DATETIME,
];
}
Request:
GET: /restify-api/posts?published_at=2020-12-01
class PostRepository extends Repository
{
public static $match = [
'published_at' => RestifySearchable::MATCH_ARRAY,
];
}
Request:
GET: /restify-api/posts?id=1,2,3
This will be converted to:
->whereIn('id', [1, 2, 3])
GET: /restify-api/posts?-id=1,2,3
This will return all posts where doesn't have the id
in the [1,2,3]
list.
You can apply -
(negation) for every match:
GET: /restify-api/posts?-title="Some title"
Published by binaryk over 4 years ago
This is very wanted feature, for a long time.
Before:
After:
Published by binaryk over 4 years ago
POST: restify-api/posts/bulk
Payload:
[ { "title": "Post title 1"}, { "title": "Post title 2"} ]
This is as well post. Just because using "POST" you can pass form data:
POST: restify-api/posts/bulk/update
Payload:
[ { "id":1, "title": "Post title 1"}, { "id": 2, "title": "Post title 2"} ]
Published by binaryk over 4 years ago
However, Laravel Restify provides eager loading based on the query related
property,
you may want to force eager load a relationship in terms of using it in fields, or whatever else:
// UserRepository.php
public static $with = ['posts'];
Published by binaryk over 4 years ago
index
method, so you can return the same format for a custom paginator.$paginator = User::query()->paginate(5);
$response = Binaryk\LaravelRestify\Controllers\RestResponse::index(
$paginator
);
resolveIndexMainMeta
method, now it accept the paginatorMeta
information:public function resolveIndexMainMeta(RestifyRequest $request, Collection $items, array $paginationMeta): array
Published by binaryk over 4 years ago
You can also override the main meta
object for the index, not the one for per item:
public function resolveIndexMainMeta(RestifyRequest $request, Collection $items)
{
$default = parent::resolveIndexMeta($request);
return array_merge($default, [
'next_payment_at' => $this->resource->current_payment_at->addMonth(),
]);
}
Published by binaryk over 4 years ago
Ability to setup your own attachers. For example if you want to attach users to a role, and you don't want to use the default attach method. In this case you can override the attach method like this:
// RoleRepository.php
public function attachUsers(RestifyRequest $request, Repository $repository, Model $model)
{
ModelHasRole::create([
'role_id' => $model->id,
'model_type' => User::class,
'model_id' => $request->get('users'),
]);
return $this->response()->created();
}
The URL used for attach will remain the same as for a normal attach:
'restify-api/roles/' . $role->id . '/attach/users'
Published by binaryk over 4 years ago
Published by binaryk over 4 years ago
public static $middlewares = [
PostAbortMiddleware::class,
];
or if you need RestifyRequest you can override the collecting method:
public static function collectMiddlewares(RestifyRequest $request): ?Collection
{
if ($request->isStoreRequest()) {
return collect([PostAbortMiddleware::class]);
}
}
attributes
property.Published by binaryk over 4 years ago
Published by binaryk over 4 years ago
restify/profile
restify/profile
restify/profile/avatar
You can customize the user avatar
column by using:
Binaryk\LaravelRestify\Http\Requests\ProfileAvatarRequest::$userAvatarAttribute = 'avatar';
You can also customize the path to the file using:
Binaryk\LaravelRestify\Http\Requests\ProfileAvatarRequest::$path = 'avatars';
or if you need current request:
Binaryk\LaravelRestify\Http\Requests\ProfileAvatarRequest::$usingPath = function (RestifyRequest $request) {
return "avatars/{$user->id}";
}
Published by binaryk over 4 years ago
Field::new('created_at')->label('sent_at')
Field::new('token')->hidden(); // this will not be visible
Field::new('user_id')->hidden()->append(auth()->user()->id); // this will not be visible, but will be stored
viaRelationship
query param, as it will get the default one from the main repository: axios.get('/restify/users?viaRelationship=users&viaRepositoryId=1&viaRepository=companies')
After:
axios.get('/restify/users?viaRepositoryId=1&viaRepository=companies')
Published by binaryk over 4 years ago
$users = $this->mockUsers(3);
$company = factory(Company::class)->create();
$company->users()->attach($users->pluck('id'));
$usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(3, $usersFromCompany->json('data'));
$this->postJson('restify-api/companies/' . $company->id . '/detach/users', [
'users' => [1, 2]
])
->assertStatus(204);
$usersFromCompany = $this->getJson('/restify-api/users?viaRepository=companies&viaRepositoryId=1&viaRelationship=users');
$this->assertCount(1, $usersFromCompany->json('data'));
Published by binaryk over 4 years ago
$response = $this->postJson('restify-api/companies/' . $company->id . '/attach/users', [
'users' => [1, 2],
'is_admin' => true,
])
->assertStatus(201);
$response->assertJsonFragment([
'company_id' => '1',
'user_id' => $user->id,
'is_admin' => true,
]);