Laravel Cheat Sheet: by Via

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Laravel Cheat Sheet

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

Artisan Seeds (faker)

php artisan routes ...

php artisan controller:make UserController User::create([


'email' => $faker->email(),
// Migrations
'password' => $faker-> md5()
php artisan migrate:make create_users_table ]);

php artisan migrate:make create_users_table --create=users ...

php artisan migrate


Routes
php artisan migrate:rollback
Ruta simple
php artisan migrate:refresh
Route::get('/',function(){
// Seed return View::make('hello');
php artisan generate:seed posts });
Ruta amb parmetres
php artisan db:seed
Route::get('posts/{id}',function($id){
php artisan migrate:refresh --seed
return View::make('post.single')->with('id', $id);
php artisan db:seed --class=PostsTableSeeder });

// Generators Ruta Controlador + mtode


Route::get('post', 'PostController@show');
php artisan generate:resource post --fields="title:string, body:text"
Ruta nominal
php artisan generate:pivot categories users Route::get('post/all', array('uses' => 'PostController@all', 'as' =>
'post.all'));
Migrations Ruta + validaci RegEX
Route:get('post/{id}', array('uses' => 'PostController@single', 'as' =>
...
'get.post.single'))->where('id', '[1-9][0-9]*');
public function up(){
Ruta POST
Schema::create('users', function(Blueprint $table){
Route::post('post', array('uses' => 'PostController@create', 'as' =>
$table->increments('id');
'post.post.create'));
$table->integer('role');
Ruta Resource
$table->string('email')->unique();
Route::resource('post', 'PostController');
$table->string('password', 60);
Route::resource('post', 'PostController', array('except' => 'show'));
$table->rememberToken();
Route::resource('post', 'PostController', array('only' => 'show'));
$table->timestamps;
Filtres
});
Route::get('post/create', array('uses' => 'PostController@create', 'as' =>
}
'post.create', 'before' => 'auth'));
public function down(){
Grups
Schema::drop('users'):
Route::group(array('before' => 'auth'), function(){
}
// Route:: ...
...
// Route:: ...

By bernattorras Published 9th October, 2014. Sponsored by Readability-Score.com


cheatography.com/bernattorras/ Last updated 13th November, 2014. Measure your website readability!
Page 1 of 3. https://readability-score.com
Laravel Cheat Sheet
by bernattorras via cheatography.com/19670/cs/2647/

Routes (cont) Query Builder

}); // SELECT
Prefixs $users = DB::table('users')->get();
Route::group(array('prefix' => 'admin'), function(){ $users = DB::table('users')->find(2);
// Route:: ... $users = DB::table('users')->where('id',2)->get();
// Route:: ... $users = DB::table('users')->where(array('id' => 2, 'email' =>
}); '[email protected]'))->get();
$users = DB::table('users')->where('id',2)->orWhere('id', 3)->get();
Blade functions $users = DB::table('users')->where(array('id' => 2, 'email' =>
'[email protected]'))->get();
@if(count($posts))
$users = DB::table('users')->where('id', '>', 1)->orderBy('id', 'asc')-
@foreach($posts as $post) >take(2)->skip(2)->get();
<p>{{{ $post->title }}} </p>
$users = DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')-
@endforeach >get();
@endif // Log
dd(DB::getQueryLog());
Blade Layout // INSERT
$data = array(
<!-- HTML -->
'email' => '[email protected]',
@include('partials.menu');
'password' => '123456'
[...]
);
@yield('content');
DB::table('users')->insert($data);
[...]
// UPDATE
@section('sidebar');
$data = array(
[...]
'email' => '[email protected]',
@show
'password' => 'abc'
);
Blade Template
DB::table('users')->where('email', $data['email'])->update($data);
@extends('layouts.default'); // DELETE
@section('content'); DB::table('users')->where('email', '[email protected]')->delete();
[...]
@stop Eloquent ORM
@section('sidebar')
// SELECT
@parent
$posts = Post::all();
[...]
$posts = Post::find(2);
@stop
$posts = Post::where('title', 'LIKE', '%et%')->get();
$posts = Post::where('title', 'LIKE', '%et%')->take(1)->skip(1)->get();
// INSERT
$post = new Post;
$post->title = 'post1 title';
$post->body = 'post1 body';

By bernattorras Published 9th October, 2014. Sponsored by Readability-Score.com


cheatography.com/bernattorras/ Last updated 13th November, 2014. Measure your website readability!
Page 2 of 3. https://readability-score.com
Laravel Cheat Sheet
by bernattorras via cheatography.com/19670/cs/2647/

Eloquent ORM (cont)

$post->save();
// Insert amb vector de dades
$data = array(
'title' => 'post2 title',
'body' => 'post2 body'
);
Post::create($data);
// UPDATE
$post = Post::find(1);
$post->title('updated title');
$post->save();
// DELETE
$post = Post::find(1);
$post->delete();

Relacions BDD (Model)

class Post extends \Eloquent {


...
public function user(){
return $this->belongsTo('User');
// hasMany
// hasOne
// belongsToMany
}
...
}

By bernattorras Published 9th October, 2014. Sponsored by Readability-Score.com


cheatography.com/bernattorras/ Last updated 13th November, 2014. Measure your website readability!
Page 3 of 3. https://readability-score.com

You might also like