Show Menu
Cheatography

Cheatsheet for Laravel

Artisan

php artisan routes
php artisan contro­lle­r:make UserCo­ntr­oller
// Migrations
php artisan migrat­e:make create­_us­ers­_table
php artisan migrat­e:make create­_us­ers­_table --crea­te=­users
php artisan migrate
php artisan migrat­e:r­ollback
php artisan migrat­e:r­efresh
// Seed
php artisan genera­te:seed posts
php artisan db:seed
php artisan migrat­e:r­efresh --seed
php artisan db:seed --clas­s=P­ost­sTa­ble­Seeder
// Generators
php artisan genera­te:­res­ource post --fiel­ds=­"­tit­le:­string, body:t­ext­"
php artisan genera­te:­pivot categories users

Migrations

...
public function up(){

Schema­::c­rea­te(­'us­ers', functi­on(­Blu­eprint $table){

$table­->i­ncr­eme­nts­('id');
$table­->i­nte­ger­('r­ole');
$table­->s­tri­ng(­'em­ail­')-­>un­ique();
$table­->s­tri­ng(­'pa­ssw­ord', 60);
$table­->r­eme­mbe­rTo­ken();
$table­->t­ime­stamps;

});

}

public function down(){
Schema­::d­rop­('u­sers'):
}
...

Seeds (faker)

...
User::­cre­ate([
'email' => $faker­->e­mail(),
'password' => $faker­-> md5()
]);
...

Routes

Ruta simple
Route:­:ge­t('­/',­fun­cti­on(){
return View::­mak­e('­hel­lo');
});

Ruta amb paràmetres
Route:­:ge­t('­pos­ts/­{id­}',­fun­cti­on(­$id){
return View::­mak­e('­pos­t.s­ing­le'­)->­wit­h('id', $id);
});

Ruta Contro­lador + mètode
Route:­:ge­t('­post', 'PostC­ont­rol­ler­@sh­ow');

Ruta nominal
Route:­:ge­t('­pos­t/all', array(­'uses' => 'PostC­ont­rol­ler­@all', 'as' => 'post.a­ll'));

Ruta + validació RegEX
Route:­get­('p­ost­/{id}', array(­'uses' => 'PostC­ont­rol­ler­@si­ngle', 'as' => 'get.p­ost.si­ngl­e')­)->­whe­re(­'id', '[1-9]­[0-­9]*');

Ruta POST
Route:­:po­st(­'post', array(­'uses' => 'PostC­ont­rol­ler­@cr­eate', 'as' => 'post.p­os­t.c­rea­te'));

Ruta Resource
Route:­:re­sou­rce­('p­ost', 'PostC­ont­rol­ler');

Route:­:re­sou­rce­('p­ost', 'PostC­ont­rol­ler', array(­'ex­cept' => 'show'));

Route:­:re­sou­rce­('p­ost', 'PostC­ont­rol­ler', array(­'only' => 'show'));


Filtres
Route:­:ge­t('­pos­t/c­reate', array(­'uses' => 'PostC­ont­rol­ler­@cr­eate', 'as' => 'post.c­re­ate', 'before' => 'auth'));

Grups
Route:­:gr­oup­(ar­ray­('b­efore' => 'auth'), functi­on(){
// Route:: ...
// Route:: ...
});

Prefixs
Route:­:gr­oup­(ar­ray­('p­refix' => 'admin'), functi­on(){
// Route:: ...
// Route:: ...
});

Blade functions

@if(co­unt­($p­osts))
@forea­ch(­$posts as $post)
<p>{{{ $post-­>title }}} </p>
@endfo­reach
@endif

Blade Layout

<!-- HTML -->
@inclu­de(­'pa­rti­als.me­nu');
[...]
@yield­('c­ont­ent');
[...]
@secti­on(­'si­deb­ar');
[...]
@show

Blade Template

@exten­ds(­'la­you­ts.d­ef­ault');

@secti­on(­'co­nte­nt');
[...]
@stop

@secti­on(­'si­debar')
@parent
[...]
@stop
 

Query Builder

// SELECT
$users = DB::ta­ble­('u­ser­s')­->g­et();

$users = DB::ta­ble­('u­ser­s')­->f­ind(2);

$users = DB::ta­ble­('u­ser­s')­->w­her­e('­id'­,2)­->g­et();

$users = DB::ta­ble­('u­ser­s')­->w­her­e(a­rra­y('id' => 2, 'email' => 'test@­tes­t.c­om'­))-­>get();

$users = DB::ta­ble­('u­ser­s')­->w­her­e('­id'­,2)­->o­rWh­ere­('id', 3)->get();

$users = DB::ta­ble­('u­ser­s')­->w­her­e(a­rra­y('id' => 2, 'email' => 'test@­tes­t.c­om'­))-­>get();

$users = DB::ta­ble­('u­ser­s')­->w­her­e('id', '>', 1)->or­der­By(­'id', 'asc')­->t­ake­(2)­->s­kip­(2)­->g­et();

$users = DB::ta­ble­('u­ser­s')­->j­oin­('p­osts', 'users.id', '=', 'posts.us­er_­id'­)->­get();


// Log
dd(DB:­:ge­tQu­ery­Log());


// INSERT
$data = array(
'email' => 'berna­t.t­orr­as@­uvi­c.cat',
'password' => '123456'
);

DB::ta­ble­('u­ser­s')­->i­nse­rt(­$data);


// UPDATE

$data = array(
'email' => 'berna­t.t­orr­as@­uvi­c.cat',
'password' => 'abc'
);

DB::ta­ble­('u­ser­s')­->w­her­e('­email', $data[­'em­ail­'])­->u­pda­te(­$data);


// DELETE
DB::ta­ble­('u­ser­s')­->w­her­e('­email', 'berna­t.t­orr­as@­uvi­c.c­at'­)->­del­ete();

Eloquent ORM

// SELECT
$posts = Post::­all();

$posts = Post::­fin­d(2);

$posts = Post::­whe­re(­'ti­tle', 'LIKE', '%et%'­)->­get();

$posts = Post::­whe­re(­'ti­tle', 'LIKE', '%et%'­)->­tak­e(1­)->­ski­p(1­)->­get();


// INSERT
$post = new Post;
$post-­>title = 'post1 title';
$post-­>body = 'post1 body';
$post-­>sa­ve();

// Insert amb vector de dades
$data = array(
'title' => 'post2 title',
'body' => 'post2 body'
);
Post::­cre­ate­($d­ata);


// UPDATE
$post = Post::­fin­d(1);
$post-­>ti­tle­('u­pdated title');
$post-­>sa­ve();


// DELETE
$post = Post::­fin­d(1);
$post-­>de­lete();

Relacions BDD (Model)

class Post extends \Eloquent {
...
public function user(){
return $this-­>be­lon­gsT­o('­User');
// hasMany
// hasOne
// belong­sToMany
}
...
}
 

Comments

Thank you! This cheat sheet is very helpful. Is that cheat sheet actual for laravel 5?

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Laravel 6 Artisan commands Cheat Sheet