Laravel - Database, Table and Column Naming Conventions?
Laravel has its own naming convention. For example, if your model name is User.php
then Laravel expects class 'User' to be inside that file. It also expects users
table for User
model. However, you can override this convention by defining a table property on your model like,
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'user';
}
From Laravel official documentation:
Note that we did not tell Eloquent which table to use for our User model.
The lower-case, plural name of the class will be used as the table name
unless another name is explicitly specified. So, in this case, Eloquent
will assume the User model stores records in the users table. You may specify a
custom table by defining a$table
property on your model
If you will use user table id in another table as a foreign key then, it should be snake-case like user_id
so that it can be used automatically in case of relation. Again, you can override this convention by specifying additional arguments in relationship function. For example,
class User extends Eloquent implements UserInterface, RemindableInterface {
public function post(){
return $this->hasMany('Post', 'userId', 'id');
}
}
class Post extends Eloquent{
public function user(){
return $this->belongsTo('User', 'userId', 'id');
}
}
Docs for Laravel eloquent relationship
For other columns in table, you can name them as you like.
I suggest you to go through documentation once.
Database, Table and Column Naming Conventions?
I recommend checking out Microsoft's SQL Server sample databases:
https://github.com/Microsoft/sql-server-samples/releases/tag/adventureworks
The AdventureWorks sample uses a very clear and consistent naming convention that uses schema names for the organization of database objects.
- Singular names for tables
- Singular names for columns
- Schema name for tables prefix (E.g.: SchemeName.TableName)
- Pascal casing (a.k.a. upper camel case)
Laravel and mysql table name conventions for categories
If your category tables have columns in common, I would suggest using many-to-many polymorphic relation:
product
id - integer
name- string
car
id - integer
manufacturer - string
model - string
categories
id - integer
name - string
categorizable
category_id - integer
categorizable_id - integer
categorizable_type - string
The Product
and Car
models will both have a categories
method that calls the morphToMany
method on the base Eloquent class:
class Product extends Model
{
/**
* Get all of the categories for the product.
*/
public function categories()
{
return $this->morphToMany('App\Category', 'categorizable');
}
}
Inverse of the relationship:
class Category extends Model
{
/**
* Get all of the products that are assigned this category.
*/
public function products()
{
return $this->morphedByMany('App\Product', 'categorizable');
}
/**
* Get all of the cars that are assigned this category.
*/
public function cars()
{
return $this->morphedByMany('App\Video', 'categorizable');
}
}
Model and table name conventions Laravel
If the name of the model is UserAddress
, the name of the table should be plural. So it will be user_addresses
.
Read more about Laravel naming conventions here.
Database/model field-name convention in Laravel?
Snake case is the convention for database fields and model attributes.
You can see it in the getter and setter (http://laravel.com/docs/eloquent#accessors-and-mutators) or relationships sections (http://laravel.com/docs/eloquent#relationships) in the documentation.
Laravel Map DB Column Names Using Proper Convention to Actual DB Column Names in Model
You can create a parent class for all your models:
abstract class Model extends \Illuminate\Database\Eloquent\Model {
protected $columns = [];
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($this->columns as $convention => $actual) {
if (array_key_exists($actual, $attributes)) {
$attributes[$convention] = $attributes[$actual];
unset($attributes[$actual]);
}
}
return $attributes;
}
public function getAttribute($key)
{
if (array_key_exists($key, $this->columns)) {
$key = $this->columns[$key];
}
return parent::getAttributeValue($key);
}
public function setAttribute($key, $value)
{
if (array_key_exists($key, $this->columns)) {
$key = $this->columns[$key];
}
return parent::setAttribute($key, $value);
}
}
Then override $columns
in your models:
protected $columns = [
'first_name' => 'first',
'last_name' => 'last',
'mobile_phone' => 'phone',
'home_phone' => 'otherPhone',
];
Laravel 5 Naming Conventions
Tables: posts, comments, comment_post
Columns: id, post_id, comment_id
Controllers: PhotoController, TaskController
Models: Page, Card, Post
For more details check out my Laravel naming conventions table.
Database Table custom naming convention Laravel Eloquent
View names in Laravel work like a path. The .
gets converted to a /
That means, your view resolves to the file app/views/completion/lsz.blade.php
(or app/views/completion/lsz.php
without Blade)
So you either have to change the name of your directory in the views folder to "completion" or change the view make command to:
View::make('lsz.lsz', ['completion' => $lsz]);
Related Topics
Mysqli + Xdebug Breakpoint After Closing Statement Result in Many Warnings
How to Add Namespace to an Attribute with PHP's Simplexml
3Rd Party Dependency Conflict in Developing Wordpress Plugin
Keeping Url Parameters During Pagination
How to Get Data in Function Extend Controller
Composer Install Error - Requires Ext_Curl When It's Actually Enabled
Parallel Processing in PHP - How to Do It
PHP Prepend Associative Array with Literal Keys
Laravel - Database, Table and Column Naming Conventions
File_Exists() Returns False Even If File Exist (Remote Url)
How to Join Two Tables with Ssp.Class.Php
How to Obtain a Nested HTML List from Object's Array Recordset
How to Remove 'Index.Php' from Url in Codeigniter
Continuing Overflowed Text in a Different Div