Skip to content

Instantly share code, notes, and snippets.

@rseon
Last active April 19, 2019 09:42
Show Gist options
  • Save rseon/a6693d93dc6bdab8390016e4c51be30c to your computer and use it in GitHub Desktop.
Save rseon/a6693d93dc6bdab8390016e4c51be30c to your computer and use it in GitHub Desktop.

Memo installation Laravel 5.8

composer create-project --prefer-dist laravel/laravel projectname
cd projectname
composer require barryvdh/laravel-debugbar --dev
php artisan make:auth

Déplacement des models (https://medium.com/@codingcave/organizing-your-laravel-models-6b327db182f9):

  • Déplacer /app/User.php dans /app/Models/User.php (répertoire à créer)
  • Modifier /app/Models/User.php, /app/Http/Controllers/Auth/RegisterController.php, /config/auth.php, /config/services.php, /database/factories/UserFactory.php
  • Créer /app/Console/Commands/ModelMakeCommand.php (répertoire à créer)
  • Modifier /app/Providers/AppServiceProvider.php

Soft-delete (https://laravel.com/docs/5.8/eloquent#soft-deleting) + Last login IP / Date (https://stackoverflow.com/a/53479216/1811981 + https://laraveldaily.com/save-users-last-login-time-ip-address/)

  • Modifier /app/Models/User.php
  • Modifier /app/Providers/EventServiceProvider.php
  • Créer /app/Listeners/LogSuccessfulLogin.php (répertoire à créer)
  • Modifier /database/migrations/*_create_users_table.php

Laravel-Userstamps (https://github.com/WildSideUK/Laravel-Userstamps) :

  • composer require wildside/userstamps
  • Modifier /database/migrations/*_create_users_table.php
  • Modifier /app/Models/User.php

Login si user est actif (https://stackoverflow.com/a/31016210/1811981) :

  • Modifier /database/migrations/*_create_users_table.php
  • Modifier /app/Models/User.php
  • Modifier /app/Http/Controllers/Auth/LoginController.php
chown -R www-data:www-data .
php artisan migrate

Resources

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Waavi\Sanitizer\Laravel\SanitizesInput;
use Illuminate\Support\Facades\Auth;
/*
In your UserController :
use App\Http\Requests\UserRequest;
...
public function store(UserRequest $request)
{
// $request is validated AND sanitized :)
$user = User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
]);
}
*/
class UserRequest extends FormRequest
{
use SanitizesInput;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
// When user update his profile
if('/'.$this->route()->uri === route('user.profile.update', [], false)) {
return $this->rulesForProfile();
}
$user = $this->route('user');
$rules = [
'role' => 'required',
'name' => 'required|string|max:50',
'email' => 'required|email|unique:users',
];
// Update
if($user) {
$rules['email'] = 'required|email|unique:users,email,'.$user->email.',email';
}
else {
$rules['password'] = 'required|min:6|confirmed';
}
return $rules;
}
/**
* Get the filters that apply to the request.
*
* @return array
*/
public function filters(): array
{
return [
'name' => 'strip_tags',
'email' => 'escape|lowercase',
'is_active' => 'cast:boolean',
'role' => 'cast:integer',
];
}
/**
* Get the specific validation rules that apply to the request when user updates his profile.
*
* @return array
*/
protected function rulesForProfile()
{
$user = Auth::user();
return [
'name' => 'required|string|max:50',
'email' => 'required|email|unique:users,email,'.$user->email.',email',
];
}
}
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Wildside\Userstamps\Userstamps;
use Spatie\Permission\Traits\HasRoles;
use Carbon\Carbon;
class User extends Authenticatable
{
use Notifiable;
use SoftDeletes;
use Userstamps;
use HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'is_active',
'last_login_at', 'last_login_ip', // @link https://laraveldaily.com/save-users-last-login-time-ip-address/
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'is_admin' => 'boolean',
'is_active' => 'boolean',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'email_verified_at',
'last_login_at', // @link https://laraveldaily.com/save-users-last-login-time-ip-address/
];
/**
* Automatic date cast
*
* @param string
* @todo Set format auto from locale
*/
public function getLastLoginAtAttribute($value) {
return $value ? Carbon::parse($value)->format('d/m/Y H:i:s') : '';
}
/**
* Automatic date cast
*
* @param string
* @todo Set format auto from locale
*/
public function getDeletedAtAttribute($value) {
return $value ? Carbon::parse($value)->format('d/m/Y H:i:s') : '';
}
}
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use App\Console\Commands\ModelMakeCommand;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
// @link https://medium.com/@codingcave/organizing-your-laravel-models-6b327db182f9
$this->app->extend('command.model.make', function ($command, $app) {
return new ModelMakeCommand($app['files']);
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// @link https://laravel-news.com/laravel-5-4-key-too-long-error
Schema::defaultStringLength(191);
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->datetime('last_login_at')->nullable();
$table->string('last_login_ip')->nullable();
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->softDeletes();
$table->unsignedInteger('created_by')->nullable();
$table->unsignedInteger('updated_by')->nullable();
$table->unsignedInteger('deleted_by')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment