Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Controllers/RestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;

/**
* Class RestResponse.
Expand Down Expand Up @@ -580,7 +581,7 @@ public function getErrors()
return $this->errors instanceof Arrayable ? $this->errors->toArray() : $this->errors;
}

public function debug(\Exception $exception, $condition)
public function dump(\Exception $exception, $condition)
{
if ($condition) {
$this->debug = true;
Expand All @@ -595,6 +596,17 @@ public function debug(\Exception $exception, $condition)
return $this;
}

/**
* Debug the log if the environment is local.
*
* @param \Exception $exception
* @return $this
*/
public function dumpLocal(\Exception $exception)
{
return $this->dump($exception, App::environment('production') === false);
}

/**
* Set the JSON:API format for a single resource.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/RestifyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function render($request, Exception $exception)
if (App::environment('production') === true) {
$response->addError(__('messages.something_went_wrong'));
} else {
$response->debug($exception, true);
$response->dump($exception, true);
}
$response->error();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public function fillAttribute(RestifyRequest $request, $model)
*/
protected function fillAttributeFromRequest(RestifyRequest $request, $model, $attribute)
{
if ($request->exists($attribute)) {
$value = $request[$attribute];
if ($request->exists($attribute) || $request->get($attribute)) {
$value = $request[$attribute] ?? $request->get($attribute);

$model->{$attribute} = is_callable($this->storeCallback) ? call_user_func($this->storeCallback, $value, $request, $model) : $value;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Controllers/RepositoryIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public function handle(RestifyRequest $request)
} catch (EntityNotFoundException $e) {
return $this->response()->notFound()
->addError($e->getMessage())
->debug($e, $request->isDev());
->dump($e, $request->isDev());
} catch (UnauthorizedException $e) {
return $this->response()->forbidden()->addError($e->getMessage())->debug($e, $request->isDev());
return $this->response()->forbidden()->addError($e->getMessage())->dump($e, $request->isDev());
} catch (InstanceOfException | \Throwable $e) {
return $this->response()->error()->debug($e, $request->isDev());
return $this->response()->error()->dump($e, $request->isDev());
}
}
}
31 changes: 18 additions & 13 deletions src/Http/Requests/InteractWithRepositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ public function authorize()
/**
* Get the class name of the repository being requested.
*
* @param null $key
* @return Repository
* @throws EntityNotFoundException
* @throws UnauthorizedException
*/
public function repository()
public function repository($key = null)
{
return tap(Restify::repositoryForKey($this->route('repository')), function ($repository) {
return tap(Restify::repositoryForKey($key ?? $this->route('repository')), function ($repository) {
if (is_null($repository)) {
throw new EntityNotFoundException(__('Repository :name not found.', [
'name' => $repository,
Expand Down Expand Up @@ -109,50 +108,56 @@ public function isResolvedByRestify()
* As a model it could accept either a model instance, a collection or even paginated collection.
*
* @param $model
* @param null $uriKey
* @return Repository
*/
public function newRepositoryWith($model)
public function newRepositoryWith($model, $uriKey = null)
{
$repository = $this->repository();
$repository = $this->repository($uriKey);

return $repository::resolveWith($model);
}

/**
* Get a new, scopeless query builder for the underlying model.
*
* @param null $uriKey
* @return \Illuminate\Database\Eloquent\Builder
* @throws EntityNotFoundException
* @throws UnauthorizedException
*/
public function newQueryWithoutScopes()
public function newQueryWithoutScopes($uriKey = null)
{
return $this->model()->newQueryWithoutScopes();
return $this->model($uriKey)->newQueryWithoutScopes();
}

/**
* Get a new instance of the underlying model.
*
* @param null $uriKey
* @return \Illuminate\Database\Eloquent\Model
* @throws EntityNotFoundException
* @throws UnauthorizedException
*/
public function model()
public function model($uriKey = null)
{
$repository = $this->repository();
$repository = $this->repository($uriKey);

return $repository::newModel();
}

/**
* Get the query to find the model instance for the request.
*
* @param mixed|null $repositoryId
* @param mixed|null $repositoryId
* @param null $uriKey
* @return \Illuminate\Database\Eloquent\Builder
* @throws EntityNotFoundException
* @throws UnauthorizedException
*/
public function findModelQuery($repositoryId = null)
public function findModelQuery($repositoryId = null, $uriKey = null)
{
return $this->newQueryWithoutScopes()->whereKey(
return $this->newQueryWithoutScopes($uriKey)->whereKey(
$repositoryId ?? request('repositoryId')
);
}
Expand Down
Loading