I am implementing a small component for the HR system for the office in which I work. I am currently working on allowing users to request a vacation. I try to use Command Handlers (and hopefully also events) as my controllers are always very bloated.
I want to do the following in my application:
First, the user accesses the form on the form to “create a new request” for the holiday, for example. This should go to the store () method in the RequestController and disable the self-help command, namely CreateRequestCommand. Then this command will save the request made by the user in the database, and then continue the work with which I have not had time.
There are two problems that I encounter when implementing the above:
Firstly, my form has a file upload. As you know, if I have a dd () file in my controller, I will see the properties of the files that I can use to download it. However, if I dd () in the handle () function of CreateRequestCommand, it shows null. So, I am confused as to where should I execute the loading logic?
Secondly, my form has 9 fields, which pretty much correspond to what I store in the database, with the exception of user_id. So what I usually do, and I know this is considered practice, so I try to use Commands, grab all the input from the form, and then drop it all into the create () function of the model to store it in the database. Now I extracted this logic into the Command and looked at the Taylor Autwell example here on the laracasters and in the documents for the PurchasePodcastCommand (or whatever). I understand that I need all the relevant data to place in the Command constructor, which means I have 9 variables in the constructor. Now forgive my ignorance, but with 9 variables in the constructor it just looks wrong,so I ask myself and now here - am I doing it right? The following is some relevant code from the classes that I mentioned above.
\Http\\RequestController.php
public function store(Requests\CreateRequestRequest $request)
{
$this->dispatchFrom(CreateRequestCommand::class, $request);
}
\\CreateRequestCommand.php
class CreateRequestCommand extends Command implements SelfHandling {
protected $requestType, $attachment, $manager, $noOfDays, $dateFrom, $dateTo, $location, $contactNumber, $cover;
public function __construct($requestType, $attachment, $manager, $noOfDays, $dateFrom, $dateTo, $location, $contactNumber, $cover)
{
$this->requestType = $requestType;
$this->attachment = $attachment;
$this->manager = $manager;
$this->noOfDays = $noOfDays;
$this->dateFrom = $dateFrom;
$this->dateTo = $dateTo;
$this->location = $location;
$this->contactNumber = $contactNumber;
$this->cover = $cover;
}
public function handle()
{
dd([
'requestType' => $this->requestType,
'attachment' => $this->attachment,
'manager' => $this->manager,
'noOfDays' => $this->noOfDays,
'dateFrom' => $this->dateFrom,
'dateTo' => $this->dateTo,
'location' => $this->location,
'contactNumber' => $this->contactNumber,
'cover' => $this->cover,
]);
}
}
, . - , .