Using repository in a FormRequest in Laravel for custom business rule validation
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
This is my app/Http/Requests/Organisation/CreateOrganisationRequest.php
<?php
namespace AppHttpRequestsOrganisation;
use AppContractsOrganisationInterface;
use IlluminateFoundationHttpFormRequest;
use AppHttpControllersApiBaseApiController;
use SymfonyComponentHttpKernelExceptionHttpException;
class CreateOrganisationRequest extends FormRequest
private $organisationRepository;
public function __construct(
array $query = array(),
array $request = array(),
array $attributes = array(),
array $cookies = array(),
array $files = array(),
array $server = array(),
$content = null,
OrganisationInterface $organisationRepository
)
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
$this->organisationRepository = $organisationRepository;
public function authorize()
return true;
public function rules()
unique:organisations,name'
];
public function withValidator($validator)
$validator->after(function()
$this->checkIfMaxOrganisationLimitReached();
);
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = $this->organisationRepository->myOrganisationCount();
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
and it's used in the controller like this:
public function store(CreateOrganisationRequest $request)
$validatedData = array_merge(
$request->validated(),
['owner_id' => auth()->user()->id]
);
$organisation = $this->organisationRepository->create($validatedData);
return $this->success(
fractal($organisation, new OrganisationTransformer()),
$this::Created
);
This works and get's the job done, but I am unsure if this is a good approach in Laravel.
P.S. The HttpException
that I throw is handled by appExceptionsHandler.php
.
Another approach I've considered is to add a custom attribute to User
model like this:
public function getOrganisationCountAttribute()
if ($this->account_owner)
return $this->organisations()->count();
return 0;
and update my CreateOrganisationRequest
(i.e. the FormRequest
) by removing the repository and do this instead in my business rule validation:
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = auth()->user()->organisation_count;
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
php validation laravel
add a comment |Â
up vote
1
down vote
favorite
This is my app/Http/Requests/Organisation/CreateOrganisationRequest.php
<?php
namespace AppHttpRequestsOrganisation;
use AppContractsOrganisationInterface;
use IlluminateFoundationHttpFormRequest;
use AppHttpControllersApiBaseApiController;
use SymfonyComponentHttpKernelExceptionHttpException;
class CreateOrganisationRequest extends FormRequest
private $organisationRepository;
public function __construct(
array $query = array(),
array $request = array(),
array $attributes = array(),
array $cookies = array(),
array $files = array(),
array $server = array(),
$content = null,
OrganisationInterface $organisationRepository
)
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
$this->organisationRepository = $organisationRepository;
public function authorize()
return true;
public function rules()
unique:organisations,name'
];
public function withValidator($validator)
$validator->after(function()
$this->checkIfMaxOrganisationLimitReached();
);
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = $this->organisationRepository->myOrganisationCount();
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
and it's used in the controller like this:
public function store(CreateOrganisationRequest $request)
$validatedData = array_merge(
$request->validated(),
['owner_id' => auth()->user()->id]
);
$organisation = $this->organisationRepository->create($validatedData);
return $this->success(
fractal($organisation, new OrganisationTransformer()),
$this::Created
);
This works and get's the job done, but I am unsure if this is a good approach in Laravel.
P.S. The HttpException
that I throw is handled by appExceptionsHandler.php
.
Another approach I've considered is to add a custom attribute to User
model like this:
public function getOrganisationCountAttribute()
if ($this->account_owner)
return $this->organisations()->count();
return 0;
and update my CreateOrganisationRequest
(i.e. the FormRequest
) by removing the repository and do this instead in my business rule validation:
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = auth()->user()->organisation_count;
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
php validation laravel
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This is my app/Http/Requests/Organisation/CreateOrganisationRequest.php
<?php
namespace AppHttpRequestsOrganisation;
use AppContractsOrganisationInterface;
use IlluminateFoundationHttpFormRequest;
use AppHttpControllersApiBaseApiController;
use SymfonyComponentHttpKernelExceptionHttpException;
class CreateOrganisationRequest extends FormRequest
private $organisationRepository;
public function __construct(
array $query = array(),
array $request = array(),
array $attributes = array(),
array $cookies = array(),
array $files = array(),
array $server = array(),
$content = null,
OrganisationInterface $organisationRepository
)
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
$this->organisationRepository = $organisationRepository;
public function authorize()
return true;
public function rules()
unique:organisations,name'
];
public function withValidator($validator)
$validator->after(function()
$this->checkIfMaxOrganisationLimitReached();
);
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = $this->organisationRepository->myOrganisationCount();
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
and it's used in the controller like this:
public function store(CreateOrganisationRequest $request)
$validatedData = array_merge(
$request->validated(),
['owner_id' => auth()->user()->id]
);
$organisation = $this->organisationRepository->create($validatedData);
return $this->success(
fractal($organisation, new OrganisationTransformer()),
$this::Created
);
This works and get's the job done, but I am unsure if this is a good approach in Laravel.
P.S. The HttpException
that I throw is handled by appExceptionsHandler.php
.
Another approach I've considered is to add a custom attribute to User
model like this:
public function getOrganisationCountAttribute()
if ($this->account_owner)
return $this->organisations()->count();
return 0;
and update my CreateOrganisationRequest
(i.e. the FormRequest
) by removing the repository and do this instead in my business rule validation:
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = auth()->user()->organisation_count;
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
php validation laravel
This is my app/Http/Requests/Organisation/CreateOrganisationRequest.php
<?php
namespace AppHttpRequestsOrganisation;
use AppContractsOrganisationInterface;
use IlluminateFoundationHttpFormRequest;
use AppHttpControllersApiBaseApiController;
use SymfonyComponentHttpKernelExceptionHttpException;
class CreateOrganisationRequest extends FormRequest
private $organisationRepository;
public function __construct(
array $query = array(),
array $request = array(),
array $attributes = array(),
array $cookies = array(),
array $files = array(),
array $server = array(),
$content = null,
OrganisationInterface $organisationRepository
)
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
$this->organisationRepository = $organisationRepository;
public function authorize()
return true;
public function rules()
unique:organisations,name'
];
public function withValidator($validator)
$validator->after(function()
$this->checkIfMaxOrganisationLimitReached();
);
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = $this->organisationRepository->myOrganisationCount();
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
and it's used in the controller like this:
public function store(CreateOrganisationRequest $request)
$validatedData = array_merge(
$request->validated(),
['owner_id' => auth()->user()->id]
);
$organisation = $this->organisationRepository->create($validatedData);
return $this->success(
fractal($organisation, new OrganisationTransformer()),
$this::Created
);
This works and get's the job done, but I am unsure if this is a good approach in Laravel.
P.S. The HttpException
that I throw is handled by appExceptionsHandler.php
.
Another approach I've considered is to add a custom attribute to User
model like this:
public function getOrganisationCountAttribute()
if ($this->account_owner)
return $this->organisations()->count();
return 0;
and update my CreateOrganisationRequest
(i.e. the FormRequest
) by removing the repository and do this instead in my business rule validation:
private function checkIfMaxOrganisationLimitReached()
$currentOrganisationsCount = auth()->user()->organisation_count;
$maxOrganisationsLimit = auth()->user()->max_organisations;
if (($currentOrganisationsCount + 1) > $maxOrganisationsLimit)
throw new HttpException(BaseApiController::Bad_Request, "You have reached maximum organisations that can be created, $maxOrganisationsLimit.");
php validation laravel
edited Feb 25 at 0:11
asked Feb 25 at 0:01
Latheesan
1939
1939
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188299%2fusing-repository-in-a-formrequest-in-laravel-for-custom-business-rule-validation%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password