Resources

Interactions

Laravel Rest API offers the flexibility to customize certain aspects of its functionality, allowing you to tailor it to your specific needs and leverage its capabilities to your advantage.

Queries

Here you can modify the queries before they are run by Laravel Rest Api.

You may only want to modify the search query since all other queries use Policies to verify if the user is allowed to access/modify the data.

Search Query

UserResource.php
/**
 * Build a query for searching resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Contracts\Database\Eloquent\Builder  $query
 * @return \Illuminate\Contracts\Database\Eloquent\Builder
 */
public function searchQuery(RestRequest $request, \Illuminate\Contracts\Database\Eloquent\Builder $query) {
    return $query;
}

Mutate Query

UserResource.php
/**
 * Build a query for mutating resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Contracts\Database\Eloquent\Builder  $query
 * @return \Illuminate\Contracts\Database\Eloquent\Builder
 */
public function mutateQuery(RestRequest $request, \Illuminate\Contracts\Database\Eloquent\Builder $query) {
    return $query;
}

Destroy Query

UserResource.php
/**
 * Build a "destroy" query for the given resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Contracts\Database\Eloquent\Builder  $query
 * @return \Illuminate\Contracts\Database\Eloquent\Builder
 */
public function destroyQuery(RestRequest $request, \Illuminate\Contracts\Database\Eloquent\Builder $query)
{
    return $query;
}

Restore Query

UserResource.php
/**
 * Build a "restore" query for the given resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Contracts\Database\Eloquent\Builder  $query
 * @return \Illuminate\Contracts\Database\Eloquent\Builder
 */
public function restoreQuery(RestRequest $request, \Illuminate\Contracts\Database\Eloquent\Builder $query)
{
    return $query;
}

Force Delete Query

UserResource.php
/**
 * Build a "forceDelete" query for the given resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Contracts\Database\Eloquent\Builder  $query
 * @return \Illuminate\Contracts\Database\Eloquent\Builder
 */
public function forceDeleteQuery(RestRequest $request, \Illuminate\Contracts\Database\Eloquent\Builder $query)
{
    return $query;
}

Operations

Laravel Rest API exposes endpoints for performing actions, giving you the freedom to customize them to meet your specific requirements and alter the default behavior of these actions:

Delete

UserResource.php
/**
 * Build a "delete" query for the given resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Database\Eloquent\Model  $query
 * @return void
 */
public function performDelete(RestRequest $request, Model $model) {
    $model->delete();
}

Restore

UserResource.php
/**
 * Build a "restore" query for the given resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Database\Eloquent\Model  $query
 * @return void
 */
public function performRestore(RestRequest $request, Model $model) {
    $model->restore();
}

Force Delete

UserResource.php
/**
 * Build a "forceDelete" query for the given resource.
 *
 * @param  \Lomkit\Rest\Http\Requests\RestRequest  $request
 * @param  \Illuminate\Database\Eloquent\Model  $query
 * @return void
 */
public function performForceDelete(RestRequest $request, Model $model) {
    $model->forceDelete();
}

Copyright © 2024