5.2

Justas Maziliauskas

middleware groups

*"web" turi būti pirmasis masyve

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
    'admin' => [
            'web',
            'auth',
        ]
];

Text

Text

rate limiting

Route::get('/api/users', ['middleware' => 'throttle:60,1'], function () {
    //
}]);

Route::get('/api/users', ['middleware' => 'throttle'], function () {
    //
}]);

HTTP/1.1 200 OK
...
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59



HTTP/1.1 429 Too Many Requests
...
Retry-After: 60 (59, 58, 57, 56, 54....)
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 0

Text

Text

Text

A) This request succeeded (the status is 200)
B) You can try this route 60 times per minute
C) You have 59 requests left for this minute

multi-auth

// config/auth.php 
'guards' => [
    'user' => [
        'driver' => 'session',
        'provider' => 'user',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],
'providers' => [
    'user' => [
        'driver' => 'eloquent',
        'model' => 'App\User',
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => 'App\Admin',
    ],
],
$admin_credentials = [
    'email' =>  'admin@gmail.com',
    'password' =>  'complex_password',
];

$user_credentials = [
    'email' =>  'user@gmail.com',
    'password' =>  'stupid_password',
];

$auth = auth()->guard('admin');
$auth->attempt($admin_credentials);
// true
$auth->attempt($user_credentials); 
// false

$auth = auth()->guard('user');
$auth->attempt($admin_credentials); 
// false
$auth->attempt($user_credentials); 
// true

timestamps

$table->timestamp('foo')->nullable();

$table->timestamp('foo')->useCurrent();

$table->nullableTimestamps();
Starting with MySQL 5.7, 0000-00-00 00:00:00
is no longer considered a valid date, 
since strict mode is enabled 
by default. 

implicit route model binding

// before
Route::get('shoes/{id}', function ($id) {
    $shoe = Shoe::findOrFail($id);
    // Do stuff
});


// now
Route::get('shoes/{shoe}', function (App\Shoe $shoe) {
    // Do stuff
});

class Shoe extends Model 
{
    public function getRouteKeyName()
    {
        return 'slug';
    }
}

form array validation

<form>
    <label>Company Name</label>
    <input type="text" name="name">

    <h3>Employees</h3>
    <div class="add-employee">
        <label>Employee Name</label>
        <input type="text" name="employee[1][name]">
        <label>Employee Title</label>
        <input type="text" name="employee[1][title]">
    </div>
    <div class="add-employee">
        <label>Employee Name</label>
        <input type="text" name="employee[2][name]">
        <label>Employee Title</label>
        <input type="text" name="employee[2][title]">
    </div>
    <a href="#" class="js-create-new-add-employee-box">Add another employee</a>

    <input type="submit">
</form>
array(2) {
  ["name"]=>
  string(10) "Acme, Inc."
  ["employee"]=>
  array(2) {
    [1]=>
    array(2) {
      ["name"]=>
      string(10) "Joe Schmoe"
      ["title"]=>
      string(11) "Head Person"
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(18) "Conchita Albatross"
      ["title"]=>
      string(21) "Executive Head Person"
    }
  }
}
// CompaniesController.php
public function store(Request $request)
{
    $this->validate($request->all(), [
        'name' => 'required|string',
        'employee.*.name' => 'required|string',
        'employee.*.title' => 'string',
    ]);
    // Save, etc.
}

auth scaffolding

php artisan make:auth
vagrant@homestead:~/projects/test$ pa make:auth
Created View: /home/vagrant/projects/test/resources/views/auth/login.blade.php
Created View: /home/vagrant/projects/test/resources/views/auth/register.blade.php
Created View: /home/vagrant/projects/test/resources/views/auth/passwords/email.blade.php
Created View: /home/vagrant/projects/test/resources/views/auth/passwords/reset.blade.php
Created View: /home/vagrant/projects/test/resources/views/auth/emails/password.blade.php
Created View: /home/vagrant/projects/test/resources/views/layouts/app.blade.php
Created View: /home/vagrant/projects/test/resources/views/home.blade.php
Created View: /home/vagrant/projects/test/resources/views/welcome.blade.php
Installed HomeController.
Updated Routes File.
Authentication scaffolding generated successfully!

database dession driver

Schema::create('sessions', function ($table) {
    $table->string('id')->unique();
    $table->integer('user_id')->nullable();
    $table->string('ip_address', 45)->nullable();
    $table->text('user_agent')->nullable();
    $table->text('payload');
    $table->integer('last_activity');
});

eloquent global scope

static::addGlobalScope('finish', function(Builder $builder){
    $builder->where('status','finished');
});
Task::withoutGlobalScope('finish')->get();
Task::withoutGlobalScopes()->get();

schedule

$schedule->command('emails:send')
         ->hourly()
         ->appendOutputTo($filePath);

mysql json column types

$table->json('column_name');	JSON equivalent for the database.
$table->jsonb('column_name');	JSONB equivalent for the database.

new helpers

validator()

/**
 * Create a new Validator instance.
 *
 * @param  array  $data
 * @param  array  $rules
 * @param  array  $messages
 * @param  array  $customAttributes
 * @return \Illuminate\Contracts\Validation\Validator
 */
function validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = []){ ... }


$rules = [
    'name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users',
    'password' => 'required|confirmed|min:6',
];
 
validator($request->all(), $rules);

dispatch()

dispatch(new SendReminderEmail($user));



/* Old */

$this->dispatch(new SendReminderEmail($user));

5.3

- PHP 5.6.4+

- The `lists` method on the Collection, 
query builder and Eloquent query 
builder objects has been 
renamed to `pluck`

- Implicit controller routes using 
Route::controller have been 
deprecated

- The `get`, `post`, and other route 
helper functions have been removed.

thx

  • mattstauffer.co
  • laravel.com
  • laracasts.com
Made with Slides.com