Skip to content

Latest commit

 

History

History
115 lines (94 loc) · 1.34 KB

04-03-19-models.md

File metadata and controls

115 lines (94 loc) · 1.34 KB
title isChild anchor
Models
true
models

Models {#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);
    }