Laravel Doc

Eloquent Models : ORM


Use $fillable property to protect against mass assignment. Only fields that are specifiled here are now allowed to access.

protected $fillable = [
Query Scope

Query scopes allow us to create a custom query if you find that query is frequently used.

public function scopePublishedBefore($query, $value)
    $query->where('published_at', '<=', $value);


Mutators / Accessors

Mutators are called when you attempt tp set the value on the model.
Accessors are called when you attempt tp get the value from the model.

// Mutators
public function setFirstNameAttribute($value)
    $this->attributes['first_name'] = strtolower($value);
// Accessors
public function getFirstNameAttribute($value)
    return ucfirst($value);

Make sure you have set/get in front and Attribute at the end.


Using Eloquent model relationship is a easier way to fetch related models. Simply like: $post->createdBy. Laravel is doing much of the job here apperantly. But, all we need to do is define a relationship when we create models. There are many types of relationship, but you will find yourself using One-to-One, One-to-Many, and Many-to-Many mostly.
Laravel Relationship Doc


In MVC pattern, Controllers contain some kind of code to respond to the request and process the data from Model to View, or vice versa.

Form Request

Form Request class can be used to validate complicated requests. Since it's a class, the class is reusable and organized. If you choose not to create a class but want to make a simple validation, you can use validate method within a controller:

$this->validate($request, ['title' => 'required|min:3']);

Here we are piping the validation using |.


Route::get('/', function () {
    return view('welcome');

Route::get('about', '[email protected]');

Route::resource('blogs', 'BlogsController');

Blade Templates

Blade is a template engine that Laravel uses. File extension is typically .blade.php.
Master template will probably contain whole html wrapper and can yield sections like:


To extend this master template and create a view:


@section('title', 'First Page')


This is my body content.

@stop @section('footer')

This is appended to the master footer.


.env file

.env file is used to setup and deploy your product. Database, application, and other settings can be configured here. Note the .env file is ignored in git if you see .gitignore.

Database Migration

Migration files are an easier way to create database schemas for the application. And also, whoever is involved in the development can easily create same schemas with just using:

$ php artisan migrate

For an already deployed database, we can create a migration file to modify the existing database. Also, make sure to specify what to do if we rollback the migration, eg. drop a column.
Think the Laravel migrations as version control for database schemas. On top of that, it is much easier to read.