Laravel: Difference between revisions

From Adam Colton
Jump to navigation Jump to search
No edit summary
No edit summary
Line 36: Line 36:
== Eloquent ==
== Eloquent ==
The $fillable property limits which properties can be assigned.
The $fillable property limits which properties can be assigned.
== 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.
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');
  }

Revision as of 23:54, 21 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

Video

 php artisan make:auth

Creates a bunch of views, routes and adds a controller.

Email

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.

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. 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');
 }