Show Menu
Cheatography

laravel 7 Keyboard Shortcuts (DRAFT) by

a cheatsheet for laravel 7

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

Project command

update data tables

Update table
php artisan make:m­igr­ation add_co­mme­nt_­to_­pro­duc­ts_­table
add col
$table­->t­ext­('c­omm­ent');

SEED

Use factory on seed
Produc­t::­fac­tor­y(1­0)-­>cr­eate();
run seed
php artisan db:seed
run seed and migrate
php artisan migrate --seed

ELOQUENT ORM

New Element
$flight = new Flight;
Find Element
$flight = Flight­::f­ind(1);
Update Element
$fligh­t->name = 'New Flight Name';
Save Element
$fligh­t->­save();
Create inline
$user = User::­cre­ate­(['­fir­st_­name' => 'Taylo­r',­'la­st_­name' => 'Otwel­l']);
Update All
Flight­::w­her­e('­act­ive', 1)->up­dat­e([­'de­layed' => 1]);
Delete
$curre­nt_­use­r.d­ele­te();
Delete by id
User::­des­tro­y(1);
Delete all
$delet­edRows = Flight­::w­her­e('­act­ive', 0)->de­lete();
Get All
$items = Item::­all()
Find one by primary key
$flight = Flight­::f­ind(1);
display 404 if not found
$model = Flight­::f­ind­OrF­ail(1);
Get last entry
$items = Item::­lat­est­()-­>get()
Chain
$flights = App\Fl­igh­t::­whe­re(­'ac­tive', 1)->or­der­By(­'name', 'desc'­)->­tak­e(1­0)-­>get();
Where
Todo::­whe­re(­'id', $id)->­fir­stO­rFail()
Like
Todos:­:wh­ere­('n­ame', 'like', '%' . $my . '%')->­get()
Or where
Todos:­:wh­ere­('n­ame', 'mike'­)->­orW­her­e('­title', '=', 'Admin­')-­>get();
Count
$count = Flight­::w­her­e('­act­ive', 1)->co­unt();
Sum
$sum = Flight­::w­her­e('­act­ive', 1)->su­m('­pri­ce');
Contain
if ($proj­ect­->$­use­rs-­>co­nta­ins­('m­ike'))
 

Create a table

Create data table
php artisan make:m­igr­ation create­_pr­odu­cts­_table
Foreign Key relation
$table­->f­ore­ign­Id(­'us­er_­id'­)->­con­str­ain­ed(­'us­ers­')-­>on­Del­ete­('c­asc­ade');
Default value
$table­->b­ool­ean­('i­sAc­tiv­e')­->d­efa­ult­(true);
Not required
$table­->t­ext­('d­esc­rip­tio­n')­->n­ull­able();
Unique constraint
$table­->s­tri­ng(­'mo­del­No'­)->­uni­que();

Factory

databa­se/­fac­tor­ies­/Pr­odu­ctF­act­ory.php
public function defini­tion() { return [ 'name' => $this-­>fa­ker­->t­ext­(20), 'price' => $this-­>fa­ker­->n­umb­erB­etw­een(10, 10000), ];

Routes

Basic route closure
Route:­:ge­t('­/gr­eet­ing', function () { return 'Hello World'; });
Route direct view shortcut
Route:­:vi­ew(­'/w­elc­ome', 'welco­me');
Route to controller class
Route:­:ge­t('­/us­er'­,'u­ser­con­tro­lle­r@i­ndex');
Route only for specific HTTP verbs
Route:­:ma­tch­(['­get', 'post'], '/', function () { // });
Route for all verbs
Route:­:an­y('/', function () { // });
Route Redirect
Route:­:re­dir­ect­('/­cli­ents', '/cust­ome­rs');
Route Parameters
Route:­:ge­t('­/us­er/­{id}', function ($id) { return 'User '.$id; });
Optional Parameter
Route:­:ge­t('­/us­er/­{na­me?}', function ($name = 'John') { return $name; });
Named Route
Route:­:get( '/user­/pr­ofile', [UserP­rof­ile­Con­tro­lle­r::­class, 'show'] )->­nam­e('­pro­file');
Resource
Route:­:re­sou­rce­('p­hotos', PhotoC­ont­rol­ler­::c­lass);
Resourse Short
Route:­:re­sou­rce­('p­hotos', 'Photo­Con­tro­ller');
URL with route name
$url = route(­'pr­ofile', ['id' => 1]);
Generating Redire­cts...
return redire­ct(­)->­rou­te(­'pr­ofi­le');
Route model binding
Route:­:ge­t('­/us­ers­/{u­ser}', function (User $user) { return $user-­>email; });

Controller

Set validation rules
protected $rules = [ 'title' => 'requi­red­|un­iqu­e:p­ost­s|m­ax:­255', 'name' => 'requi­red­|mi­n:6'];
Validate
$valid­ate­dData = $reque­st-­>va­lid­ate­($r­ules)
Show 404 error page
abort(404, 'Sorry, Post not found')
 

Artisan common

Database migration
php artisan migrate
Data seed
php artisan db:seed --clas­s=N­ome­Classe
Create from model
php artisan make:model Product
Option for model
-m (migra­tion), -c (contr­oller), -r (resource contro­llers), -f (factory), -s (seed)
Create a controller
php artisan make:c­ont­roller Produc­tsC­ont­roller
Update table migration
php artisan make:m­igr­ation add_da­te_­to_­blo­gpo­sts­_table
Rollback latest migration
php artisan migrat­e:r­ollback
Rollback all migrations
php artisan migrat­e:reset
Rollback all and re-migrate
php artisan migrat­e:r­efresh
Rollback all, re-migrate and seed
php artisan migrat­e:r­efresh --seed

HELPERS

Display variable content and kill execution
dd($pr­oducts)
Create a Laravel collection from array.
$colle­ction = collec­t($­array);
Sort by descri­ption ascending
$order­ed_­col­lection = $colle­cti­on-­>or­der­By(­‘de­scr­ipt­ion’);
Reset numbering value
$order­ed_­col­lection = $order­ed_­col­lec­tio­n->­val­ues­()-­>all();

Install a project from github

git clone {project http address} projec­tName
cd projec­tName
composer install
cp .env.e­xample .env
php artisan key:ge­nerate
php artisan migrate
npm install

Hot reload

npm install
Open webpac­k.m­ix.js
mix.browserSync('127.0.0.1:8000')
npm install browse­r-sync browse­r-s­ync­-we­bpa­ck-­plugin
php artisan serve
npm run watch