{"version":"https:\/\/jsonfeed.org\/version\/1.1","title":"Gluten Design","home_page_url":"glutendesign.com","feed_url":"glutendesign.com\/feeds\/json","items":[{"id":"5","title":"6 astuces pour les Collection dans Laravel","url":"https:\/\/www.glutendesign.com\/posts\/6-astuces-pour-les-collection-dans-laravel","content_text":"Jeff Madsen pr\u00e9sente [six astuces tr\u00e8s utiles](http:\/\/codebyjeff.com\/blog\/2015\/05\/stupid-collection-tricks-in-laravel) si vous travaillez avec [Laravel](http:\/\/www.laravel.com\/) et notamment son excellent [ORM](https:\/\/en.wikipedia.org\/wiki\/Object-relational_mapping), [Eloquent](http:\/\/laravel.com\/docs\/5.0\/eloquent).\r\n\r\nLa prochaine fois que vous avez \u00e0 faire un `<select>` dans un formulaire, n\u2019oubliez pas celle-ci (modifi\u00e9e pour Laravel 5.1) :\r\n\r\n```php\r\n$engineer = $collection\r\n               ->where('type', 'engineer')\r\n\t           ->lists('first_name')\r\n\t           ->all();\r\n```\r\n\r\nVoir les autres astuces sur le blog de de Jeff Madsen : [Stupid Collection tricks in Laravel](http:\/\/codebyjeff.com\/blog\/2015\/05\/stupid-collection-tricks-in-laravel).","content_html":"<p>Jeff Madsen pr\u00e9sente <a href=\"http:\/\/codebyjeff.com\/blog\/2015\/05\/stupid-collection-tricks-in-laravel\">six astuces tr\u00e8s utiles<\/a> si vous travaillez avec <a href=\"http:\/\/www.laravel.com\/\">Laravel<\/a> et notamment son excellent <a href=\"https:\/\/en.wikipedia.org\/wiki\/Object-relational_mapping\">ORM<\/a>, <a href=\"http:\/\/laravel.com\/docs\/5.0\/eloquent\">Eloquent<\/a>.<\/p>\n<p>La prochaine fois que vous avez \u00e0 faire un <code>&lt;select&gt;<\/code> dans un formulaire, n\u2019oubliez pas celle-ci (modifi\u00e9e pour Laravel 5.1) :<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">$engineer = $collection\n               -&gt;where(<span class=\"hljs-string\">'type'<\/span>, <span class=\"hljs-string\">'engineer'<\/span>)\n\t           -&gt;lists(<span class=\"hljs-string\">'first_name'<\/span>)\n\t           -&gt;all();\n<\/code><\/pre>\n<p>Voir les autres astuces sur le blog de de Jeff Madsen : <a href=\"http:\/\/codebyjeff.com\/blog\/2015\/05\/stupid-collection-tricks-in-laravel\">Stupid Collection tricks in Laravel<\/a>.<\/p>","summary":"Jeff Madsen pr\u00e9sente six astuces tr\u00e8s utiles si vous travaillez avec Laravel et notamment son excellent ORM, Eloquent.\nLa prochaine fois que vous avez \u00e0 faire un &lt;select&gt; dans un formulaire, n\u2019oubliez pas celle-ci (modifi\u00e9e pour Laravel 5.1) :\n$engineer = $collection\n               -&gt;where('type', 'engineer')\n\t           -&gt;lists('first_name')\n\t           -&gt;all();\n\nVoir les autres as...","date_published":"2015-05-30T21:01:39+02:00","date_modified":"2015-12-16T11:50:44+01:00"},{"id":"6","title":"Automatic password resets with Laravel","url":"https:\/\/www.glutendesign.com\/posts\/automatic-passwords-resets-with-laravel","content_text":"I\u2019ve recently had to implement a way to force the registred users of a [Laravel 5.1](http:\/\/www.laravel.com) application to change their passwords after a given period. It\u2019s not a very good security measure if you ask me because of the burden it puts on the users: an effective security policy is a necessary trade-off between ease of use and things like password complexity. But if you ever need to implement such functionnality, here\u2019s how I\u2019ve done it.\r\n\r\n## Password timestamp\r\n\r\nWe\u2019ll need to add a `timestamp` column to the `users` table in order to retain the last date at which the password has been modified, so let\u2019s make a migration:\r\n\r\n```php\r\n<?php\r\n\r\nuse Illuminate\\Database\\Schema\\Blueprint;\r\nuse Illuminate\\Database\\Migrations\\Migration;\r\n\r\nclass AddPasswordUpdatedAtToUsersTable extends Migration\r\n{\r\n    \/**\r\n     * Run the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function up()\r\n    {\r\n        Schema::table('users', function(Blueprint $table) {\r\n            $table->timestamp('password_updated_at');\r\n        });\r\n    }\r\n\r\n    \/**\r\n     * Reverse the migrations.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function down()\r\n    {\r\n        Schema::table('users', function(Blueprint $table) {\r\n            $table->dropColumn('password_updated_at');\r\n        });\r\n    }\r\n}\r\n```\r\n\r\nDon\u2019t forget to actually run the migration: `php artisan migrate`.\r\n\r\nLaravel prodives an easy way to convert `timestamps` to instances of [Carbon](https:\/\/github.com\/briannesbitt\/Carbon) with the [dates mutator properties](http:\/\/laravel.com\/docs\/5.1\/eloquent-mutators#date-mutators) on the Eloquent model. By default the `created_at` and `updated_at` attributes are automatically converted but if you want to add a column you need to explicitly list them all:\r\n\r\n```php\r\n    \/**\r\n     * Date mutators.\r\n     *\r\n     * @var array\r\n     *\/\r\n    protected $dates = ['created_at', 'updated_at', 'password_updated_at'];\r\n```\r\n\r\n## Date comparison\r\n\r\nThis date mutator allows us to make use of the Carbon library to easily compare the newly added `password_updated_at` attribute to a date three months before. There\u2019s a lot of others useful methods to compare date with the Carbon library so if the months approach doesn\u2019t fit your project, just [check them out](http:\/\/carbon.nesbot.com\/docs\/#api-comparison).\r\n\r\nLet\u2019s wrap that verification into a method of our User model, not that you will use it much but it makes for a more readable code:\r\n\r\n```php\r\n\/**\r\n * Check if user has an old password that needs to be reset\r\n * @return boolean\r\n *\/\r\npublic function hasOldPassword()\r\n{\r\n    return $this->password_updated_at->lt(Carbon::now()->subMonths(3));\r\n}\r\n```\r\n\r\nGreat. Now we can add it to the `Authenticate` middleware:\r\n\r\n```php\r\nif ($request->user()->hasOldPassword()) {\r\n    return redirect('old-password-reset');\r\n}\r\n```\r\n\r\n## The form\r\n\r\nTo recap: the Autenticate middleware checks if the current user is authenticated, if not it redirects to the `auth\/login` route. If the user is logged it will then check for the last password reset and if it is too old then it redirects to the `old-password-reset`. But we don\u2019t have such a route don\u2019t we? Let\u2019s tackle that:\r\n\r\n```php\r\nRoute::get('old-password-reset', 'Auth\\PasswordController@getOldPasswordReset');\r\n```\r\n\r\nThis route references the `getOldPasswordReset` method on the `PasswordController`. What we need this route to do is to return a view explaining why the user is redirected and a form to reset his password:\r\n\r\n```php\r\n\/**\r\n* Get the password reset view when it is too old\r\n* @return Response\r\n*\/\r\npublic function getOldPasswordReset()\r\n{\r\n    return view('auth.reset-old');\r\n}\r\n```\r\n\r\nNothing crazy. We still need that view though. I\u2019ll intentionnaly leave off the template part of it and just expose the necessary. I\u2019m using [Bootstrap](http:\/\/www.getbootstrap.com\/) and [Laravel Collective Forms](http:\/\/laravelcollective.com\/docs\/5.1\/html) here though:\r\n\r\n```php\r\n{!! Form::open(['method' => 'POST', 'route' => 'store-new-password', 'class' => 'form-horizontal']) !!}\r\n\r\n    <div class=\"form-group\">\r\n        {!! Form::label('old_password', 'Password *', ['class' => 'col-sm-3 control-label']) !!}\r\n            <div class=\"col-sm-9\">\r\n                {!! Form::password('old_password', ['class' => 'form-control', 'required' => 'required']) !!}\r\n                <small class=\"text-danger\">{{ $errors->first('old_password') }}<\/small>\r\n            <\/div>\r\n    <\/div>\r\n\r\n    <div class=\"form-group\">\r\n        {!! Form::label('password', 'New password *', ['class' => 'col-sm-3 control-label']) !!}\r\n            <div class=\"col-sm-9\">\r\n                {!! Form::password('password', ['class' => 'form-control', 'required' => 'required']) !!}\r\n                <small class=\"text-danger\">{{ $errors->first('password') }}<\/small>\r\n            <\/div>\r\n    <\/div>\r\n\r\n    <div class=\"form-group\">\r\n        {!! Form::label('password_confirmation', 'Confirm your new password *', ['class' => 'col-sm-3 control-label']) !!}\r\n            <div class=\"col-sm-9\">\r\n                {!! Form::password('password_confirmation', ['class' => 'form-control', 'required' => 'required']) !!}\r\n                <small class=\"text-danger\">{{ $errors->first('password_confirmation') }}<\/small>\r\n            <\/div>\r\n    <\/div>\r\n\r\n    <div class=\"btn-group pull-right\">\r\n        {!! Form::submit(\"Save\", ['class' => 'btn btn-success']) !!}\r\n    <\/div>\r\n\r\n{!! Form::close() !!}\r\n```\r\n\r\n## Validation and storage\r\n\r\nYep. So three form inputs here: the first one will check for the actual password as a security measure while the second and the third will store the new password. Let\u2019s make a new method inside our `PasswordController` to validate and store the new password:\r\n\r\n```php\r\n\/**\r\n * Store new password in DB\r\n * @param  Request $request\r\n * @return Response\r\n *\/\r\npublic function storeNewPassword(Request $request)\r\n{\r\n    $this->validate($request, [\r\n        'old_password' => 'required|hashmatch:' . auth()->user()->id,\r\n        'password'     => 'required|regex:((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20})|confirmed|hashdiffer:' . auth()->user()->id,\r\n    ]);\r\n\r\n    $user = auth()->user();\r\n\r\n    $user->password            = $request->password;\r\n    $user->password_updated_at = Carbon::now();\r\n\r\n    $user->save();\r\n\r\n    return redirect('admin')->with('message', 'Your password has been updated');\r\n}\r\n```\r\n\r\nI\u2019m using two customs validation rules here in order to check that:\r\n1. the `old_password` field matches the old one.\r\n2. the new password is not the same as the old one\r\n\r\nIn order to add those custom validation rules we\u2019ll going to add them to the `boot()` method of the `AppServiceProvider` class:\r\n\r\n```php\r\n\/**\r\n * Bootstrap any application services.\r\n *\r\n * @return void\r\n *\/\r\npublic function boot()\r\n{\r\n    \/\/ Check given password against password stored in database\r\n    Validator::extend('hashmatch', function($attribute, $value, $parameters)\r\n    {\r\n        $user = User::find($parameters[0]);\r\n\r\n        return Hash::check($value, $user->password);\r\n    });\r\n\r\n    \/\/ Check that given password is not the same as the password stored in database\r\n    Validator::extend('hashdiffer', function($attribute, $value, $parameters)\r\n    {\r\n        $user = User::find($parameters[0]);\r\n\r\n        return ! Hash::check($value, $user->password);\r\n    });\r\n}\r\n```\r\n\r\nAnd that\u2019s it! You may still want to add custom errors messages to provide user-friendly feedbacks, but other than that we\u2019re done.\r\n","content_html":"<p>I\u2019ve recently had to implement a way to force the registred users of a <a href=\"http:\/\/www.laravel.com\">Laravel 5.1<\/a> application to change their passwords after a given period. It\u2019s not a very good security measure if you ask me because of the burden it puts on the users: an effective security policy is a necessary trade-off between ease of use and things like password complexity. But if you ever need to implement such functionnality, here\u2019s how I\u2019ve done it.<\/p>\n<h2>Password timestamp<\/h2>\n<p>We\u2019ll need to add a <code>timestamp<\/code> column to the <code>users<\/code> table in order to retain the last date at which the password has been modified, so let\u2019s make a migration:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Database<\/span>\\<span class=\"hljs-title\">Schema<\/span>\\<span class=\"hljs-title\">Blueprint<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Illuminate<\/span>\\<span class=\"hljs-title\">Database<\/span>\\<span class=\"hljs-title\">Migrations<\/span>\\<span class=\"hljs-title\">Migration<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AddPasswordUpdatedAtToUsersTable<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Migration<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/**\n     * Run the migrations.\n     *\n     * <span class=\"hljs-doctag\">@return<\/span> void\n     *\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">up<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        Schema::table(<span class=\"hljs-string\">'users'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">(Blueprint $table)<\/span> <\/span>{\n            $table-&gt;timestamp(<span class=\"hljs-string\">'password_updated_at'<\/span>);\n        });\n    }\n\n    <span class=\"hljs-comment\">\/**\n     * Reverse the migrations.\n     *\n     * <span class=\"hljs-doctag\">@return<\/span> void\n     *\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">down<\/span><span class=\"hljs-params\">()<\/span>\n    <\/span>{\n        Schema::table(<span class=\"hljs-string\">'users'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">(Blueprint $table)<\/span> <\/span>{\n            $table-&gt;dropColumn(<span class=\"hljs-string\">'password_updated_at'<\/span>);\n        });\n    }\n}\n<\/code><\/pre>\n<p>Don\u2019t forget to actually run the migration: <code>php artisan migrate<\/code>.<\/p>\n<p>Laravel prodives an easy way to convert <code>timestamps<\/code> to instances of <a href=\"https:\/\/github.com\/briannesbitt\/Carbon\">Carbon<\/a> with the <a href=\"http:\/\/laravel.com\/docs\/5.1\/eloquent-mutators#date-mutators\">dates mutator properties<\/a> on the Eloquent model. By default the <code>created_at<\/code> and <code>updated_at<\/code> attributes are automatically converted but if you want to add a column you need to explicitly list them all:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">    <span class=\"hljs-comment\">\/**\n     * Date mutators.\n     *\n     * <span class=\"hljs-doctag\">@var<\/span> array\n     *\/<\/span>\n    <span class=\"hljs-keyword\">protected<\/span> $dates = [<span class=\"hljs-string\">'created_at'<\/span>, <span class=\"hljs-string\">'updated_at'<\/span>, <span class=\"hljs-string\">'password_updated_at'<\/span>];\n<\/code><\/pre>\n<h2>Date comparison<\/h2>\n<p>This date mutator allows us to make use of the Carbon library to easily compare the newly added <code>password_updated_at<\/code> attribute to a date three months before. There\u2019s a lot of others useful methods to compare date with the Carbon library so if the months approach doesn\u2019t fit your project, just <a href=\"http:\/\/carbon.nesbot.com\/docs\/#api-comparison\">check them out<\/a>.<\/p>\n<p>Let\u2019s wrap that verification into a method of our User model, not that you will use it much but it makes for a more readable code:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-comment\">\/**\n * Check if user has an old password that needs to be reset\n * <span class=\"hljs-doctag\">@return<\/span> boolean\n *\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">hasOldPassword<\/span><span class=\"hljs-params\">()<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">$this<\/span>-&gt;password_updated_at-&gt;lt(Carbon::now()-&gt;subMonths(<span class=\"hljs-number\">3<\/span>));\n}\n<\/code><\/pre>\n<p>Great. Now we can add it to the <code>Authenticate<\/code> middleware:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-keyword\">if<\/span> ($request-&gt;user()-&gt;hasOldPassword()) {\n    <span class=\"hljs-keyword\">return<\/span> redirect(<span class=\"hljs-string\">'old-password-reset'<\/span>);\n}\n<\/code><\/pre>\n<h2>The form<\/h2>\n<p>To recap: the Autenticate middleware checks if the current user is authenticated, if not it redirects to the <code>auth\/login<\/code> route. If the user is logged it will then check for the last password reset and if it is too old then it redirects to the <code>old-password-reset<\/code>. But we don\u2019t have such a route don\u2019t we? Let\u2019s tackle that:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">Route::get(<span class=\"hljs-string\">'old-password-reset'<\/span>, <span class=\"hljs-string\">'Auth\\PasswordController@getOldPasswordReset'<\/span>);\n<\/code><\/pre>\n<p>This route references the <code>getOldPasswordReset<\/code> method on the <code>PasswordController<\/code>. What we need this route to do is to return a view explaining why the user is redirected and a form to reset his password:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-comment\">\/**\n* Get the password reset view when it is too old\n* <span class=\"hljs-doctag\">@return<\/span> Response\n*\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">getOldPasswordReset<\/span><span class=\"hljs-params\">()<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">return<\/span> view(<span class=\"hljs-string\">'auth.reset-old'<\/span>);\n}\n<\/code><\/pre>\n<p>Nothing crazy. We still need that view though. I\u2019ll intentionnaly leave off the template part of it and just expose the necessary. I\u2019m using <a href=\"http:\/\/www.getbootstrap.com\/\">Bootstrap<\/a> and <a href=\"http:\/\/laravelcollective.com\/docs\/5.1\/html\">Laravel Collective Forms<\/a> here though:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">{!! Form::open([<span class=\"hljs-string\">'method'<\/span> =&gt; <span class=\"hljs-string\">'POST'<\/span>, <span class=\"hljs-string\">'route'<\/span> =&gt; <span class=\"hljs-string\">'store-new-password'<\/span>, <span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'form-horizontal'<\/span>]) !!}\n\n    &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">form<\/span>-<span class=\"hljs-title\">group<\/span>\"&gt;\n        <\/span>{!! Form::label(<span class=\"hljs-string\">'old_password'<\/span>, <span class=\"hljs-string\">'Password *'<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'col-sm-3 control-label'<\/span>]) !!}\n            &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">col<\/span>-<span class=\"hljs-title\">sm<\/span>-9\"&gt;\n                <\/span>{!! Form::password(<span class=\"hljs-string\">'old_password'<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'form-control'<\/span>, <span class=\"hljs-string\">'required'<\/span> =&gt; <span class=\"hljs-string\">'required'<\/span>]) !!}\n                &lt;small <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">text<\/span>-<span class=\"hljs-title\">danger<\/span>\"&gt;<\/span>{{ $errors-&gt;first(<span class=\"hljs-string\">'old_password'<\/span>) }}&lt;\/small&gt;\n            &lt;\/div&gt;\n    &lt;\/div&gt;\n\n    &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">form<\/span>-<span class=\"hljs-title\">group<\/span>\"&gt;\n        <\/span>{!! Form::label(<span class=\"hljs-string\">'password'<\/span>, <span class=\"hljs-string\">'New password *'<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'col-sm-3 control-label'<\/span>]) !!}\n            &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">col<\/span>-<span class=\"hljs-title\">sm<\/span>-9\"&gt;\n                <\/span>{!! Form::password(<span class=\"hljs-string\">'password'<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'form-control'<\/span>, <span class=\"hljs-string\">'required'<\/span> =&gt; <span class=\"hljs-string\">'required'<\/span>]) !!}\n                &lt;small <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">text<\/span>-<span class=\"hljs-title\">danger<\/span>\"&gt;<\/span>{{ $errors-&gt;first(<span class=\"hljs-string\">'password'<\/span>) }}&lt;\/small&gt;\n            &lt;\/div&gt;\n    &lt;\/div&gt;\n\n    &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">form<\/span>-<span class=\"hljs-title\">group<\/span>\"&gt;\n        <\/span>{!! Form::label(<span class=\"hljs-string\">'password_confirmation'<\/span>, <span class=\"hljs-string\">'Confirm your new password *'<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'col-sm-3 control-label'<\/span>]) !!}\n            &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">col<\/span>-<span class=\"hljs-title\">sm<\/span>-9\"&gt;\n                <\/span>{!! Form::password(<span class=\"hljs-string\">'password_confirmation'<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'form-control'<\/span>, <span class=\"hljs-string\">'required'<\/span> =&gt; <span class=\"hljs-string\">'required'<\/span>]) !!}\n                &lt;small <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">text<\/span>-<span class=\"hljs-title\">danger<\/span>\"&gt;<\/span>{{ $errors-&gt;first(<span class=\"hljs-string\">'password_confirmation'<\/span>) }}&lt;\/small&gt;\n            &lt;\/div&gt;\n    &lt;\/div&gt;\n\n    &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">btn<\/span>-<span class=\"hljs-title\">group<\/span> <span class=\"hljs-title\">pull<\/span>-<span class=\"hljs-title\">right<\/span>\"&gt;\n        <\/span>{!! Form::submit(<span class=\"hljs-string\">\"Save\"<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'btn btn-success'<\/span>]) !!}\n    &lt;\/div&gt;\n\n{!! Form::close() !!}\n<\/code><\/pre>\n<h2>Validation and storage<\/h2>\n<p>Yep. So three form inputs here: the first one will check for the actual password as a security measure while the second and the third will store the new password. Let\u2019s make a new method inside our <code>PasswordController<\/code> to validate and store the new password:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-comment\">\/**\n * Store new password in DB\n * <span class=\"hljs-doctag\">@param<\/span>  Request $request\n * <span class=\"hljs-doctag\">@return<\/span> Response\n *\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">storeNewPassword<\/span><span class=\"hljs-params\">(Request $request)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">$this<\/span>-&gt;validate($request, [\n        <span class=\"hljs-string\">'old_password'<\/span> =&gt; <span class=\"hljs-string\">'required|hashmatch:'<\/span> . auth()-&gt;user()-&gt;id,\n        <span class=\"hljs-string\">'password'<\/span>     =&gt; <span class=\"hljs-string\">'required|regex:((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20})|confirmed|hashdiffer:'<\/span> . auth()-&gt;user()-&gt;id,\n    ]);\n\n    $user = auth()-&gt;user();\n\n    $user-&gt;password            = $request-&gt;password;\n    $user-&gt;password_updated_at = Carbon::now();\n\n    $user-&gt;save();\n\n    <span class=\"hljs-keyword\">return<\/span> redirect(<span class=\"hljs-string\">'admin'<\/span>)-&gt;with(<span class=\"hljs-string\">'message'<\/span>, <span class=\"hljs-string\">'Your password has been updated'<\/span>);\n}\n<\/code><\/pre>\n<p>I\u2019m using two customs validation rules here in order to check that:<\/p>\n<ol>\n<li>the <code>old_password<\/code> field matches the old one.<\/li>\n<li>the new password is not the same as the old one<\/li>\n<\/ol>\n<p>In order to add those custom validation rules we\u2019ll going to add them to the <code>boot()<\/code> method of the <code>AppServiceProvider<\/code> class:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-comment\">\/**\n * Bootstrap any application services.\n *\n * <span class=\"hljs-doctag\">@return<\/span> void\n *\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">boot<\/span><span class=\"hljs-params\">()<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/\/ Check given password against password stored in database<\/span>\n    Validator::extend(<span class=\"hljs-string\">'hashmatch'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">($attribute, $value, $parameters)<\/span>\n    <\/span>{\n        $user = User::find($parameters[<span class=\"hljs-number\">0<\/span>]);\n\n        <span class=\"hljs-keyword\">return<\/span> Hash::check($value, $user-&gt;password);\n    });\n\n    <span class=\"hljs-comment\">\/\/ Check that given password is not the same as the password stored in database<\/span>\n    Validator::extend(<span class=\"hljs-string\">'hashdiffer'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span><span class=\"hljs-params\">($attribute, $value, $parameters)<\/span>\n    <\/span>{\n        $user = User::find($parameters[<span class=\"hljs-number\">0<\/span>]);\n\n        <span class=\"hljs-keyword\">return<\/span> ! Hash::check($value, $user-&gt;password);\n    });\n}\n<\/code><\/pre>\n<p>And that\u2019s it! You may still want to add custom errors messages to provide user-friendly feedbacks, but other than that we\u2019re done.<\/p>","summary":"I\u2019ve recently had to implement a way to force the registred users of a Laravel 5.1 application to change their passwords after a given period. It\u2019s not a very good security measure if you ask me because of the burden it puts on the users: an effective security policy is a necessary trade-off between ease of use and things like password complexity. But if you ever need to implement such functionnal...","date_published":"2015-10-06T23:12:01+02:00","date_modified":"2015-12-16T11:50:58+01:00"},{"id":"7","title":"Detect and change language on the fly with Laravel","url":"https:\/\/www.glutendesign.com\/posts\/detect-and-change-language-on-the-fly-with-laravel","content_text":"If you\u2019re builiding a multilingual site you may want to guess the user\u2019s language in order to provide the correct content and still let her decide afterwards which language to use. So this a is two part problem:\r\n\r\n1. Find a way to detect the user\u2019s native language and tell Laravel to use it\r\n2. Provide a way for the user to change it and store that choice in session\r\n\r\nThis tutorial will *not* cover how to make a multilingual Laravel application, for that I\u2019ll suggest you take a look at [Laravel Translatable](https:\/\/github.com\/dimsav\/laravel-translatable) and [this tutorial](https:\/\/laravel-news.com\/2015\/09\/how-to-add-multilingual-support-to-eloquent\/) on Laravel News.\r\n\r\n## Detect the user\u2019s native language\r\n\r\nThere are multiple ways to detect language from a HTTP Request but as I only needed to check for english or french speakers, I choosed to check if those were present in the `HTTP_ACCEPT_LANGUAGE` HTTP directive and fallback to english if nothing matches.\r\n\r\nThe best way to do that is to use a [Middleware](http:\/\/laravel.com\/docs\/5.1\/middleware). Type the following command from the console to create a new one: `php artisan make:middleware SetLocale`.\r\n\r\n```php\r\n<?php\r\n\r\nnamespace App\\Http\\Middleware;\r\n\r\nuse Closure;\r\nuse Session;\r\nuse App;\r\nuse Config;\r\n\r\nclass SetLocale\r\n{\r\n    \/**\r\n     *\r\n     * Handle an incoming request.\r\n     *\r\n     * @param  \\Illuminate\\Http\\Request  $request\r\n     * @param  \\Closure  $next\r\n     * @return mixed\r\n     *\/\r\n    public function handle($request, Closure $next)\r\n    {\r\n        if (Session::has('locale')) {\r\n            $locale = Session::get('locale', Config::get('app.locale'));\r\n        } else {\r\n            $locale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);\r\n\r\n            if ($locale != 'fr' && $locale != 'en') {\r\n                $locale = 'en';\r\n            }\r\n        }\r\n\r\n        App::setLocale($locale);\r\n\r\n        return $next($request);\r\n    }\r\n}\r\n```\r\n\r\nThen you need to register this middleware to be fired on each request in the `$middleware` attribute inside `app\/Http\/Middleware\/Kernel.php`:\r\n\r\n```php\r\n\/**\r\n * The application's global HTTP middleware stack.\r\n *\r\n * @var array\r\n *\/\r\nprotected $middleware = [\r\n    \\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode::class,\r\n    \\App\\Http\\Middleware\\EncryptCookies::class,\r\n    \\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse::class,\r\n    \\Illuminate\\Session\\Middleware\\StartSession::class,\r\n    \\Illuminate\\View\\Middleware\\ShareErrorsFromSession::class,\r\n    \\App\\Http\\Middleware\\VerifyCsrfToken::class,\r\n    \\App\\Http\\Middleware\\SetLocale::class,\r\n];\r\n```\r\n\r\nAnd that\u2019s it!\r\n\r\n## Allow user to change locale on the fly\r\n\r\nWe will use a simple form with a `select` input to let the user choose its language, this is not necessarily the best UX implementation you can come up with but that\u2019s not the purpose of this tutorial so I\u2019ll let you decide on what\u2019s best. I\u2019m using [Laravel Collective Forms](http:\/\/laravelcollective.com\/docs\/5.1\/html) and [Bootstrap 3](http:\/\/getbootstrap.com) for convenience here.\r\n\r\nSo let\u2019s make that simple form:\r\n\r\n\r\n```php\r\n{!! Form::open(['method' => 'POST', 'route' => 'changelocale', 'class' => 'form-inline navbar-select']) !!}\r\n\r\n    <div class=\"form-group @if($errors->first('locale')) has-error @endif\">\r\n        <span aria-hidden=\"true\"><i class=\"fa fa-flag\"><\/i><\/span>\r\n        {!! Form::select(\r\n            'locale',\r\n            ['en' => 'EN', 'fr' => 'FR'],\r\n            \\App::getLocale(),\r\n            [\r\n                'id'       => 'locale',\r\n                'class'    => 'form-control',\r\n                'required' => 'required',\r\n                'onchange' => 'this.form.submit()',\r\n            ]\r\n        ) !!}\r\n        <small class=\"text-danger\">{{ $errors->first('locale') }}<\/small>\r\n    <\/div>\r\n\r\n    <div class=\"btn-group pull-right sr-only\">\r\n        {!! Form::submit(\"Change\", ['class' => 'btn btn-success']) !!}\r\n    <\/div>\r\n\r\n{!! Form::close() !!}\r\n```\r\n\r\nThen we need to declare the `POST` route to bind our form with a method (in `app\/Http\/routes.php`):\r\n\r\n```php\r\nRoute::post('changelocale', ['as' => 'changelocale', 'uses' => 'TranslationController@changeLocale']);\r\n```\r\n\r\nAnd then in `app\/Http\/Controllers\/TranslationController.php` (or whatever controller suits you best):\r\n\r\n```php\r\n\/**\r\n * Change session locale\r\n * @param  Request $request\r\n * @return Response\r\n *\/\r\npublic function changeLocale(Request $request)\r\n{\r\n    $this->validate($request, ['locale' => 'required|in:fr,en']);\r\n\r\n    \\Session::put('locale', $request->locale);\r\n\r\n    return redirect()->back();\r\n}\r\n```\r\n\r\nThat\u2019s all there is to it.\r\n","content_html":"<p>If you\u2019re builiding a multilingual site you may want to guess the user\u2019s language in order to provide the correct content and still let her decide afterwards which language to use. So this a is two part problem:<\/p>\n<ol>\n<li>Find a way to detect the user\u2019s native language and tell Laravel to use it<\/li>\n<li>Provide a way for the user to change it and store that choice in session<\/li>\n<\/ol>\n<p>This tutorial will <em>not<\/em> cover how to make a multilingual Laravel application, for that I\u2019ll suggest you take a look at <a href=\"https:\/\/github.com\/dimsav\/laravel-translatable\">Laravel Translatable<\/a> and <a href=\"https:\/\/laravel-news.com\/2015\/09\/how-to-add-multilingual-support-to-eloquent\/\">this tutorial<\/a> on Laravel News.<\/p>\n<h2>Detect the user\u2019s native language<\/h2>\n<p>There are multiple ways to detect language from a HTTP Request but as I only needed to check for english or french speakers, I choosed to check if those were present in the <code>HTTP_ACCEPT_LANGUAGE<\/code> HTTP directive and fallback to english if nothing matches.<\/p>\n<p>The best way to do that is to use a <a href=\"http:\/\/laravel.com\/docs\/5.1\/middleware\">Middleware<\/a>. Type the following command from the console to create a new one: <code>php artisan make:middleware SetLocale<\/code>.<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">App<\/span>\\<span class=\"hljs-title\">Http<\/span>\\<span class=\"hljs-title\">Middleware<\/span>;\n\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Closure<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Session<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">App<\/span>;\n<span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">Config<\/span>;\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SetLocale<\/span>\n<\/span>{\n    <span class=\"hljs-comment\">\/**\n     *\n     * Handle an incoming request.\n     *\n     * <span class=\"hljs-doctag\">@param<\/span>  \\Illuminate\\Http\\Request  $request\n     * <span class=\"hljs-doctag\">@param<\/span>  \\Closure  $next\n     * <span class=\"hljs-doctag\">@return<\/span> mixed\n     *\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">handle<\/span><span class=\"hljs-params\">($request, Closure $next)<\/span>\n    <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> (Session::has(<span class=\"hljs-string\">'locale'<\/span>)) {\n            $locale = Session::get(<span class=\"hljs-string\">'locale'<\/span>, Config::get(<span class=\"hljs-string\">'app.locale'<\/span>));\n        } <span class=\"hljs-keyword\">else<\/span> {\n            $locale = substr($request-&gt;server(<span class=\"hljs-string\">'HTTP_ACCEPT_LANGUAGE'<\/span>), <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">2<\/span>);\n\n            <span class=\"hljs-keyword\">if<\/span> ($locale != <span class=\"hljs-string\">'fr'<\/span> &amp;&amp; $locale != <span class=\"hljs-string\">'en'<\/span>) {\n                $locale = <span class=\"hljs-string\">'en'<\/span>;\n            }\n        }\n\n        App::setLocale($locale);\n\n        <span class=\"hljs-keyword\">return<\/span> $next($request);\n    }\n}\n<\/code><\/pre>\n<p>Then you need to register this middleware to be fired on each request in the <code>$middleware<\/code> attribute inside <code>app\/Http\/Middleware\/Kernel.php<\/code>:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-comment\">\/**\n * The application's global HTTP middleware stack.\n *\n * <span class=\"hljs-doctag\">@var<\/span> array\n *\/<\/span>\n<span class=\"hljs-keyword\">protected<\/span> $middleware = [\n    \\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode::class,\n    \\App\\Http\\Middleware\\EncryptCookies::class,\n    \\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse::class,\n    \\Illuminate\\Session\\Middleware\\StartSession::class,\n    \\Illuminate\\View\\Middleware\\ShareErrorsFromSession::class,\n    \\App\\Http\\Middleware\\VerifyCsrfToken::class,\n    \\App\\Http\\Middleware\\SetLocale::class,\n];\n<\/code><\/pre>\n<p>And that\u2019s it!<\/p>\n<h2>Allow user to change locale on the fly<\/h2>\n<p>We will use a simple form with a <code>select<\/code> input to let the user choose its language, this is not necessarily the best UX implementation you can come up with but that\u2019s not the purpose of this tutorial so I\u2019ll let you decide on what\u2019s best. I\u2019m using <a href=\"http:\/\/laravelcollective.com\/docs\/5.1\/html\">Laravel Collective Forms<\/a> and <a href=\"http:\/\/getbootstrap.com\">Bootstrap 3<\/a> for convenience here.<\/p>\n<p>So let\u2019s make that simple form:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">{!! Form::open([<span class=\"hljs-string\">'method'<\/span> =&gt; <span class=\"hljs-string\">'POST'<\/span>, <span class=\"hljs-string\">'route'<\/span> =&gt; <span class=\"hljs-string\">'changelocale'<\/span>, <span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'form-inline navbar-select'<\/span>]) !!}\n\n    &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">form<\/span>-<span class=\"hljs-title\">group<\/span> @<span class=\"hljs-title\">if<\/span>($<span class=\"hljs-title\">errors<\/span>-&gt;<span class=\"hljs-title\">first<\/span>('<span class=\"hljs-title\">locale<\/span>')) <span class=\"hljs-title\">has<\/span>-<span class=\"hljs-title\">error<\/span> @<span class=\"hljs-title\">endif<\/span>\"&gt;\n        &lt;<span class=\"hljs-title\">span<\/span> <span class=\"hljs-title\">aria<\/span>-<span class=\"hljs-title\">hidden<\/span>=\"<span class=\"hljs-title\">true<\/span>\"&gt;&lt;<span class=\"hljs-title\">i<\/span> <span class=\"hljs-title\">class<\/span>=\"<span class=\"hljs-title\">fa<\/span> <span class=\"hljs-title\">fa<\/span>-<span class=\"hljs-title\">flag<\/span>\"&gt;&lt;\/<span class=\"hljs-title\">i<\/span>&gt;&lt;\/<span class=\"hljs-title\">span<\/span>&gt;\n        <\/span>{!! Form::select(\n            <span class=\"hljs-string\">'locale'<\/span>,\n            [<span class=\"hljs-string\">'en'<\/span> =&gt; <span class=\"hljs-string\">'EN'<\/span>, <span class=\"hljs-string\">'fr'<\/span> =&gt; <span class=\"hljs-string\">'FR'<\/span>],\n            \\App::getLocale(),\n            [\n                <span class=\"hljs-string\">'id'<\/span>       =&gt; <span class=\"hljs-string\">'locale'<\/span>,\n                <span class=\"hljs-string\">'class'<\/span>    =&gt; <span class=\"hljs-string\">'form-control'<\/span>,\n                <span class=\"hljs-string\">'required'<\/span> =&gt; <span class=\"hljs-string\">'required'<\/span>,\n                <span class=\"hljs-string\">'onchange'<\/span> =&gt; <span class=\"hljs-string\">'this.form.submit()'<\/span>,\n            ]\n        ) !!}\n        &lt;small <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">text<\/span>-<span class=\"hljs-title\">danger<\/span>\"&gt;<\/span>{{ $errors-&gt;first(<span class=\"hljs-string\">'locale'<\/span>) }}&lt;\/small&gt;\n    &lt;\/div&gt;\n\n    &lt;div <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"<span class=\"hljs-title\">btn<\/span>-<span class=\"hljs-title\">group<\/span> <span class=\"hljs-title\">pull<\/span>-<span class=\"hljs-title\">right<\/span> <span class=\"hljs-title\">sr<\/span>-<span class=\"hljs-title\">only<\/span>\"&gt;\n        <\/span>{!! Form::submit(<span class=\"hljs-string\">\"Change\"<\/span>, [<span class=\"hljs-string\">'class'<\/span> =&gt; <span class=\"hljs-string\">'btn btn-success'<\/span>]) !!}\n    &lt;\/div&gt;\n\n{!! Form::close() !!}\n<\/code><\/pre>\n<p>Then we need to declare the <code>POST<\/code> route to bind our form with a method (in <code>app\/Http\/routes.php<\/code>):<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">Route::post(<span class=\"hljs-string\">'changelocale'<\/span>, [<span class=\"hljs-string\">'as'<\/span> =&gt; <span class=\"hljs-string\">'changelocale'<\/span>, <span class=\"hljs-string\">'uses'<\/span> =&gt; <span class=\"hljs-string\">'TranslationController@changeLocale'<\/span>]);\n<\/code><\/pre>\n<p>And then in <code>app\/Http\/Controllers\/TranslationController.php<\/code> (or whatever controller suits you best):<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\"><span class=\"hljs-comment\">\/**\n * Change session locale\n * <span class=\"hljs-doctag\">@param<\/span>  Request $request\n * <span class=\"hljs-doctag\">@return<\/span> Response\n *\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">changeLocale<\/span><span class=\"hljs-params\">(Request $request)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">$this<\/span>-&gt;validate($request, [<span class=\"hljs-string\">'locale'<\/span> =&gt; <span class=\"hljs-string\">'required|in:fr,en'<\/span>]);\n\n    \\Session::put(<span class=\"hljs-string\">'locale'<\/span>, $request-&gt;locale);\n\n    <span class=\"hljs-keyword\">return<\/span> redirect()-&gt;back();\n}\n<\/code><\/pre>\n<p>That\u2019s all there is to it.<\/p>","summary":"If you\u2019re builiding a multilingual site you may want to guess the user\u2019s language in order to provide the correct content and still let her decide afterwards which language to use. So this a is two part problem:\n\nFind a way to detect the user\u2019s native language and tell Laravel to use it\nProvide a way for the user to change it and store that choice in session\n\nThis tutorial will <em>not<\/em> cover...","date_published":"2015-11-19T18:37:04+01:00","date_modified":"2015-12-16T11:49:59+01:00"},{"id":"8","title":"A Laravel factory with starting and ending dates","url":"https:\/\/www.glutendesign.com\/posts\/laravel-factory-starting-ending-dates","content_text":"Laravel provides a convenient way to seed your database with fake data for testing purpose using [a system of factories](https:\/\/laravel.com\/docs\/5.2\/testing#model-factories\/) for your Eloquent models.\r\nvh\r\nThis is a demonstration of how to create a factory for a model that is using starting and ending dates, like a `Workshop` model for example.\r\n\r\nFirst we need a starting date that would be useful for the tests: one that\u2019s not already past and yet not too far in the future because you might filter the whorkshops in your code to only display relevant informations. The great [faker](https:\/\/github.com\/fzaninotto\/Faker) library comes to help by providing a few date-related functions like `dateTimeThisYear()` to which you can pass a string that will be interpreted by `strotime()`: `$faker->dateTimeThisYear('+1 month')`. You can then use `strotime()` to generate an endind date at a specific time *after* the starting date: `strtotime('+1 Week', $startingDate->getTimestamp())`.\r\n\r\nThe factory will look like:\r\n\r\n```php\r\n$factory->define(App\\Workshop::class, function (Faker\\Generator $faker) {\r\n    $startingDate = $faker->dateTimeThisYear('+1 month');\r\n    $endingDate   = strtotime('+1 Week', $startingDate->getTimestamp());\r\n\r\n    return [\r\n        'starting_date' => $startingDate,\r\n        'ending_date'   => $endingDate,\r\n        \/\/ ... other model attributes\r\n    ];\r\n});\r\n```\r\n\r\nYou may also want to generate whorkshops for, say, the current week, by using the faker method `dateTimeBetween()`:\r\n\r\n```php\r\n$factory->defineAs(App\\Working::class, 'this_week', function (Faker\\Generator $faker) use ($factory) {\r\n    $activity = $factory->raw(App\\Activity::class);\r\n    \/\/ Random datetime of the current week\r\n    $startingDate = $faker->dateTimeBetween('this week', '+6 days');\r\n    \/\/ Random datetime of the current week *after* `$startingDate`\r\n    $endingDate   = $faker->dateTimeBetween($startingDate, strtotime('+6 days'));\r\n\r\n    return array_merge($activity, [\r\n        'starting_date' => $startingDate,\r\n        'ending_date'   => $endingDate,\r\n    ]);\r\n});\r\n```","content_html":"<p>Laravel provides a convenient way to seed your database with fake data for testing purpose using <a href=\"https:\/\/laravel.com\/docs\/5.2\/testing#model-factories\/\">a system of factories<\/a> for your Eloquent models.\nvh\nThis is a demonstration of how to create a factory for a model that is using starting and ending dates, like a <code>Workshop<\/code> model for example.<\/p>\n<p>First we need a starting date that would be useful for the tests: one that\u2019s not already past and yet not too far in the future because you might filter the whorkshops in your code to only display relevant informations. The great <a href=\"https:\/\/github.com\/fzaninotto\/Faker\">faker<\/a> library comes to help by providing a few date-related functions like <code>dateTimeThisYear()<\/code> to which you can pass a string that will be interpreted by <code>strotime()<\/code>: <code>$faker-&gt;dateTimeThisYear('+1 month')<\/code>. You can then use <code>strotime()<\/code> to generate an endind date at a specific time <em>after<\/em> the starting date: <code>strtotime('+1 Week', $startingDate-&gt;getTimestamp())<\/code>.<\/p>\n<p>The factory will look like:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">$factory-&gt;define(App\\Workshop::class, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">(Faker\\Generator $faker)<\/span> <\/span>{\n    $startingDate = $faker-&gt;dateTimeThisYear(<span class=\"hljs-string\">'+1 month'<\/span>);\n    $endingDate   = strtotime(<span class=\"hljs-string\">'+1 Week'<\/span>, $startingDate-&gt;getTimestamp());\n\n    <span class=\"hljs-keyword\">return<\/span> [\n        <span class=\"hljs-string\">'starting_date'<\/span> =&gt; $startingDate,\n        <span class=\"hljs-string\">'ending_date'<\/span>   =&gt; $endingDate,\n        <span class=\"hljs-comment\">\/\/ ... other model attributes<\/span>\n    ];\n});\n<\/code><\/pre>\n<p>You may also want to generate whorkshops for, say, the current week, by using the faker method <code>dateTimeBetween()<\/code>:<\/p>\n<pre><code class=\"language-php hljs php\" data-lang=\"php\">$factory-&gt;defineAs(App\\Working::class, <span class=\"hljs-string\">'this_week'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">(Faker\\Generator $faker)<\/span> <span class=\"hljs-title\">use<\/span> <span class=\"hljs-params\">($factory)<\/span> <\/span>{\n    $activity = $factory-&gt;raw(App\\Activity::class);\n    <span class=\"hljs-comment\">\/\/ Random datetime of the current week<\/span>\n    $startingDate = $faker-&gt;dateTimeBetween(<span class=\"hljs-string\">'this week'<\/span>, <span class=\"hljs-string\">'+6 days'<\/span>);\n    <span class=\"hljs-comment\">\/\/ Random datetime of the current week *after* `$startingDate`<\/span>\n    $endingDate   = $faker-&gt;dateTimeBetween($startingDate, strtotime(<span class=\"hljs-string\">'+6 days'<\/span>));\n\n    <span class=\"hljs-keyword\">return<\/span> array_merge($activity, [\n        <span class=\"hljs-string\">'starting_date'<\/span> =&gt; $startingDate,\n        <span class=\"hljs-string\">'ending_date'<\/span>   =&gt; $endingDate,\n    ]);\n});\n<\/code><\/pre>","summary":"Laravel provides a convenient way to seed your database with fake data for testing purpose using a system of factories for your Eloquent models.\nvh\nThis is a demonstration of how to create a factory for a model that is using starting and ending dates, like a Workshop model for example.\nFirst we need a starting date that would be useful for the tests: one that\u2019s not already past and yet not too far...","date_published":"2016-01-04T15:06:19+01:00","date_modified":"2016-01-04T15:06:48+01:00"},{"id":"9","title":"Search for your github stars with Alfred","url":"https:\/\/www.glutendesign.com\/posts\/search-for-your-github-stars-with-alfred","content_text":"As part of my web development activity I find myself constantly referencing the Github repositories of libraries used in my current project. I also got into the habit of starring every repo that I\u2019m using as it is quite convenient to filter out the bazilion Github repositories by going to the dedicated [Your stars page](https:\/\/github.com\/stars).\r\n\r\nAs an avid user of [Alfred](https:\/\/www.alfredapp.com), the next logical step is to make use of the awesome [Web search feature](https:\/\/www.alfredapp.com\/help\/features\/web-search\/): go to Preferences > Features > Web Search, add a new custom search and paste this snippet: `https:\/\/github.com\/stars?utf8=\u2713&q={query}`. Now you\u2019re just a few keys away from the repository you\u2019re looking for. Neat.","content_html":"<p>As part of my web development activity I find myself constantly referencing the Github repositories of libraries used in my current project. I also got into the habit of starring every repo that I\u2019m using as it is quite convenient to filter out the bazilion Github repositories by going to the dedicated <a href=\"https:\/\/github.com\/stars\">Your stars page<\/a>.<\/p>\n<p>As an avid user of <a href=\"https:\/\/www.alfredapp.com\">Alfred<\/a>, the next logical step is to make use of the awesome <a href=\"https:\/\/www.alfredapp.com\/help\/features\/web-search\/\">Web search feature<\/a>: go to Preferences &gt; Features &gt; Web Search, add a new custom search and paste this snippet: <code>https:\/\/github.com\/stars?utf8=\u2713&amp;q={query}<\/code>. Now you\u2019re just a few keys away from the repository you\u2019re looking for. Neat.<\/p>","summary":"As part of my web development activity I find myself constantly referencing the Github repositories of libraries used in my current project. I also got into the habit of starring every repo that I\u2019m using as it is quite convenient to filter out the bazilion Github repositories by going to the dedicated Your stars page.\nAs an avid user of Alfred, the next logical step is to make use of the awesome...","date_published":"2016-01-20T15:56:14+01:00","date_modified":"2016-01-20T15:56:14+01:00"},{"id":"10","title":"Drupal Code Linting with Sublime Text, PHPStorm and VSCode","url":"https:\/\/www.glutendesign.com\/posts\/drupal-code-linting-with-sublime-text-phpstorm-and-vscode","content_text":"## Using ZSH?\r\n\r\nCreate a `.zprofile` with this: `export PATH=$HOME\/.composer\/vendor\/bin:$PATH`\r\n\r\n\r\n## Getting CodeSniffer and Drupal Coder\r\n\r\n- Install code sniffer v2.7 globally with composer (see [this issue](https:\/\/www.drupal.org\/node\/2809335)): `composer global require squizlabs\/php_codesniffer`\r\n\r\n- Install drupal\/coder globally with composer: `composer global require drupal\/coder`\r\n\r\n- Register the Drupal and DrupalPractice Standard with PHPCS: `phpcs --config-set installed_paths ~\/.composer\/vendor\/drupal\/coder\/coder_sniffer`\r\n\r\n\r\n## Sublime Text configuration\r\n\r\n- Get SublimeLinter using [Package Control](https:\/\/packagecontrol.io\/): <kbd>\u2318<\/kbd> + <kbd>P<\/kbd> `Package Control: Install Package` and then `SublimeLinter`\r\n\r\n- Get SublimeLinter-phpcs using [Package Control](https:\/\/packagecontrol.io\/): <kbd>\u2318<\/kbd> + <kbd>P<\/kbd> `Package Control: Install Package` and then `SublimeLinter-phpcs`\r\n\r\n- You can set the standard globally in your SublimeLinter Settings (Preferences > Package Settings > Sublime Linter > Settings) or per project in your `.sublime-project` file:\r\n\r\n```js\r\n    \"SublimeLinter\": {\r\n        \"linters\": {\r\n            \"phpcs\": {\r\n                \"standard\": \"Drupal\"\r\n            }\r\n        }\r\n    }\r\n```\r\n    \r\n\r\n## PHP Storm configuration\r\n\r\nOfficial instructions [here](https:\/\/www.jetbrains.com\/help\/phpstorm\/php-code-sniffer.html).\r\n\r\n* Under `Preferences > Languages & Frameworks > PHP > Code Sniffer` select \u201cLocal\u201d for the phpcs script path and paste the exact path to your local installation: (on macOS it would like this: `\/Users\/yourusername\/.composer\/vendor\/squizlabs\/php_codesniffer\/scripts\/phpcs`)\r\n\r\n* Open and check `Preferences > Editor > Inspections > PHP Code Sniffer validation`\r\n\r\n* From the right pane select the Coding standard drop-down list, choose \u201cCustom\u201d\r\n and click the Browse button\r\n\r\n* In the Custom Coding Standard dialog box that opens, specify the path to the root directory of your own coding standard in the Root directory, something like: `\/Users\/yourusername\/.composer\/vendor\/drupal\/coder\/coder_sniffer\/Drupal\/ruleset.xml`\r\n\r\n\r\n## VSCode Configuration\r\n\r\n- Install the [phpcs extension](https:\/\/marketplace.visualstudio.com\/items?itemName=ikappas.phpcs)\r\n\r\n- In the preferences search for \u201cphpcs\u201d and then set the standard to \u201cDrupal\u201d\r\n","content_html":"<h2>Using ZSH?<\/h2>\n<p>Create a <code>.zprofile<\/code> with this: <code>export PATH=$HOME\/.composer\/vendor\/bin:$PATH<\/code><\/p>\n<h2>Getting CodeSniffer and Drupal Coder<\/h2>\n<ul>\n<li>\n<p>Install code sniffer v2.7 globally with composer (see <a href=\"https:\/\/www.drupal.org\/node\/2809335\">this issue<\/a>): <code>composer global require squizlabs\/php_codesniffer<\/code><\/p>\n<\/li>\n<li>\n<p>Install drupal\/coder globally with composer: <code>composer global require drupal\/coder<\/code><\/p>\n<\/li>\n<li>\n<p>Register the Drupal and DrupalPractice Standard with PHPCS: <code>phpcs --config-set installed_paths ~\/.composer\/vendor\/drupal\/coder\/coder_sniffer<\/code><\/p>\n<\/li>\n<\/ul>\n<h2>Sublime Text configuration<\/h2>\n<ul>\n<li>\n<p>Get SublimeLinter using <a href=\"https:\/\/packagecontrol.io\/\">Package Control<\/a>: <kbd>\u2318<\/kbd> + <kbd>P<\/kbd> <code>Package Control: Install Package<\/code> and then <code>SublimeLinter<\/code><\/p>\n<\/li>\n<li>\n<p>Get SublimeLinter-phpcs using <a href=\"https:\/\/packagecontrol.io\/\">Package Control<\/a>: <kbd>\u2318<\/kbd> + <kbd>P<\/kbd> <code>Package Control: Install Package<\/code> and then <code>SublimeLinter-phpcs<\/code><\/p>\n<\/li>\n<li>\n<p>You can set the standard globally in your SublimeLinter Settings (Preferences &gt; Package Settings &gt; Sublime Linter &gt; Settings) or per project in your <code>.sublime-project<\/code> file:<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-js hljs javascript\" data-lang=\"js\">    <span class=\"hljs-string\">\"SublimeLinter\"<\/span>: {\n        <span class=\"hljs-string\">\"linters\"<\/span>: {\n            <span class=\"hljs-string\">\"phpcs\"<\/span>: {\n                <span class=\"hljs-string\">\"standard\"<\/span>: <span class=\"hljs-string\">\"Drupal\"<\/span>\n            }\n        }\n    }\n<\/code><\/pre>\n<h2>PHP Storm configuration<\/h2>\n<p>Official instructions <a href=\"https:\/\/www.jetbrains.com\/help\/phpstorm\/php-code-sniffer.html\">here<\/a>.<\/p>\n<ul>\n<li>\n<p>Under <code>Preferences &gt; Languages &amp; Frameworks &gt; PHP &gt; Code Sniffer<\/code> select \u201cLocal\u201d for the phpcs script path and paste the exact path to your local installation: (on macOS it would like this: <code>\/Users\/yourusername\/.composer\/vendor\/squizlabs\/php_codesniffer\/scripts\/phpcs<\/code>)<\/p>\n<\/li>\n<li>\n<p>Open and check <code>Preferences &gt; Editor &gt; Inspections &gt; PHP Code Sniffer validation<\/code><\/p>\n<\/li>\n<li>\n<p>From the right pane select the Coding standard drop-down list, choose \u201cCustom\u201d\nand click the Browse button<\/p>\n<\/li>\n<li>\n<p>In the Custom Coding Standard dialog box that opens, specify the path to the root directory of your own coding standard in the Root directory, something like: <code>\/Users\/yourusername\/.composer\/vendor\/drupal\/coder\/coder_sniffer\/Drupal\/ruleset.xml<\/code><\/p>\n<\/li>\n<\/ul>\n<h2>VSCode Configuration<\/h2>\n<ul>\n<li>\n<p>Install the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ikappas.phpcs\">phpcs extension<\/a><\/p>\n<\/li>\n<li>\n<p>In the preferences search for \u201cphpcs\u201d and then set the standard to \u201cDrupal\u201d<\/p>\n<\/li>\n<\/ul>","summary":"Using ZSH?\nCreate a .zprofile with this: export PATH=$HOME\/.composer\/vendor\/bin:$PATH\nGetting CodeSniffer and Drupal Coder\n\n\nInstall code sniffer v2.7 globally with composer (see this issue): composer global require squizlabs\/php_codesniffer\n\n\nInstall drupal\/coder globally with composer: composer global require drupal\/coder\n\n\nRegister the Drupal and DrupalPractice Standard with PHPCS: phpcs --conf...","date_published":"2018-02-05T11:15:11+01:00","date_modified":"2018-02-05T11:15:11+01:00"}]}