Skip to main content

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/start and /parkings/stop?
  • Should it be POST /parkings and PUT /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.

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

A
andywong31 ✓ Link copied!

Hi Povilas, question regarding your code under start method of ParkingController:

$parking->load('vehicle', 'zone');

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.

PK
Povilas Korop ✓ Link copied!

Right, you're probably right, this time I was overly cautious. But better safe than sorry! :)

BD
Bless Darah ✓ Link copied!

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.

K
kalDeveloper ✓ Link copied!

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

AA
Ali Al Qahtani ✓ Link copied!

In start method, 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.

PK
Povilas Korop ✓ Link copied!

Good catch, didn't think of that validation.

CM
Chris McGee ✓ Link copied!

What would be the best way to implement such validation?

N
Nerijus ✓ Link copied!

In the repo it was inplemented, updated this lesson with the code.

K
Karolis ✓ Link copied!

$parkingData validation section exists has table vehicles and the column deleted_at. Need to update migration/ model files to add softDelete.

EC
Elmar Cabbarlı ✓ Link copied!

I think you put the wrong screenshot "We launch this endpoint with success!" (after this sentence)

PK
Povilas Korop ✓ Link copied!

Thanks, another well spotted mistake, changed to the correct screenshot. It's so good to see people actually READ the content :)

EC
Elmar Cabbarlı ✓ Link copied!

you forgot to add "id" in ParkingResource.php

return [ 'id' => $this->id, ...

PK
Povilas Korop ✓ Link copied!

Well spotted! Fixed now.

V
vilyo ✓ Link copied!

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?

PK
Povilas Korop ✓ Link copied!

No difference, any Service Provider is fine.

HN
Huy Nguyen ✓ Link copied!

Regarding

Route::get('parkings/{parking}', [ParkingController::class, 'show']);
Route::put('parkings/{parking}', [ParkingController::class, 'stop']);

Would it be better it we add these constraints for these routes?? Like this

Route::get('parkings/{parking}', [ParkingController::class, 'show'])->whereNumber('parking')
Route::put('parkings/{parking}', [ParkingController::class, 'stop'])->whereNumber('parking')
PK
Povilas Korop ✓ Link copied!

Yes I think it's a good suggestion, great comment.

FZ
fatima zohra ezzaidani ✓ Link copied!

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 ?

PK
Povilas Korop ✓ Link copied!

Pretty sure it was created earlier, with $table->softDeletes();

M
Márlon ✓ Link copied!

Hello! It is ok in the repository, but in the text lesson, not was created.

PK
Povilas Korop ✓ Link copied!

Oh right, I see it now. Fixed in the lesson, thanks for the notice!

FZ
fatima zohra ezzaidani ✓ Link copied!

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!

PK
Povilas Korop ✓ Link copied!

Weird, it did for me. Well, you can rewrite it with if-statement or ternary then, try something like $this->stop_time->toDateTimeString() ?? null or if-statement

FZ
fatima zohra ezzaidani ✓ Link copied!

Yeah I fix it using Carbon, thank you Povilas!

K
kaitrenbath ✓ Link copied!

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.

PK
Povilas Korop ✓ Link copied!

Well noted, thanks! Weird, in the repository scopes are here but perhaps forgot to add them to the lesson text itself. Fixed now!

CS
Cesar Schefer ✓ Link copied!

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.

CS
Cesar Schefer ✓ Link copied!

Well, someone noticed the same in the next video :)

LA
Luis Antonio Parrado ✓ Link copied!

Hi Povilas, The syntax

'exists:vehicles,id,deleted_at,NULL,user_id,'.auth()->id(),

applied in ParkingController.php validation 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.

DL
David Lun ✓ Link copied!

This translates to:

value provided exists on vehicles table id column AND deleted_at column has value NULL AND user_id is of currently authenticated user

so you can request to start parking for car you own that is not deleted otherwise request would be invalid

LA
Luis Antonio Parrado ✓ Link copied!

Are they any way more readable to write this line, using for example the Rule class? I have saw something like

Rule::exists('vehicles')->whereNull('deleted_at') . .
;
I
IsaiasXavier ✓ Link copied!

I have edit the

if (Parking::active()->where('vehicle_id', $request->vehicle_id)->exists())

to

if (Parking::active()->where('vehicle_id', $request->vehicle_id)->whereNull('stop_time')->exists())

to allow the user to park with the same vehicle more than once only after checking out.

A
augusto-dmh ✓ Link copied!

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).

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.