Naming Conventions
The following is the generally accepted naming conventions being used by Laravel Community:
Controllers
Controller name MUST start with a noun (in singular form) followed by the word “Controller”.
Good
class ArticleController extends Controller
{ ...
Bad
class ArticlesController extends Controller
{...
class wp_articlesController extends Controller
{
class Article extends Controller
{
You SHOULD Use Resource Controllers unless you have any particular reason not to do so
Good
class DomainController extends Controller
{
public function index(){} // list domains
public function create(){} // show create form
public function store(Request $request){ } // handle the form POST
public function show($id){} // show a single domain
public function edit($id){} // show edit page
public function update(Request $request, $id){} // handle show edit page POST
public function destroy($id){} // delete a domain
}
Bad
class DomainController extends Controller
{
public function list(){} // list domains
public function create_or_save(){} // show create form then handle save
public function show_edit($id){} // show a single domain then show edit page
public function delete($id){} // delete a domain
}
Models
Model names MUST be in singular form with its first letter in uppercase
Good
class Flight extends Model
{
...
Bad
class Flights extends Model
{
...
class flight extends Model
{
...
hasOne or belongsTo relationship methods MUST be in singular form
Good
class User extends Model
{
public function phone()
{
return $this->hasOne('App\Phone');
}
}
Bad
class User extends Model
{
public function phones()
{
return $this->hasOne('App\Phone');
}
}
Any other relationships other than above MUST be in plural form
Good
class Post extends Model
{
public function comments()
{
return $this->hasMany('App\Comment');
}
}
Bad
class Post extends Model
{
public function comment()
{
return $this->hasMany('App\Comment');
}
}
Model properties should be in snake_case
Good
$user->created_at
Bad
$user->createdAt
Methods should be in camelCase
Good
class User extends Model
{
public function scopePopular($query)
{
return $query->where('votes', '>', 100);
}
Bad
class User extends Model
{
public function scope_popular($query)
{
return $query->where('votes', '>', 100);
}
Functions
Laravel comes with a lot of useful helper functions, but you can also define your own helper functions, given the following conditions:
You SHOULD place your custom helper functions by creating a file called helper.php
Good
project_folder/app/helper.php
project_folder/app/Http/helper.php
Bad
project_folder/functions.php
You MUST use Composer’s autoloading capability to load your functions
Good
// file composer.json
...
"autoload": {
"files": [
"app/helpers.php"
],
...
Bad
// file app/Http/Controllers/HomeController.php
class HomeController.php
{
function index(){
require_once(app_path("helpers.php"));
}
}
You MUST check if the the function exists before defining it
Good
if (! function_exists('my_custom_helper')) {
function my_custom_helper($key, $default = null) {
// ...
}
}
Bad
function my_custom_helper($key, $default = null) {
// ...
}
Other General guides with functions
    If the function length exceeds 25 lines, you SHOULD break it down to multiple functions
    Each function SHOULD have a Unit Test associated with it
Routes
Routes should be in plural form of the resource it is trying to manipulate and SHOULD be all lower-case
Good
Route::get('/users', '[email protected]');
Route::resource('photos', 'PhotoController');
Bad
Route::get('/user', '[email protected]');
Route::get('/UsersList', '[email protected]');
Route::resource('PHOTO', 'PhotoController');
Named Routes SHOULD use snake_case and dot notation
Good
Route::get('/user', '[email protected]')->name('users.show_active');
Bad
Route::get('/user', '[email protected]')->name('users.show-active');
Route::get('/user', '[email protected]')->name('show-active-users');
Variables
General rule for variable is it SHOULD be in camelCase
Good
$articlesWithAuthor
Bad
$articles_with_author
Collection names SHOULD be descriptive and in plural form
Good
$activeUsers = User::active()->get()
Bad
$users = User::active()->get()
$user = User::active()->get()
$User = User::active()->get()
Single Object SHOULD be descriptive and in singular form
Good
$activeUser = User::active()->first()
Bad
$users = User::active()->first()
Views
You SHOULD use snake_case as file name of your Blade templates
Good
show_filtered.blade.php
Bad
showFiltered.blade.php
show-filtered.blade.php
You MUST not make non UI-related operations inside blade templates
Good
// $api_results is passed by controller
<ul>
@foreach($api_results as $result)
<li>{{ $result->name }}</li>
@endforeach
</ul>
Bad
@php
$api_results = json_decode(file_get_contents("https://api.example.com"));
@endphp
<ul>
@foreach($api_results as $result)
<li>{{ $result->name }}</li>
@endforeach
</ul>
Last modified 8mo ago
Copy link