Тёмный

Laravel + Service Pattern + DTOs = ❤️❤️❤️ 

Przemysław Przyłucki
Подписаться 5 тыс.
Просмотров 44 тыс.
0% 0

Hi guys!
In this video series, I'll be showcasing a simple way to integrate service pattern into existing Laravel applications. We'll start with a super simple example - an app that stores blog posts from the app and the api.
We'll do a couple of iterations before landing in type-safe service pattern solution that utilizes DTOs.
For more content visit saaslaravel.com

Наука

Опубликовано:

 

9 янв 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 100   
@free2idol1
@free2idol1 10 месяцев назад
<a href="#" class="seekto" data-time="675">11:15</a> using `tap()` to get the updated model instance after the update. This is a great thing for me. Thanks for fabulous video.
@abdullah.alhabal
@abdullah.alhabal 2 месяца назад
yeah
@kpomeru
@kpomeru 11 месяцев назад
I had to take my time to understand this from the solution back to how it was implemented. This is insightful
@peterpardo4138
@peterpardo4138 9 месяцев назад
This is a GEM! Thank you so much for sharing this. I really appreciate you!
@GayanSanjeewaKarunarathne
@GayanSanjeewaKarunarathne 8 месяцев назад
This is just awesome. Good job mate! To be honest I think you covered more than just Services and DTOs. Keep it up 👍
@sunnysk43
@sunnysk43 Год назад
Very very useful! Will definitely use it in my next project! Looks very promising.
@e-raticartist
@e-raticartist 2 месяца назад
huge improvement in the quality of my code after this, next level content
@casweeney
@casweeney Год назад
This video was really helpful, a lot of things now make sense. Thanks so much.
@SerG63rus
@SerG63rus Год назад
One of the best videos which let’s you understand clever way to write code in just less than 20min. Very very useful and pure. Thank you!
@VimKanzo
@VimKanzo 9 месяцев назад
I couldn't agree any better
@soyarenamovediza6498
@soyarenamovediza6498 5 месяцев назад
Loved this video, thanks! You explained many patterns and did very well. I would like a video from yourself explaining some real life scenarios where these patterns really shine by themselves. I’ll share this video.
@unknownstalker2624
@unknownstalker2624 3 месяца назад
Man, you are just awesome! Your content really helped me!
@dongham2865
@dongham2865 Год назад
I like this video. This is exactly the pattern I want. Thanks!
@XTishka1980
@XTishka1980 2 месяца назад
Amazing! Thank you for your lessons
@zachariascreutznacher3093
@zachariascreutznacher3093 Год назад
Very nice, simple and clean description about Services and DTOs in Laravel, thanks man 👍
@natenatters
@natenatters Год назад
Great video! Thats very similar to how we are structuring our application. I like your approach of showing the iterations, a good way to learn. Thanks.
@saaslaravel
@saaslaravel Год назад
Thanks Natan! If you sprinkle some DDD on top it gets even better 🤓
@mayanksgajjar
@mayanksgajjar Год назад
You just earned one more subscriber, thanks for sharing valuable information...😊
@christiansosa5484
@christiansosa5484 Год назад
Great stuff man! thanks for sharing
@GonzaloGuevaraFreire
@GonzaloGuevaraFreire Год назад
Muy buen video. Muy entendible y progresivo.
@lucasdepace
@lucasdepace Год назад
Very cool! Thanks for the video Keep making more videos please!
@saaslaravel
@saaslaravel Год назад
Thanks Lucas!
@mohammadravand4351
@mohammadravand4351 7 месяцев назад
thank you very mutch, this is very excelent video a bout laravel service pattern, 😍😍😍🥰
@chlouis-girardot
@chlouis-girardot Год назад
Nice work ! Thanks for sharing 🙏
@saaslaravel
@saaslaravel Год назад
Thanks for watching!
@AhmadAli-jb6ri
@AhmadAli-jb6ri 4 месяца назад
❤❤ Thanks for this tutorial...
@SeyedaMansour
@SeyedaMansour 9 месяцев назад
wow, this was great
@AdrianoAlves7
@AdrianoAlves7 Год назад
I really do not understand why add complexity and more entities when we have some methods in Laravel to get all the validated data from a request. I like and i employ the Service Pattern because we can separate the app business logic from models and controllers, but the DTO.. oh my... i can't see any need for this. I would like to see a Video from you about Model Events and BroadCasting them. Thank you and congratulations for your great work, Przemyslaw. Greetings from Brazil.
@saaslaravel
@saaslaravel Год назад
Hi Adriano! Thanks for the kind words. Lemme explain the reasoning: The biggest reason why DTOs are useful is it unifies the data passage through your application. Let's say you have a piece of business logic that needs to be executed from your app, public api, command and a job. DTOs ensure that proper data gets passed to the service. Also, if you ever add a property, it will cause errors in places where it was not implemented / handled which also prevents bugs
@felipeflashmaster
@felipeflashmaster 8 месяцев назад
So using the Model itself as a DTO would be the same, right? or how in typescript to use an interface
@irlnidallah
@irlnidallah Месяц назад
@@felipeflashmaster using the Model as a DTO itself would clutter the Model into a mess, if you're planning to implement DRY, that would not be a recommended approach.
@abdurashid_dev
@abdurashid_dev Год назад
Helpful content! Thank u!
@saaslaravel
@saaslaravel Год назад
You're welcome!
@programmertelo
@programmertelo 10 дней назад
thanks for the great video
@MrSfinks13
@MrSfinks13 9 месяцев назад
Cool! Thank you!
@midclock
@midclock 2 месяца назад
Nice tutorial! You can check ide-helper to generate model auto completion for properties. For dtos I find laravel-data by spatie a very good solution. Cheers
@saaslaravel
@saaslaravel 2 месяца назад
Yeah I use both of them in my workflow! Def worth checking out for anybody interested though!
@laxman123in
@laxman123in 9 месяцев назад
Awesome
@codewithtee
@codewithtee Год назад
This is good. Simple and easy to understand... I have always wanted somethinh simple for dto instead of a package... Will be improving some project with this implementation
@saaslaravel
@saaslaravel Год назад
Thanks! Glad you enjoyed it and good luck on your project
@codewithtee
@codewithtee Год назад
While refatoring, I wonder how you would handle Request for update that has fields set as sometimes for validation. Considering for patch request, not all fields will be supplied.
@saaslaravel
@saaslaravel Год назад
@@codewithtee Often times I just duplicate the request, I like this approach because it's the most flexible one. You could try and extract common rules but as the resource gets more complex I think it just adds unnecessary complexity in something that should be sime. Another solution is to simply check the method in the request and modify the rules array but I find it harder to read :)
@toheebabiodun70
@toheebabiodun70 Год назад
same here too..
@B0re_d
@B0re_d 21 день назад
I would add the transformation method to request classes. In my opinion you should not have to extend DTO classes when adding new pieces of code eg. API v2 instead you should have a method called asDto(or toDto) in the request class and call them instead of making X static factory methods. Overall great content :)
@muhammadumarsotvoldiev8768
@muhammadumarsotvoldiev8768 17 часов назад
Thank you very much!
@gazorbpazorbian
@gazorbpazorbian Год назад
awesome video dude! saw it again :D
@saaslaravel
@saaslaravel Год назад
Legend!
@brunofunnie
@brunofunnie Год назад
B-e-a-u-tiful!
@user-dk4or7bs2q
@user-dk4or7bs2q Год назад
So nice
@NoNameUkr
@NoNameUkr 7 месяцев назад
I prefer to pass array into static DTO method, then Request, for me its much safety, u can use this DTO from wherever u want, not only from Controller. Example: tomorow u will need to create Post from console command or from Job
@saaslaravel
@saaslaravel 6 месяцев назад
Which you can still do by instantiating the DTO from the constructor. Imo array passing loses all of the benefits of type checking built into the class
@borakayalar
@borakayalar 6 месяцев назад
Perfect
@bakre_dev8728
@bakre_dev8728 Год назад
Cool
@nikolayrogoza4240
@nikolayrogoza4240 27 дней назад
Great tips, but i more like to use different actions as class, not as a part of service. Its easy to extend from basic model to others
@gazorbpazorbian
@gazorbpazorbian Год назад
this is a very cool video.. nicely explained. what about making a video about php types?
@saaslaravel
@saaslaravel Год назад
Thanks so much! Types as in strings, ints etc? What would you like to know about them?
@gazorbpazorbian
@gazorbpazorbian Год назад
@@saaslaravel everything hehe, like using a separated file, best practices, how to use it in a function like boot(): void and all that stuff.
@antonsobchuk7690
@antonsobchuk7690 10 месяцев назад
Great job, man. What’s the theme of phpstorm you are using btw?
@LowVi
@LowVi Месяц назад
Really good man! I would like to know what you think about adding an extra layer: the repository. Because thats kinda whats going on my in job rn, laravel + service pattern + dto + repository, I feel like the Model itself could act as the Repository, what do you think?
@saaslaravel
@saaslaravel Месяц назад
It really depends on what you are looking for from the solution. I'd say - if you don't have a good reason to introduce a repository - don't do it. If you want to have more fine grained control over your persistence layer (i.e to handle CQRS more easily) go for repository!
@chris11651
@chris11651 Год назад
very informative, thank you very much! what theme do you use in your IDE?
@saaslaravel
@saaslaravel Год назад
Thank you! It's called Catpuccino Mocha
@chenten420
@chenten420 Год назад
This is a really great video! Is there a repository where I can check the code for this example? I would greatly appreciate it.
@saaslaravel
@saaslaravel Год назад
I don't have it unfortunately but will probably start doing it going forward
@VipStudio
@VipStudio 3 месяца назад
When you have form request class included into ur method you dont need to do $request->validated cuz the data is already validated otherwise it throws validate exception until first line of code starts compile from function
@VipStudio
@VipStudio 3 месяца назад
And you dont need to use Dto u can just do create($request) //already validated clean data
@artemkanarskyi8231
@artemkanarskyi8231 Год назад
Thanks is very helpful! I want ask as how use DTO when you have relations for example Post has many Comments. In PostDTO comments will be array/collections of CommentsDTO or simple array of ids?
@saaslaravel
@saaslaravel Год назад
Thanks Artem! It really depends on the scenario, I think there is a use-case for both Option A: Nested models created all at once. Example: You create BlogPost and it has different "sections" of content, or you create a Recipe that has Ingredients Solution: In this example you'd have a RecipeDTO that has all of the recipe properties, and either an array of IngrednetDTO, or a IngredientDtoCollection (the latter if you want something that's more type-safe) Option B: Creating a new model and associating it with existing ones Example: You select which users should have access to a project Solution: In this example you'd have a ProjectDTO, that'd contain an array with IDs of users to associate and then simply attach it to a pivot table Lemme know if this makes sense!
@artemkanarskyi8231
@artemkanarskyi8231 Год назад
@@saaslaravel Thank you for your help.
@lassestube
@lassestube Год назад
You don’t need constructor, you can bind service in controller method automatically...
@saaslaravel
@saaslaravel Год назад
That's true, but I prefer using constructor. The method tends to have 2/3/4 arguments - with type hints it takes a lot of space, and when it lives on constructor level I can just define it once :D
@lassestube
@lassestube Год назад
@@saaslaravel makes sense as well :) I tend to use actions in method binding and if I use a service in more than one method I would promote it to the constructor.
@nicolascanala9940
@nicolascanala9940 Месяц назад
Hey! How do you approach having different validation rules in the store and update actions? For example, a StoreBlogRequest which marks all fields as required, VS an UpdateBlogRequest which doesn't (for example, you just wanna update the name). Would you create more than one DTO and mark the properties as nullable?
@saaslaravel
@saaslaravel Месяц назад
Hey there! It depends on the situation, generally speaking with updates I'd not go with nulls because partial updates are pretty sexy - if you use spatie/laravel-data - they handle it out of the box with Optional type hint
@huntwithmeid
@huntwithmeid Год назад
Well done bro! Great videos. But I have a question here, so what is the benefit of using DTOs? I'm interested to use this in my project, is it hard to implemented in existing project. I'm also using service class.
@saaslaravel
@saaslaravel Год назад
The main benefit is making sure that the data you're passing is valid data that service can consume. This is especially useful when you have multiple pieces of app - console, public api and internal api calling the same service. You can just start implementing it on new services and then gradually go over existing code! Hope that helps :)
@pwcodigo
@pwcodigo Год назад
@@saaslaravel Obrigado !
@GonzaloGuevaraFreire
@GonzaloGuevaraFreire Год назад
Además que simplifica el constructor
@Pavelbrov
@Pavelbrov Месяц назад
<a href="#" class="seekto" data-time="350">5:50</a> - what about duplication in BlogPostRequest validation for api and app? Can we get rid of it?
@zulfikarditya7142
@zulfikarditya7142 Год назад
Very nice tutorial. But i have a question, how to use DTO in many relational data like transaction with many items or something?. and we'll be use service as well,
@saaslaravel
@saaslaravel Год назад
It really depends on the usecase so it's hard to give you a good response here but you can also nest them and utilize arrays with phpdoc type hinting to skip some of the type limitations of php
@JSONFX
@JSONFX 3 месяца назад
What font are you using on this video? Thanks!
@Pavelbrov
@Pavelbrov 9 месяцев назад
Is "laravel-data" package from spatie doing something similar?
@saaslaravel
@saaslaravel 9 месяцев назад
Yes, it's just a concept that you need to know from scratch. Just a regular, plan, old PHP class. Nothing wrong with using the package!
@iqbalrafli2191
@iqbalrafli2191 8 месяцев назад
Good video, but i have a questions. What if we want to store data to two or more table from one endpoint controller?
@saaslaravel
@saaslaravel 8 месяцев назад
You can include all data into one dto or pass two separate DTOs. It will depend on the usecase
@mihaes7172
@mihaes7172 5 месяцев назад
I don't understand why instantiating a readonly DTO just to pass request vars to a writing service method? what's the 'secret' between request vars and service update method that dto needs to exists inside same controller method, concerning the same Post model? what if there are 25->50 controllers & models in the application? that would generate a lot of fancy classes, code to be reviewed, etc. Sometimes we go overboard for the sake of buzzwords....
@Pavelbrov
@Pavelbrov Месяц назад
<a href="#" class="seekto" data-time="1040">17:20</a> - Shouldn't our DTO have a method like toArray so that we can manually avoid passing all the parameters? I mean, they correspond to the properties of the model. Or is this a bad practice? Like this public function toArray(): array { $properties = get_object_vars($this); $array = []; foreach ($properties as $key => $value) { $snakeCaseKey = Str::snake($key); $array[$snakeCaseKey] = $value; } return $array; } and then BlogPost::create($dto->toArray());
@saaslaravel
@saaslaravel Месяц назад
I prefer to be explicit when possible, realistically, just use spatie/laravel-data which handles this out of the box
@3dxspx703
@3dxspx703 Месяц назад
Yikes. Model mutators and observers will be enough guys. But if you want to Over-engineer your code, then go on with dto or something like repository pattern. 😅
@saaslaravel
@saaslaravel Месяц назад
Until your app had 1200 endpoints and some places haven't been touched for years - yeah
@3dxspx703
@3dxspx703 Месяц назад
@@saaslaravel yeah? But I prefer laravel's defaults for easier maintenance and debugging. Also KISS. Pfft
@LucasOliveira-ny6kw
@LucasOliveira-ny6kw 2 месяца назад
.
@deecee2204
@deecee2204 6 месяцев назад
Why is this so painful to watch? no offense
@saaslaravel
@saaslaravel 6 месяцев назад
Idk, why is it bud?
@zayedadel8552
@zayedadel8552 Месяц назад
million times better and understandable than any indian out there that i cannot understand
@MrSfinks13
@MrSfinks13 5 месяцев назад
Thank you! Very useful video, realy!
Далее
Laravel: Repository Pattern in practice
27:13
Просмотров 10 тыс.
One moment can change your life ✨🔄
00:32
Просмотров 8 млн
WOW! Laravel's pipeline pattern is AWESOME
20:39
Просмотров 12 тыс.
Refactoring to action pipelines in Laravel
1:25:03
Просмотров 9 тыс.
Are VALUE OBJECTS secretly overpowered?!
13:44
Просмотров 3,3 тыс.
I Cannot Believe TypeScript Recommends You Do This!
7:45
Top 5 Laravel "Bad Practices" (My Opinion)
10:32
Просмотров 19 тыс.
Главная Ai фишка Samsung
0:37
Просмотров 185 тыс.
Я УКРАЛ ТЕЛЕФОН В МИЛАНЕ
9:18
Просмотров 110 тыс.
Acer Predator Тараканьи Бега!
1:00
Просмотров 465 тыс.