Eloquent Models : ORM
Security
Use $fillable
property to protect against mass assignment. Only fields that are specifiled here are now allowed to access.
protected $fillable = [ 'name', ];
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); }
Usage:
Post::publishedBefore(Carbon::now())
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.
Usage:
// 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.
Relationship
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
Controllers
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 |
.
Routes
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:
@yield('title') @yield('content')@yield('footer')
To extend this master template and create a view:
@extends('layouts.master') @section('title', 'First Page') @section('content')This is my body content.
@stop @section('footer')This is appended to the master footer.
@stop
.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.
Leave a Reply