Laravel: Difference between revisions
Adamcolton (talk | contribs) No edit summary |
Adamcolton (talk | contribs) |
||
| Line 136: | Line 136: | ||
== Migrations == | == Migrations == | ||
=== Artisan === | |||
php artisan make:migration create_NAME_table --create=NAME | |||
php artisan make:migration add_SOMETHING_to_NAME_table --table=NAME | |||
=== Types === | |||
From the [https://laravel.com/docs/5.2/migrations#creating-columns documentation]: | From the [https://laravel.com/docs/5.2/migrations#creating-columns documentation]: | ||
* $table->increments('id'); | * $table->increments('id'); | ||
* $table->integer('role')->unsigned; | * $table->integer('role')->unsigned; | ||
* $table->string('name'); | * $table->string('name'); | ||
== Research These == | == Research These == | ||
* https://github.com/laracasts/Laravel-5-Generators-Extended | * https://github.com/laracasts/Laravel-5-Generators-Extended | ||
Revision as of 15:51, 24 January 2016
Implicit Route Binding
From this video
Route::get('users/{user}', function(App\User $user){
return $user;
});
If the arg name matches the token, it will magic that value into place. With the type hinting, it will find a user with the primary key given in the url.
To use something other than the primary key, you have to add a route binding in RouteServiceProvider.
Auth
php artisan make:auth
Creates a bunch of views, routes and adds a controller.
Easiest is to use 'sendmail'. Make sure to set 'from' in config/mail.php.
Homestead
I'll need to try setting up a homestead instance on AWS with Vagrant. Could be useful to know for other projects too.
Illuminate/html
composer require illuminate/html
Then in config/app.php, add 'Illuminate\Html\HtmlServiceProvider' to the providers array and 'Form' => 'Illuminate\Html\FormFacade' to the aliases array.
This seems to be the most up-to-date documentation.
Request
$input = Request::all();
$foo = Request::get('foo');
Gets all inputs from get and post.
Good shorthand
$input = Request::all(); SomeModel::create($input);
Eloquent
The $fillable property limits which properties can be assigned. Special protected properties:
- $fillable: which fields can be assigned
- $dates: an array of fields that will be treated as Carbon dates
Accessors and Mutators
Accessors modify data coming from the database before it's inserted in the model. Mutators modify it before inserting.
A mutator method must have a name following the pattern setFieldNameAttribute
Query Scope
Name is scopeName, takes a query as an arg. Useful for attaching repeated query logic to the model.
Relationships
One-to-many (author to article)
//in the 'one' (author)
public function articles(){
return $this->hasMany('App\Articles');
}
//in the 'many' (article)
public function author(){
return $this->belongsTo('App\user');
}
Many-to-many: create the pivot table, name is singular underscore singular, alphabetic order. Then in each
public function article(){ //in tag
return $this->belongsToMany('article');
}
The many to many can handle insert/delete diffs with ->sync().
Form Requests
php artisan make:requestion NameOfRequest
Starts with the methods authorize and rules.
Rules returns an array of fields and their validation logic.
Validators
- required
- min:n
- date
Authorize returns a bool.
If validation fails, it's redirected back to the previous page with $errors populated with proper errors.
Form requests are implemented with type hinting on the controller.
The controller method looks like this:
public function someMethod(SomeRequest $request){
Model::create(request->all());
return redirect('someplace');
}
Direct Validation
Rather than creating a form request, we can do this
public function someMethod(Request $request){
$this->validate($request, ['title' => 'required|min:3']);
Model::create(request->all());
return redirect('someplace');
}
Routes
php artisan route:list
Shows a table of the routes.
Route Resource
Implements restful crud.
Route Model Binding
Done through RouteServicesProvider.php in the boot method, add a line like:
$router->model('article', 'App\Article');
now any instance of {article} in a route will be used to look up an Article by it's primary id and that article will be passed into the controller method.
Middleware
All methods have the same args $request and $next and the continue by calling $next($request);
In http/Kernal.php, every piece of middleware in $middleware is run against every request.
Middleware can be attached in a controllers constructor:
public function __construct(){
$this->middleware('auth');
}
We can also limit what methods it applies to
public function __construct(){
$this->middleware('auth', ['only'=>'create']);
}
At the route level:
Route::get('create', ['middleware' => 'auth', 'uses' => 'ArticleController@create']);
Sessions
Flash Messaging
\Session::flash('key','value') // set
Session::has('key'); // get
Anything set with flash will only persist for one request.
Migrations
Artisan
php artisan make:migration create_NAME_table --create=NAME php artisan make:migration add_SOMETHING_to_NAME_table --table=NAME
Types
From the documentation:
- $table->increments('id');
- $table->integer('role')->unsigned;
- $table->string('name');