Skip to content

Instantly share code, notes, and snippets.

@tojibon
Last active May 27, 2019 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tojibon/bfcbc2142aa39db516736dbc1d8b166c to your computer and use it in GitHub Desktop.
Save tojibon/bfcbc2142aa39db516736dbc1d8b166c to your computer and use it in GitHub Desktop.
My Laravel Best Practices
  • Always follow PSR2 - PSR4 php coding style, better install it on PHPStorm
  • Always validate things via RequestValidators even if it is nullable(), Move validation from controllers to Request classes.
  • Use child relationships to get child data and filter data
  • Make sure if there is any auto generated codes like __construct
  • How can a array count ever be less than 0? Check to use a simple condition instead of making it complex with double check
  • Check if there is anything should actually fail if it doesn't have a set value
  • It's a good idea to check first for the less costly conditions (especially inside a loop)
  • Always use Carbon for date processing
  • Maintain code commenting and proper docblock
  • Always use custom env for static values, do not get data from the .env file directly
    • // config/api.php 'key' => env('API_KEY'), // Use the data $apiKey = config('api.key');
  • Confirm if a method really need a default value to be set or not
  • If you need a fixed id to be used somewhere from a table add a const for that specific Model
  • Use ::select() if we are only going to use one field, use pluck if you need a list
  • For Routing always user RESTful design
  • Create new migrations with only the changes and don't drop existing tables that have data in production
  • All string fields need max length validation
  • All relational field should have a validation if it exists
  • Don't mix array indexing with object indexing on the same function. unless there's a necessity to use both
  • Let's try to at least be coherent inside a block of code with variable name (snake_case) and indexing (array|object)
    • snake_case - where all words are concatenated in lowercase with underscores
  • Always condider visibility (public|protected|private) of a property, a method or (as of PHP 7.1.0) a constant
  • Storing Relationships in Variables, Try to reduce the relationship calls as much as possible. $posts = $user->posts;
  • Always make sure not to overcrowd your route.php file. Avoid writing closures in your route.php file.
  • Avoid N+1 queries, This is when you query for relations inside of a loop, User with() $comments = Comment::with('author')->get();
  • Take Care of Eloquent Relation Caching - Because once it queries the relation, it will stores it in the cache for that request
  • Follow - Fat Models and Skinny Controllers - Put all DB related logic into Eloquent models or into Repository classes if you're using Query Builder or raw SQL queries
  • Don't repeat yourself (DRY)
  • Prefer to use Eloquent over using Query Builder and raw SQL queries. Prefer collections over arrays
  • Use config and language files, constants instead of text in the code
  • Use shorter and more readable syntax where possible, request(), session()
  • When creating a migration always use proper naming
  • When adding a new column always use proper type and position of the field, use after() nullable() unique() etc
  • Never change the date time of a migration.
  • Do not push any commit with unnecessary code commented. Only release final code.
  • Check typos for FileNeam.php, ClassNeam.php, MethodNeam, VerNeam anything with spelling mistakes.
  • For an entity api endpoint make sure you applied for dynamic ordering, sorting and relational data load.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment