Important commands related to Laravel 5 Tutorials, specifically 5.1 Related docs
Tinkering around with PHP
php artisan tinkerRelated settings in .env file
-
Create new migration
php artisan make:migration create_articles_table --create="articles" -
Push migration
php artisan migrate
-
Undo migration
php artisan migrate:rollback
-
Making new migration to certain table
php artisan make:migration add_excerpt_to_articles_table --table="articles"
Created model will be extended from a default Model.php class, which includes a bunch of default methods, review when necessary.
Related docs
-
Create a
Modelboilerplate classphp artisan make:model Article
-
Plugin for timestamps
Carbon\Carbon::now();
-
Creating
Model-
Basic
$article = new App\Article;
-
Pre-added values
$article = App\Article::create(['title' => 'New Article', 'body' = > 'New body']);
Remember Add fillable property inside related model class to avoid
MassAssignment Exceptionand security breaches. In this case, inside theApp/Article.phpfileprotected $fillable = ['title', 'body'];
-
-
Saving
Model$article->save();
-
Updating
Model$article->update(['body' => 'Updated']);
-
Find a
Modelusingid$article = App\Article::find(2);
-
Using scope to automatically set things by laravel. Define function in the following format
set{AttributeName}Attribute({$data}). With the code body below$this->attributes['password'] = mcrypt($password);
-
Define relationship in model class, with a proper function name and a code body below
return $this->hasMany('App\Article');
-
Using relationship for
Userto save anArticleof it's own.\Auth::user()->articles()->save($article);
Usually would need to get the
User's id, but since it's already defined in the relationship, process would be automated -
Many to many relationship between
ArticleandTagUsebelongsToManyas the relationship type in the currentModelpublic function {otherModel}s() { return $this->belongsToMany('App\{otherModel}'); }
Add code below into created migration file
/** * Format : * {singularNameTableOne}_{singularNameTableTwo} * Ordered alphabetically */ Schema::table('article_tag', function (Blueprint $table) { $table->foreign('article_id') ->references('id') ->on('articles') ->onDelete('cascade'); $table->foreign('tag_id') ->references('id') ->on('tags') ->onDelete('cascade'); });
Hook them up using
$article->tags()->attach({otherModelId})
and vice versa
$tag->articles()->attach({otherModelId})
Disable debug mode when in production. Change code below in .env
APP_DEBUG=falseUse Illuminate/Html package using Composer
composer require illuminate/htmlTag open {!! Form::open() !!}, tag close {!! Form::close() !!}, CSRF tag added automatically using this tag.
Add code below into config\app.php file
-
in
providersIlluminate\Html\HtmlServiceProvider::class,
-
in
aliases'Form' => Illuminate\Html\FormFacade::class, 'Html' => Illuminate\Html\HtmlFacade::class,
Illuminate handles all form securities, NO SQL injection.
-
Do validation in server side using
Requestclassphp artisan make:request ArticleRequest
-
Insert code below into the
rules()functionreturn [ 'title' => 'required|min:3', 'body' => 'required', 'published_at' => 'required|date', ];
-
Or use a simpler one just for little validation
$this->validate($request, ['title' => 'required'])
-
Add middleware
php artisan make:middleware RedirectIfNotAManager
-
Change code in
RouteServiceProvider.php$router->model('articles', 'App\Article');
-
Remove any
$idas parameter from function inArticlesController.php, change intopublic function show(Article $article) { // remove the code in body : $article = Article::findOrFail($id); }
-
For complicated logic, change default in number 1 into
$router->bind('articles', function($id) { return \App\Article::published()->findOrFail($id); });
-
Create new service provider
php artisan make:provider ViewComposerServiceProvider
-
Add the new provider into
config/app.phpfile in theprovidersApp\Providers\ViewComposerServiceProvider::class,
-
Insert related code
view()->composer('partials.nav', function($view) { $view->with('latest', Article::latest()->first()); });