At a first glance, it's simple: another CRUD-like resource for Parking, and we're done? But here's where you have a lot of room to choose from, how to structure the API endpoints and Controller methods.
- Should it be
/parkings/startand/parkings/stop? - Should it be
POST /parkingsandPUT /parkings? - Some other structure?
I posted this question on Twitter - you can read the replies or watch my re-cap on YouTube: To CRUD or Not To CRUD?.
In short, there are at least a few ways to structure this. I would go for a non-standard-CRUD approach and create a ParkingController with start/stop methods.
Hi Povilas, question regarding your code under start method of ParkingController:
Is it still necessary to use the load function to eager load vehicle and zone? i believe there wont be any N+1 problem knowing that there will only be one vehicle and one zone per parking.
Right, you're probably right, this time I was overly cautious. But better safe than sorry! :)
This is the first time seeing this syntax. I'm happy to always learn something new in Laravel. It gives the confidence that I am getting far deeper into the framework everyday. Thanks a lot.
Since there will be only one record, is that the reason that show() and stop() method does not load relationships before passing to resource class like in the start() method ? thanks
In
startmethod, when we fire this endpoint more one time it will store duplicate records at same parking and zone, so I suggest set a validation when same parking exists if stop time null.Good catch, didn't think of that validation.
What would be the best way to implement such validation?
In the repo it was inplemented, updated this lesson with the code.
$parkingDatavalidation sectionexistshas tablevehiclesand the columndeleted_at. Need to update migration/ model files to add softDelete.I think you put the wrong screenshot "We launch this endpoint with success!" (after this sentence)
Thanks, another well spotted mistake, changed to the correct screenshot. It's so good to see people actually READ the content :)
you forgot to add "id" in ParkingResource.php
Well spotted! Fixed now.
Is there difference if we register an observer in AppServiceProvider rather than EventServiceProvider (as in Laravel docs) like you did for Vehicle and Parking observers?
No difference, any Service Provider is fine.
Regarding
Would it be better it we add these constraints for these routes?? Like this
Yes I think it's a good suggestion, great comment.
hi, After creating this :
'exists:vehicles,id,deleted_at,NULL,user_id,'.auth()->id(),for validation Rule, when runing postman , I have this issue:Column not found: 1054 Unknown column 'deleted_at' in 'where clause' (SQL: select count(*) as aggregate from `vehicles`I need to create deleted_at colum in the parking migration ?
Pretty sure it was created earlier, with
$table->softDeletes();Hello! It is ok in the repository, but in the text lesson, not was created.
Oh right, I see it now. Fixed in the lesson, thanks for the notice!
Hi Povilas, I think
'stop_time' => $this->stop_time?->ToDateTimeString(), the?dont fixe my problem about parse null toDateTime.. this is what postman said:"message": "Call to a member function ToDateTimeString() on null",but it work on database!
Weird, it did for me. Well, you can rewrite it with if-statement or ternary then, try something like
$this->stop_time->toDateTimeString() ?? nullor if-statementYeah I fix it using Carbon, thank you Povilas!
It looks like you're using the local scope
Parking::active()when checking if there's an active parking in the controller but it's not been added to the model in any previous steps.Well noted, thanks! Weird, in the repository scopes are here but perhaps forgot to add them to the lesson text itself. Fixed now!
If you stop an already stopped parking, it will always update the stop_time. One possible solution would be to apply the scopeStopped (declared but not used) similarly to the scopeActive in start parking.
Well, someone noticed the same in the next video :)
Hi Povilas, The syntax
applied in
ParkingController.phpvalidation calls my attention and i need some explanation.Why can I use so many parameters separated by commas?, in the documentation is not clear to me.
This translates to:
value provided exists on
vehiclestableidcolumn ANDdeleted_atcolumn has valueNULLANDuser_idis of currently authenticated userso you can request to start parking for car you own that is not deleted otherwise request would be invalid
Are they any way more readable to write this line, using for example the
Ruleclass? I have saw something likeI have edit the
to
to allow the user to park with the same vehicle more than once only after checking out.
Why do we are checking for authentication in the observers if they're only executed on context where there's always an authenticated user? (Parking creation only happens in this context: accessing the endpoint '/parking/start' unauthenticated results in authentication error).