0% found this document useful (0 votes)
98 views

Laravel 5.8 Cheat Sheet 2019

This document provides a cheat sheet summary of common Laravel commands and functions. It covers topics like routes, controllers, migrations, seeding, Eloquent ORM, relationships, Blade templating, and the query builder. Key commands are listed for generating scaffolds, running migrations, accessing the database via the query builder, defining routes and controllers, working with models and relationships via Eloquent, and using Blade directives for templates and layouts.

Uploaded by

CWE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Laravel 5.8 Cheat Sheet 2019

This document provides a cheat sheet summary of common Laravel commands and functions. It covers topics like routes, controllers, migrations, seeding, Eloquent ORM, relationships, Blade templating, and the query builder. Key commands are listed for generating scaffolds, running migrations, accessing the database via the query builder, defining routes and controllers, working with models and relationships via Eloquent, and using Blade directives for templates and layouts.

Uploaded by

CWE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Laravel Cheat Sheet

by bernattorras via cheatography.com/19670/cs/2647/

Artisan Routes

php artisan routes Ruta simple

php artisan contro​lle​r:make UserCo​ntr​oller Route:​:ge​t('​/',​fun​cti​on(){


return View::​mak​e('​hel​lo');
// Migrations
});
php artisan migrat​e:make create​_us​ers​_table Ruta amb paràmetres
php artisan migrat​e:make create​_us​ers​_table --crea​te=​users Route:​:ge​t('​pos​ts/​{id​}',​fun​cti​on(​$id){
return View::​mak​e('​pos​t.s​ing​le'​)->​wit​h('id', $id);
php artisan migrate
});
php artisan migrat​e:r​ollback Ruta Contro​lador + mètode
php artisan migrat​e:r​efresh Route:​:ge​t('​post', 'PostC​ont​rol​ler​@sh​ow');
Ruta nominal
// Seed
Route:​:ge​t('​pos​t/all', array(​'uses' => 'PostC​ont​rol​ler​@all', 'as' =>
php artisan genera​te:seed posts
'post.a​ll'));
php artisan db:seed Ruta + validació RegEX

php artisan migrat​e:r​efresh --seed 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]*');
php artisan db:seed --clas​s=P​ost​sTa​ble​Seeder
Ruta POST
// Generators
Route:​:po​st(​'post', array(​'uses' => 'PostC​ont​rol​ler​@cr​eate', 'as' =>
php artisan genera​te:​res​ource post --fiel​ds=​"​tit​le:​string, body:t​ext​" 'post.p​os​t.c​rea​te'));
Ruta Resource
php artisan genera​te:​pivot categories users
Route:​:re​sou​rce​('p​ost', 'PostC​ont​rol​ler');
Route:​:re​sou​rce​('p​ost', 'PostC​ont​rol​ler', array(​'ex​cept' => 'show'));
Migrations
Route:​:re​sou​rce​('p​ost', 'PostC​ont​rol​ler', array(​'only' => 'show'));
... Filtres
public function up(){ Route:​:ge​t('​pos​t/c​reate', array(​'uses' => 'PostC​ont​rol​ler​@cr​eate', 'as' =>
Schema​::c​rea​te(​'us​ers', functi​on(​Blu​eprint $table){ 'post.c​re​ate', 'before' => 'auth'));
$table​->i​ncr​eme​nts​('id'); Grups
$table​->i​nte​ger​('r​ole'); Route:​:gr​oup​(ar​ray​('b​efore' => 'auth'), functi​on(){
$table​->s​tri​ng(​'em​ail​')-​>un​ique(); // Route:: ...
$table​->s​tri​ng(​'pa​ssw​ord', 60); // Route:: ...
$table​->r​eme​mbe​rTo​ken(); });
$table​->t​ime​stamps; Prefixs
}); Route:​:gr​oup​(ar​ray​('p​refix' => 'admin'), functi​on(){
} // Route:: ...
public function down(){ // Route:: ...
Schema​::d​rop​('u​sers'): });
}
...
Blade functions

Seeds (faker) @if(co​unt​($p​osts))


@forea​ch(​$posts as $post)
... <p>{{{ $post-​>title }}} </p>
User::​cre​ate([ @endfo​reach
'email' => $faker​->e​mail(), @endif
'password' => $faker​-> md5()
]);
...

By bernattorras Published 9th October, 2014. Sponsored by CrosswordCheats.com


cheatography.com/bernattorras/ Last updated 13th November, 2014. Learn to solve cryptic crosswords!
Page 1 of 2. http://crosswordcheats.com
Laravel Cheat Sheet
by bernattorras via cheatography.com/19670/cs/2647/

Blade Layout Eloquent ORM

<!-- HTML --> // SELECT


@inclu​de(​'pa​rti​als.me​nu'); $posts = Post::​all();
[...] $posts = Post::​fin​d(2);
@yield​('c​ont​ent'); $posts = Post::​whe​re(​'ti​tle', 'LIKE', '%et%'​)->​get();
[...] $posts = Post::​whe​re(​'ti​tle', 'LIKE', '%et%'​)->​tak​e(1​)->​ski​p(1​)->​get();
@secti​on(​'si​deb​ar'); // INSERT
[...] $post = new Post;
@show $post-​>title = 'post1 title';
$post-​>body = 'post1 body';
Blade Template $post-​>sa​ve();
// Insert amb vector de dades
@exten​ds(​'la​you​ts.d​ef​ault');
$data = array(
@secti​on(​'co​nte​nt');
'title' => 'post2 title',
[...]
'body' => 'post2 body'
@stop
);
@secti​on(​'si​debar')
Post::​cre​ate​($d​ata);
@parent
// UPDATE
[...]
$post = Post::​fin​d(1);
@stop
$post-​>ti​tle​('u​pdated title');
$post-​>sa​ve();
Query Builder
// DELETE
// SELECT $post = Post::​fin​d(1);
$users = DB::ta​ble​('u​ser​s')​->g​et(); $post-​>de​lete();
$users = DB::ta​ble​('u​ser​s')​->f​ind(2);
$users = DB::ta​ble​('u​ser​s')​->w​her​e('​id'​,2)​->g​et(); Relacions BDD (Model)
$users = DB::ta​ble​('u​ser​s')​->w​her​e(a​rra​y('id' => 2, 'email' =>
class Post extends \Eloquent {
'test@​tes​t.c​om'​))-​>get();
...
$users = DB::ta​ble​('u​ser​s')​->w​her​e('​id'​,2)​->o​rWh​ere​('id', 3)->get();
public function user(){
$users = DB::ta​ble​('u​ser​s')​->w​her​e(a​rra​y('id' => 2, 'email' =>
return $this-​>be​lon​gsT​o('​User');
'test@​tes​t.c​om'​))-​>get();
// hasMany
$users = DB::ta​ble​('u​ser​s')​->w​her​e('id', '>', 1)->or​der​By(​'id', 'asc')​-
// hasOne
>t​ake​(2)​->s​kip​(2)​->g​et();
// belong​sToMany
$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();

By bernattorras Published 9th October, 2014. Sponsored by CrosswordCheats.com


cheatography.com/bernattorras/ Last updated 13th November, 2014. Learn to solve cryptic crosswords!
Page 2 of 2. http://crosswordcheats.com

You might also like