Show Menu
Cheatography

Laravel Cheat Sheet (DRAFT) by

Laravel

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

Artisan

php artisan routes
php artisan make:c­ont­roller UserCo­­nt­r­oller
php artisan make:model User
php artisan make:model User -m
// Migrations
php artisan migrat­­e:make create­­_u­s­e­rs­­_table
php artisan migrat­­e:make create­­_u­s­e­rs­­_table --crea­­te­=­users
php artisan migrate
php artisan migrat­­e:­r­o­llback
php artisan migrat­­e:­r­e­fresh
// Seed
php artisan genera­­te­:seed posts
php artisan db:seed
php artisan migrat­­e:­r­e­fresh --seed
php artisan db:seed --clas­­s=­P­o­st­­sTa­­bl­e­S­eeder
// Generators
php artisan genera­­te­:­r­es­­ource post --fiel­­ds­=­"­­tit­­le­:­s­tring, body:t­­ex­t­"
php artisan genera­­te­:­pivot categories users

Model

Relations
class Post extends Eloquent {
...
public function user(){
return $this-­­>b­e­l­on­­gsT­­o(­'­U­ser');
// hasMany
// hasOne
// belong­­sT­oMany
}
...
}

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

$posts = Post::­­fi­n­d(2);

$posts = Post::­­wh­e­r­e(­­'ti­­tle', 'LIKE', '%et%'­­)-­>­g­et();

$posts = Post::­­wh­e­r­e(­­'ti­­tle', 'LIKE', '%et%'­­)-­>­t­ak­­e(1­­)-­>­s­ki­­p(1­­)-­>­g­et();



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

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


// UPDATE
$post = Post::­­fi­n­d(1);
$post-­­>t­i­t­le­­('u­­pdated title');
$post-­­>s­a­ve();


// DELETE
$post = Post::­­fi­n­d(1);
$post-­­>d­e­l­ete();
$post-­>fo­rce­Del­ete(1); If using SoftDe­letes columns
Post::­des­tro­y(1);

Általános info

A laravel model alapér­tel­mezett vissza­térési értéke json.

author­(be­lon­gsTo) book (hasMany)
id id
name title
author_id

Laravel telepítése

1. Virtual box feltel­epítése
2. Vagrant telepítése
3. git, composer telepítése

--git paranc­ske­zelőből
--c:/u­ser­s/f­elh­asz­nál­óneved
4. vagrant box add larave­l/h­ome­stead parancs kiadása git-ből
5. git clone https:­//g­ith­ub.c­om­/la­rav­el/­hom­est­ead.git Homestead
6. cd Homestead
7. bash init.sh**


8. ssh beállítása

ssh-keygen -t rsa -C "­ern­ax" - a későbbi kapcso­lód­áshoz jegyezzük meg a stringet

Homestead beállí­tás­ai:­(ho­mes­tea­d.yaml)

---
ip: "­192.16­8.1­0.1­0"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh­/id­_rs­a.pub
keys:
- ~/.ssh­/id_rsa
folders:

- map: E:/Pro­jects
// a host gépen a projectek mappájának elérése
to: /home/­vag­ran­t/P­rojects
// a virtuális gépen a mappák elérése

sites:

- map: larave­l.dev
// a project elérése, mint a xamppnál a localhost vagy 127.0.0.1/
to: /home/­vag­ran­t/P­roj­ect­s/l­ara­vel­/public
//a virtuális gép url elérése
- map: homest­ead.dev

to: /home/­vag­ran­t/P­roj­ect­s/h­ome­ste­ad/­public


databases:
//adat­báz­isok, amiket a projec­tekhez használunk
- homestead
- laravel
-


Host file szerke­sztése:

A homest­ead.ya­ml-ben az ip-t kell hozzár­endelni a project nevéhez

pl.: 192.16­8.10.10 larave­l.dev


Ahhoz, hogy belépjünk a virtuális gépbe először bekell lépnünk a Homestead mappába (cd Homestead) , majd a vagrant ssh parancsot kell kiadnunk.

A következő parancssal létreh­ozzuk a projec­tünket:

composer create­-pr­oject --pref­er-dist larave­l/l­aravel ernax //azért ernax a project neve mert a homest­ead.ya­ml-ben ernax.d­ev­-ként vettünk fel egy host nevet, azaz annak a hostnévnek a nevét kell megadni amelyikhez a projectet szeretnénk létreh­ozni.




mysql workbench csatla­kozása a homest­ead-hez

ip 192.16­8.10.10
port: 3306
user homestead
pw secret



homstead futtatása, leállítása

vagrant up / vagrant halt / vagrant ssh(ctrl+d kilépés)
 

Migration Columns methods reference

$table­->b­igI­ncr­eme­nts­('id');
Increm­enting ID using a big integer equivalent
$table­->b­igI­nte­ger­('v­otes');
BIGINT equivalent to the table

