Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (5)
# [4.3.0](https://git.customd.com/composer/laravel-api-controller/compare/v4.2.2...v4.3.0) (2022-05-24)
### Bug Fixes
* update namespace for batch actions ([c41ac90](https://git.customd.com/composer/laravel-api-controller/commit/c41ac90f9f13e2b8ee4632c440838a26c45a25e6))
### Features
* allow usage of Laravel Route Binding ([2cde22e](https://git.customd.com/composer/laravel-api-controller/commit/2cde22ee79ea32e454e6473ac9ad22b03f9f0733))
## [4.2.3](https://git.customd.com/composer/laravel-api-controller/compare/v4.2.2...v4.2.3) (2022-05-23)
### Bug Fixes
* update namespace for batch actions ([c41ac90](https://git.customd.com/composer/laravel-api-controller/commit/c41ac90f9f13e2b8ee4632c440838a26c45a25e6))
## [4.2.2](https://git.customd.com/composer/laravel-api-controller/compare/v4.2.1...v4.2.2) (2022-05-16)
......
<?php
namespace App\Models\Contracts;
namespace Phpsa\LaravelApiController\Http\Api\Contracts;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
......
......@@ -4,6 +4,7 @@ namespace Phpsa\LaravelApiController\Http\Api\Contracts;
use Illuminate\Database\Eloquent\Builder;
use Phpsa\LaravelApiController\Helpers;
use Illuminate\Database\Eloquent\Model;
trait HasQueryBuilder
{
......@@ -22,6 +23,15 @@ trait HasQueryBuilder
return resolve($this->model())->newQuery();
}
protected function resolveRouteBinding(mixed $id): Builder
{
$routeKeyName = $this->getModel()->getRouteKeyName();
return $id instanceof Model
? $this->getBuilder()->where($routeKeyName, $id->getAttribute($routeKeyName))
: $this->getBuilder()->where($routeKeyName, $id);
}
protected function setWhereHasClause(array $where): void
{
[$with, $key] = explode('.', $where['key']);
......
......@@ -174,7 +174,7 @@ abstract class Controller extends BaseController
$this->qualifyItemQuery();
try {
$item = $this->getBuilder()->whereKey($id)->firstOrFail($fields);
$item = $this->resolveRouteBinding($id)->firstOrFail($fields);
$this->authoriseUserAction('view', $item);
} catch (ModelNotFoundException $exception) {
......@@ -199,7 +199,7 @@ abstract class Controller extends BaseController
$this->handleCommonActions($this->request);
try {
$item = $this->getBuilder()->whereKey($id)->firstOrFail();
$item = $this->resolveRouteBinding($id)->firstOrFail();
$this->authoriseUserAction('update', $item);
} catch (ModelNotFoundException $exception) {
return $this->errorNotFound('Record does not exist');
......@@ -249,7 +249,7 @@ abstract class Controller extends BaseController
$this->qualifyItemQuery();
try {
$item = $this->getBuilder()->whereKey($id)->firstOrFail();
$item = $this->resolveRouteBinding($id)->firstOrFail();
$this->authoriseUserAction('delete', $item);
$item->delete();
event(new Deleted($item, $this->request));
......