Laravel: How to Name Various Things

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today let's talk about how to name things in laravel because often i see inconsistencies in people's code which has two problems lead to two problems first code is less readable or in some cases it doesn't work with laravel standards or php standards and may even break the code so this example on the screen when you're making the model php artisan make model is it singular or plural also controller singular or plural do you need suffixes or prefixes in file name class name let's talk about all of that in this video and i have around a dozen of tips on naming different things in laravel and php some of them will be laravel specific some of them php specific some of them will be my personal recommendation and advice just from personal experience and the first one is around model so the thing you see on the screen the rule of thumb for laravel is that model names should be singular make model book not books why is that because in laravel model when it is generated and it's empty it assumes that the table name that model will work with will be plural from this english word and by the way english is important please please please do not name things like classes in your native language if it's not english for two reasons first you make the code not readable for anyone who is not your language speaker and you never know when the team expands internationally even if it's in your language currently and second thing you may break the laravel stuff so for example if you generate make model uh for example in german book right then it generates the model book which assumes that your database table name will be books as at the end because laravel is trying to make plural from this word and in german plural of book is not books it's booham from what i remember and imagine for a non-german speaker to read that class it doesn't make sense what it does and what it is so singular english word and if you want to make a database table from two words from multiple not just for example review but book review it should be in camel case or in pascal case i think it is called so uppercase each word and it would assume that the table that works with is plural for the last part so it should be book reviews by default the table name of the database but if you already have the table name differently for example and you need to customize that you can of course customize it in table name for example if you have books reviews already just tell laravel that your database table name is different from your model convention next thing controller name should it be book controller or books controller and in fact both options are okay there's no error if you have books or book but the preferred method is book controller and how do i know that let's try to generate the model with all the options so there is make model book minus a which would generate a lot of files with that model so factory migration seeder and controller and let's see what controller it generates app http controllers book controller singular so laravel itself recommends and automatically generates singular controller but there's no strict rule here is just what laravel generates but if you have books controller it's all okay next tip is about naming blade files and structuring them into folders please try to keep it in order in subfolders according to the action that they have and according to the object that they work with so i often see especially in junior developers code and this is an example from one of my junior developers i currently mentor the method is called show but the view is called book view and if you look at the folder structure of the views it is inconsistent so in some cases there's admin book and then really well index show and edit exactly for the actions and in some cases edit is for like in no subfolder so admin edit for what and in main blade is book index book view so instead of that it should be books subfolder and then index and show blade corresponding to the methods general view probably should be generous show search view that's debatable could be book search for example but underscore view doesn't really give any more value here and it comes from the laravel resource controller so i advise it's not a rule but it's kind of more readable to others like myself when i'm doing code reviews when i see those actions i immediately understand what the method does where's the blade and stuff like that so those route names and those method names should be probably the names of the blade files as well in a subfolder related to that for example photos or in that case as books and in general in more simple projects i advise it's a personal opinion some people could disagree that you should stick to crud resource controller structure there's a wonderful video about that called karate by design by adam watten and i will link to that in the description so watch that one and maybe you will like that philosophy as much as i do but that's not the rule that's just an advice the next tip is about having suffixes or class endings according to the purpose of that class and laravel so in that scenario make model book minus a look what it generated database factories book factory factory in the ending of the class we already saw book controller for controller class there is also a seeder database cedar book cedar it is called cedar at the end so i really like by just seeing the file or the class name i understand immediately what it does whether it's sending email notification notification at the end if it's an exception class so throw something exception at the end so the only class in my opinion that should not have suffix is the model so if it's book i know it's a model but if it's for example book resource i know it's api resource and related to that there was a pull request in laravel documentation by zuzana kunskova recently a month ago she proposed to name the resource not by user but user resource let's zoom it in so for a long time in the documentation for api resources there was this but it's actually user resource so if you generate resource user it would conflict even by file name by class name with user model and then even while typing for searching for file in php storm or sublime or vs code you type in user and both would appear and probably by typing user resource you find exactly what you need next thing is migration so by default in this command it generates file name create books table with timestamp and that is not a coincidence when you do php artisan make migration and if that migration generates a new table the syntax should be create underscore your table name for example brook reviews underscore table then laravel will help you to generate the part of the migration which would look like this let's take a look so book reviews this part would be generated automatically schema create because you specified that you're creating a new table and then everything that is between create underscore and underscore table will become table name here in comparison imagine doing php artisan make migration books for example just like this it would generate books but without any code and if your migration is not about creating a new table but rather modify an existing ones you want to do something like this so whatever is in your beginning so modify or whatever in book reviews table so it generates correctly schema table not schema create which means editing of the table and book reviews is before table but i'm not sure how it defines that in is not a table name i'm not sure actually let's try like this would it have three table names out of three parts let's see okay so it does need to have in i actually prefer two so add fields to book reviews modify whatever to book reviews table or add fields for example add user id to book reviews table something like that so it also generates book reviews here so probably you need in or two before the table name maybe some of you know just shoot in the comments and help me with this video because as every developer i don't know everything apparently and finally for all the other naming things in php and in laravel i advise you to use psr standards which is psr 12 latest standard and i have an article on that on our quick admin panel blog about why you should use that for example what is the problem that a code is not readable enough so things like tabs or spaces two spaces versus four spaces snake case or camel case for class names and method names everything is in psr 12 standard so for example if we open it up officially there's extending coding styles on how to name things in php so please read this one and also psr1 which is the basic coding standard so there's a basic and there's extended psr 12 on top and if you want to generate your code or reformat it with phpstorm for example it would help you with that i guess something similar exists in sublime and vs code and then your code will be more readable for everyone on your current team even if it's yourself in the future or in your team in the future which may be international which may be bigger and it would also help me if sometime in the future i review your code and make my recommendation on how to improve that that's it for this time subscribe to the channel because it's daily now and see you guys in other videos
Info
Channel: Laravel Daily
Views: 22,283
Rating: undefined out of 5
Keywords:
Id: 6f7AvZF8ciY
Channel Id: undefined
Length: 10min 25sec (625 seconds)
Published: Wed Feb 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.