Show Menu
Cheatography

Laravel 5 Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Artisan

php artisan serv //lance un serveur de test
php artisan down //Met l'appl­ication en mainte­nance
php artisan up //enleve l'appl­ication de la mainte­nance
php artisan env //affiche les variables d'envi­ron­ement
php artisan help //affiche l'aide de la commande
php artisan list //list les commandes
php artisan optimize //optimise l'appl­ication
php artisan clear-­com­piled //efface du cache les fichiers compilés
php artisan tinker // lance l'appl­ication en mode console

Make

php artisan make:c­ont­roller MonCon­troller
php artisan make:c­ommand MaCommande
php artisan make:event Moneve­nement
php artisan make:job MonJob
php artisan make:l­istener MonLis­tener
php artisan make:m­idd­leware MonMid­dleWare
php artisan make:m­igr­ation MaMigr­ation
php artisan make:model MonModel
php artisan make:p­rovider MonPro­vider
php artisan make:r­equest MaRequest
php artisan make:s­eeder MonSeeder

Blade Layout

//if condition
@if(condition)
@else
@endif

//loop
@foreach($foo as $bar)
@endforeach

//inclusion
// Resources/views/folder/page.blade.php
@include('folder.page')

//Template
@yield('section')

@extends('template')
@section('section')
@endsection

Eloquent

//Model en base de donnees
class Post extend Model{
  protected $primaryKey = "IDPost"
  protected $table = "post"
}

//Recuperation par cle primaire
Post::find(1);
//Recuperation par where
Post::where('titre', '=', 'valeur')->get();
//Faire un join
Post::join('table', 'post.idTable', 'table.IDTable')->get();

//Insertion en base
$post = new Post();
$post->titre = "hello";
$post->save();

//Update
$post = Post::find(1);
$post->titre = "modif"
$post->save();

//suppression
$post->delete();
 

Route

php artisan route:­cache //Met les routes en cache
php artisan route:­clear //Supprime le cache des routes
php artisan route:list //affiche la liste des routes

Route

//Route Simple
Route::get('/',function (){
    return view('hello');
});

//Route avec parametre
Route::get('/post/{id}', function($id){
    return view('post', ["id"=>$id]);
});

//Route avec controller et methode
Route::get('/add', 'MusiqueController@formAdd');

//Route nommé
Route::get('/add', 'MusiqueController@formAdd')->name('showAddForm');

//Route validation regex
Route::get('/post/{id}', "MusiqueController@detail")->where('id', '[0-9]+');

//Methode http
Route::get('/', "controller@example");
Route::post('/', "controller@example");
Route::put('/', "controller@example");
Route::patch('/', "controller@example");
Route::delete('/', "controller@example");

//groupe
Route::prefix('api', function (){
    //http:foo.com/api
    Route::get('/', function (){
        return view('api');
    });
    //http:foo.com/api/test
    Route::get('/test', function (){
        return view('api');
    });
});

//utilisation d'un middleware
//sur plusieurs routes
Route::middleware(['first', 'second'])->group(function () {
    Route::get('/', function () {
        // utilise first & second Middleware
    });

    Route::get('user/profile', function () {
        // utilise first & second Middleware
    });
});

Seeder

php artisan migrate --seed

Seeder

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //seed
    }
}

Migration

php artisan migrate
php artisan migrat­e:i­nstall
php artisan migrat­e:r­ollback
php artisan migrat­e:r­efresh
php artisan migrat­e:reset
php artisan migrat­e:s­tatus

Migration

class CreateMusiquesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('musiques', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('ordre');
            $table->string('titre',64);
            $table->string('description',256)->nullable();
            $table->string('url',256);
            $table->date('publication');
            $table->integer('categorie_id')->unsigned();
            $table->foreign('categorie_id')->references('id')->on('categories');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('musiques');
    }
}