Laravel and variable naming conventions (snake case + camel case)

Asked
Active3 hr before
Viewed126 times

6 Answers

laravelvariable
90%

Thanks for contributing an answer to Stack Overflow!,How can we easily convert all model attributes, relations and general Laravel bindings into camel case?, Stack Overflow Public questions & answers , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

see vendor\laravel\framework\src\Database\Eloquent\Model.php also you could see these values are set here for checking from models.

public static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany'];
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
load more v
88%

Normal variables should typically be in camelCase, with the first character lower case., Laravel Naming Conventions,Laravel naming conventions,These should follow the same rules as model methods. I.e. camelCase (first character lowercase).

Generally, your models should be able to automatically work out what database table it should use from the following method:

< ? php
/**
 * Get the table associated with the model.
 *
 * @return string
 */
public
function getTable() {
   if (!isset($this - > table)) {
      return str_replace(
         '\\', '', Str::snake(Str::plural(class_basename($this)))
      );
   }
   return $this - > table;
}
load more v
72%

Fat models, skinny controllers , Comment your code, but prefer descriptive method and variable names over comments ,Move validation from controllers to Request classes.,A controller must have only one responsibility, so move business logic from controllers to service classes.

$request - > session() - > get('cart');
$request - > input('name');
load more v
65%

Gegasoft uses the following name conventions
for laravel
   ===
   === === === === === === === === === === === === === === === === === === === =
   Variables => camelCase => $articlesWithAuthor
Collection Variable => descriptive, plural => $activeUsers = User::active() - > get()
Object Variable => descriptive, singular => $activeUser = User::active() - > first()
View => snake_case => show_filtered.blade.php
Controllers => singular, ProperCase => ArticleController
Model Name => singular, ProperCase => User, FollowingRequest
Model attribute / property => snake_case => $model - > created_at
Method => camelCase => getAll
Method in resource controller => table => store
Method in test class => camelCase => testGuestCannotSeeArticle
hasOne / belongsTo relation => singular => articleComment
Other relations => plural => articleComments
Table Name => plural => article_comments
Table column => snake_case without model name => meta_title
Route => plural => articles / 1
Named route => snake_case with dot notation => users.show_active
Primary key => - => id
Foreign key => singular model name with _id suffix => article_id
Pivot table => singular model names in alphabetical order => article_user
Migration => - => 2017_01_01_000000_ create_articles_table
Config and language files index => snake_case => articles_enabled
Config => snake_case => google_calendar.php
Contract(interface) => adjective or noun => Authenticatable
Trait => adjective => Notifiable
75%

In my heart of hearts, I prefer tabs and snake_case variables over spaces and camelCase. But that fight was lost a long time ago :'(,I prefer snake_case for PHP variables, and only plural if it's an array. I always camelCase my function names, and again snake_case for db schemas.,For variables I struggle with consistency because I often feel like it comes down to what the variable holds. e.g. I prefer to use cameCase for objects, but snake_case for strings and simple numerics.,I use snake_case only to refer to database fields. All overt variables are camelCase. It really helps when I want to know where data is coming from. If I see $model->attribute_name it’s a database field. If I see $model->attributeName is a magic method.

For variables I struggle with consistency because I often feel like it comes down to what the variable holds. e.g. I prefer to use cameCase for objects, but snake_case for strings and simple numerics.

cameCase

For variables I struggle with consistency because I often feel like it comes down to what the variable holds. e.g. I prefer to use cameCase for objects, but snake_case for strings and simple numerics.

snake_case
load more v
40%

The best id is with underscore (kebab-case) or camelCase just like below.,Blade file should be kebab-case in lower underscore between words.,Naming Conversion for Id,Its time to naming conversion for controller name for what is the controller name is suitable for writing best namig.

Controllers should be in singular case no spacing between words we known this type namig is PascalCase and not plurel just like below.

PostController, UserController, ProfileController
load more v

Other "laravel-variable" queries related to "Laravel and variable naming conventions (snake case + camel case)"