Junior Code Review: 13 Tips from Cryptocracy Project

  Рет қаралды 11,119

Laravel Daily

Laravel Daily

Күн бұрын

Another review of a code submitted by one of you in the audience, with a lot of topics to cover.
00:00 Intro
01:11 Naming Things Properly
02:09 Route Grouping
03:33 "Special" Route with dd()
04:37 Global Middleware with Redirect
05:23 This Should NOT Be in Controller
06:45 "Cool" Laravel Collections
08:07 Too "Fat" Model?
09:23 Config Helper
09:48 Don't Use $request-all()
10:22 Shorter Way to Create DB Record
11:25 No Extra Variables/Methods
12:29 Ternary Operator or If-Else
13:26 Eloquent firstOrCreate
Related links:
- Project Repository: github.com/dotangad/cryptocra...
- Intro to Inertia.js with Laravel Breeze Example • Intro to Inertia.js wi...
- Danger of Using $request-all(), and How to Protect • Danger of Using $reque...
- - - - -
Support the channel by checking out our products:
- Enroll in my Laravel courses: laraveldaily.teachable.com
- Try our Laravel QuickAdminPanel: bit.ly/quickadminpanel
- Purchase my Livewire Kit: livewirekit.com
- View Laravel Code Examples: laravelexamples.com
- Subscribe to my weekly newsletter: bit.ly/laravel-newsletter

Пікірлер: 39
@belce1982
@belce1982 2 жыл бұрын
Thanks as always Povilas. You are almost at 75k, what a milestone!! Don't stop now, keep pushing!
@Shez-dc3fn
@Shez-dc3fn 2 жыл бұрын
ternary operators are good if you have just one conditional, in this case you had two and if, if statement is quite intensive, then i would leave it as the long version if couple of variables depend on it. but in this example they could initialise the two variables as null and then use if to do what they do so no else..
@angelp11
@angelp11 2 жыл бұрын
Good review bro :3 I learn so much
@alexaverkiyev9099
@alexaverkiyev9099 2 жыл бұрын
Also scopes is a good practice. I'd replace all those 'where's to scopes, since they are used many times
@anthoniG
@anthoniG 2 жыл бұрын
First well done Angad Singh and keep up the learning process. Second I actually like how you have structured and might "borrow" that idea :) Lastly, where are you hosting it and you find it difficult to deploy?
@sksmf
@sksmf 2 жыл бұрын
5:04 the person is using env(). Env() should be avoided, and create a config file or use any other config file. In the moment you cache your config (that you should do in production) env gonna return null.
@intipontt7490
@intipontt7490 2 жыл бұрын
7:41 using array_values() instead of Collection's values() method for some reason. 8:45 I disagree, but it would look nicer if the non-relationship related methods were in a trait, or at least were all grouped (first all accessor/mutators, then relationships, then the "game methods").
@TravisFont
@TravisFont 2 жыл бұрын
Using global functions in Laravel should be avoided. This is secretly façade noise. I see nothing wrong with this developer using Config::get() as it doesn't attach the codebase to the framework. You can see this pattern be reused such as Shortlink::get() elsewhere. This keeps convention. Overall, solid review! It's nice to see these. Lots of good reminders.
@ThanuraNadun
@ThanuraNadun 2 жыл бұрын
7:11 There is a N+1 issue from that map
@PunyFlash
@PunyFlash 2 жыл бұрын
I'm actually not agree with grouping routes by middleware. Usualy different routes may have different combinations off middleware pipes, so in case you change it, you will need to change the whole structure of your route groups. The correct way to group is by their meaning which usualy separated by route prefix. For example admin routes, auth routes, resources etc. In this case there may be general middlewares which you can add to the whole group or to a single routes in this group. This is also gives you a tree-like vision of all routes from a whole route file.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Well yeah, I agree, what I mostly meant is to group by *something* instead of repeating middlewares and putting it all in one bigger routes file.
@soniablanche5672
@soniablanche5672 2 жыл бұрын
I would create a scope function inside the model for those long queries in the controller
@rafael98php
@rafael98php Жыл бұрын
Hello everything is fine? I'm from Brazil and I love your content! congratulations. A doubt that I didn't understand much in the video. when we need to create a certain function, but this function will not be called in the route, but it will be a function that has only a random logic, so the ideal is to take this function from the controller and play it to a service? thank you in advance and if you answer me it would be very grateful :)
@gtsmeg3474
@gtsmeg3474 2 жыл бұрын
5:21 I mostly use in_array method instead of preg_match if I have fewer values like this case is 3 only.
@AneesKhan-uz4mz
@AneesKhan-uz4mz 2 жыл бұрын
all hail dq
@aogunnaike
@aogunnaike 2 жыл бұрын
Still a newbie to Laravel, where would you suggest I start from?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
You can follow my roadmap: github.com/LaravelDaily/Laravel-Roadmap-Learning-Path
@seppotaalasmaa3404
@seppotaalasmaa3404 2 жыл бұрын
Thanks :) What keyboard you use?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Macbook Pro 2020.
@warwarrior0
@warwarrior0 2 жыл бұрын
This is gold mine. Who wants some gold?
@leslysuarez9686
@leslysuarez9686 2 жыл бұрын
Wow Povilas, I'm starting with Laravel and now I'm learning how to make my API, well, in another comment I saw your repository, I'll read it to support myself with the path. Thanks for sharing your knowledge. Now can you suggest any article or video to help me with the API update? I can't (yet) update the image from the postman, thank you very much! Sorry for so much text :(
@LaravelDaily
@LaravelDaily 2 жыл бұрын
What do you mean by "API update"? Can you give more details, what is the "image" - is it a file, or text field, and what do you want to happen on that update?
@AlanD20
@AlanD20 2 жыл бұрын
Uploading files only work with POST method. Make sure you are using POST method. You can not use methods other than POST method to upload a file. Unfortunately.
@leslysuarez9686
@leslysuarez9686 2 жыл бұрын
@@LaravelDaily from postman I try to update the image (which is a file) of a 'book' and if I do it by sending everything from a form it tells me that the first field cannot be empty, but it is not, so I tried to update by sending it a JSON and If I update correctly, but my question is, how do I update the image if I cannot send it from postman?
@leslysuarez9686
@leslysuarez9686 2 жыл бұрын
@@AlanD20 but that's how I asked Povilas, so how do we update the postman image? I just can not? And then how do I save it when I send it from a front?
@AlanD20
@AlanD20 2 жыл бұрын
First, make a route for POST method, then you can validate the file for example: _$request->validate([_ _'profile_image'=> ['file','image','mimetypes:image/jpeg,image/png',_ _'mimes:jpg,png'],_ _]);_ after validation, you can get the file with file method as following: _$file = $request->file('profile_image');_ then you can save the file with store method, first argument is the location of your image to be saved, second is what driver you want to save into. public driver means it will be saved in public folder in your *storage/app/public* . _$file->store('images', 'public');_ You can find your image in your laravel project, *storage/app/public/images/my_image.jpg* You can find more information in laravel documentation: *laravel.com/docs/8.x/requests#files* Regarding, how you can use postman to send a file, create a new request in Postman then put your URL, after that, make sure in headers section you set the *accept* to *application/json* if you cant find that one you can just add it manually by defining the key to *accept* and the value should be *application/json.* After that, You can only send file in *form-data only,* so make sure set *form-data* in your body, then define the key which we set *profile_image* and change the Text to file, then you can upload a file through postman. *Remember,* form-data sends data only in string format, so if you are sending other data types along with your image, for example, you may want to send price of a book along with the image of the book, when you get both request from your backend, you get both of them in string data type, so if you have validation for price that should be numeric, you might encounter errors, so, there are many ways to do it. first method is, I would suggest to make specific route only to upload images. Second method is, you can create middleware to convert the requests to their original data type then send it for validation. *Remember Middleware works before validation.* Third method is, if you are using form request validation you can define *prepareForValidation()* method in your form request. there are many work around to get it done. more information regarding in Laravel documentation: *middleware: **laravel.com/docs/8.x/middleware* *prepareForValidation Method: **laravel.com/docs/8.x/validation#preparing-input-for-validation* *Additional steps to upload a file through postman* -sometimes, you may not be able to upload file through postman, to fix the issue all you have to do, go to your postman settings at the top right of your menu bar, then go to General, after that, scroll down and make sure to enable *Allow reading files outside working directory* . -Another issue is, sometimes after enabling that option you may also not be able to upload, again if the first solution doesnt work, try to make *Postman* folder in your *C:\Users\%USERPROFILE%* then a new folder inside your Postman folder by *files* . The final location would be an empty folder at *C:\Users\%USERPROFILE%\Postman\files* I believe both should work fine. Let me know if you encouter any errors
@vivekwah
@vivekwah 2 жыл бұрын
Did I just heard 20 years. After working 2 years on PHP I got bored and looking for job change with other domains/languages and this man. This will take some time to sink in. Big salute sir.
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Well yeah, I created my first website at 16. Now I'm 36.
@angelp11
@angelp11 2 жыл бұрын
In what cases should we use the "fillable" configuration in our models?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I advise to use them all the time.
@angelp11
@angelp11 2 жыл бұрын
@@LaravelDaily OMG 😱 Thank you so much, you are a total hero.
@goldenmamba4839
@goldenmamba4839 2 жыл бұрын
That unnecessary variable is just a trust issue i dont know why it happens to me a lot too😂
@yungifez
@yungifez 2 жыл бұрын
Fun fact Uncle Bob would tell you to not do things the laravel way😂😂😂😂
@TravisFont
@TravisFont 2 жыл бұрын
hahahahaha
@alanvasquez9968
@alanvasquez9968 2 жыл бұрын
You hide the developer, but we can see him in his Github
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Yes later I noticed that there was no point in hiding
Laravel Junior Code Review: 12 Tips on Everything
15:30
Laravel Daily
Рет қаралды 74 М.
Laravel Junior Code Review: Security and Consistency
17:29
Laravel Daily
Рет қаралды 48 М.
Became invisible for one day!  #funny #wednesday #memes
00:25
Watch Me
Рет қаралды 57 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:25
CRAZY GREAPA
Рет қаралды 25 МЛН
Junior Code Review: Cleaning Up Laravel CRUD
14:54
Laravel Daily
Рет қаралды 68 М.
From Laravel Blade to Vue.js + API [Live-coding Example]
22:28
Laravel Daily
Рет қаралды 168 М.
Laravel Code Review: Why NOT Use Repository Pattern?
14:21
Laravel Daily
Рет қаралды 76 М.
Eloquent Soft Deletes: Things You May Not Know
10:57
Laravel Daily
Рет қаралды 20 М.
Thoughts About Unit Testing | Prime Reacts
11:21
ThePrimeTime
Рет қаралды 213 М.
Declare Variables in Templates: Angular's New @let Declaration (v18.1)
11:55
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 462 М.
The Tools I Use to Build Products in Laravel
19:00
Josh Cirre
Рет қаралды 17 М.
Does size matter? BEACH EDITION
0:32
Mini Katana
Рет қаралды 18 МЛН
Козы Едят Траву за Деньги
0:31
РЕТАРД
Рет қаралды 2,3 МЛН
What did I eat? 🤪😂 LeoNata Best #shorts
0:19
LeoNata Best
Рет қаралды 2,1 МЛН
Immersive outdoor dining Chinese rural dog Little puppy Rural dog
0:30
Country Puppy
Рет қаралды 25 МЛН
Самая Благодарная Сестра ❤️
0:26
Глеб Рандалайнен
Рет қаралды 1,8 МЛН