Migration Columns methods reference (copy)

$table­->b­igI­ncr­eme­nts­('id');
Increm­enting ID using a big integer equivalent
$table­->b­igI­nte­ger­('v­otes');
BIGINT equivalent to the table
$table­->b­ina­ry(­'da­ta');
BLOB equivalent to the table
$table­->b­ool­ean­('c­onf­irm­ed');
BOOLEAN equivalent to the table
$table­->c­har­('n­ame', 4);
CHAR equivalent with the length
$table­->d­ate­('c­rea­ted­_at')
DATE equivalent to the table
$table­->d­ate­Tim­e('­cre­ate­d_at');
DATETIME equivalent to the table
$table­->d­eci­mal­('a­mount', 5, 2);
DECIMAL equivalent with a precision and scale
$table­->d­oub­le(­'co­lumn', 15, 8);
DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point
$table­->e­num­('c­hoi­ces', ['foo', 'bar']);
'bar']); ENUM equivalent to the table
$table­->f­loa­t('­amo­unt');
FLOAT equivalent to the table
$table­->i­ncr­eme­nts­('id');
Increm­enting ID to the table (primary key)
$table­->i­nte­ger­('v­otes');
INTEGER equivalent to the table
$table­->j­son­('o­pti­ons');
JSON equivalent to the table
$table­->l­ong­Tex­t('­des­cri­pti­on');
LONGTEXT equivalent to the table
$table­->m­edi­umI­nte­ger­('n­umb­ers');
MEDIUMINT equivalent to the table
$table­>me­diu­mTe­xt(­'de­scr­ipt­ion');
MEDIUMTEXT equivalent to the table
$table­->n­ull­abl­eTi­mes­tam­ps();
Same as timest­amps(), except the fact that this allows NULLs
$table­->s­mal­lIn­teg­er(­'vo­tes');
SMALLINT equivalent to the table
$table­->t­iny­Int­ege­r('­num­bers');
TINYINT equivalent to the table
$table­->s­tri­ng(­'em­ail');
VARCHAR equivalent column
$table­->s­tri­ng(­'name', 100);
VARCHAR equivalent with the length of a string
$table­->t­ext­('d­esc­rip­tion');
TEXT equivalent to the table

Model relation

author­(be­lon­gsTo)
book (hasMany)
id
id
name
title
 
author_id
hasMany - sok van
belongsTo - hozzá tartozik
 

Migration other methods

$table­->t­ime­sta­mps();
$table­->r­eme­mbe­rTo­ken();
$table­->s­oft­Del­etes();
$table­->i­nte­ger­('m­y_c­olu­mn'­)->­uns­ign­ed();
$table­->s­tri­ng(­'my­_co­lum­n')­->d­efa­ult­('m­y_d­efa­ult­_va­lue');
$table­->s­tri­ng(­'my­_co­lum­n')­->n­ull­able();
Updating tables and columns
Schema­::t­abl­e('­books', functi­on(­Blu­eprint $table) {
$table­->s­tri­ng(­'ti­tle', 250)->­cha­nge();
});

$table­->d­rop­Pri­mar­y('­aut­hor­s_i­d_p­rim­ary'); $table­->d­rop­Uni­que­('a­uth­ors­_em­ail­_un­ique'); $table­->d­rop­Ind­ex(­'bo­oks­_ti­tle­_in­dex'); $table­->d­rop­For­eig­n('­boo­ks_­aut­hor­_id­_fo­rei­gn');

Route

Simple route
Route:­:ge­t('­/'f­unc­tion(){
return view('­view');
});
Route:­:ge­t('­/',­'Ho­meC­ont­rol­ler­@in­dex');
Route:­:ge­t('­/ar­tic­le/­{id­}',­'Ar­tic­leC­ont­rol­ler­@in­dex');
Route:­:ge­t('­/ar­tic­le/­{id­?}'­,'A­rti­cle­Con­tro­lle­r@i­nde­x');//? nullable parameter

Route:­­:g­e­t­('­­pos­­ts­/­{­id­­}',­­fu­n­c­ti­­on(­­$id){
return View::­­ma­k­e­('­­pos­­t.s­­i­ng­­le'­­)-­>­w­it­­h('id', $id);
});
Groups
Route:­­:g­r­o­up­­(ar­­ra­y­(­'b­­efore' => 'auth'), functi­­on(){
// Route:: ...
// Route:: ...
});

Prefixs
Route:­­:g­r­o­up­­(ar­­ra­y­(­'p­­refix' => 'admin'), functi­­on(){
// Route:: ...
// Route:: ...
});

Blade

Blade layout
<!-- HTML -->
@inclu­­de­(­'­pa­­rti­­al­s.m­e­nu');
[...]
@yield­­('­c­o­nt­­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

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