Laravel Command

Tools

Lang

  • Laravel Lang
  • composer require caouecs/laravel-lang:~3.0
  • second way to change language is to put json language file to lang folder

Services

  • Auth
    • attempt(): to login and register session data. Verify email and unencrypted password
    • logout()

Migration

1
2
3
4
* php artisan make:migration UserTestTable
* php artisan make:seeder UserTestSeeder
* php artisan make:model UserTestSeeder
* php artisan migrate:refresh --model=UserTestSeeder --seed

FORM CRUD

  • @csrf @method(‘DELETE’): filter request
  • csrf
  • php artisan make:policy PostPolicy: to avoid not authorize user to delete
  • using @can(‘delete’, $user) to toggle delete button
  • @csrf @method(‘PUT’): fake update method using put instead
  • Method Field

Helpers

Artisan Command

1
2
3
4
5
php artisan make:controller IndexController
php artisan make:controller UserController --resource
php artisan route:list
php artisan migrate
php artisan make:policy --model=User UserPolicy

Session Driver

  • change .env session_driver to session
    • laravel will create sessoin table to record user login message
    • once you change the driver type, you need to refresh all record to get the correct result
1
2
php artisan session:table
php artisan migrate:refresh --seed

Create App Key & Smtp Setting

smtp gmail setting

1
2
php artisan key:generate
php artisan make:mail RegMail

create factory data

1
2
3
// manually added records
php artisan tinker
factory(App\User::class, 20)->create();
1
2
3
4
5
6
// execute by db:seed
php artisan make:seeder UsersSeeder
// rewrite the DatabaseSeeder.php run method by using factory
// factory(App\User::class, 20)->create();
php artisan db:seed
php artisan migrate:refresh --seed

Using Policy

  • create policy
    • php artisan make:policy --model=user UserPolicy
  • AuthServiceProvider add Policy
    • ‘App\User’ => UserPolicy::class,
    • remember to add namespace before using this policy
  • authorize controller destroy method to execute delete method
    • $this->authorize(‘delete’, $user);

Notice

  • routes/web.php
  1. use get to access controller function
    • alias name for changing back to home page
  2. use resource to create default CRUD method
    • no need to create additional view blade files
  3. laravel 5.4 support emoji for MySQL 5.7.7
    • every time start the app
    • Providers\AppServiceProvider.php
    • config\app.php
1
2
Route::get('/', 'IndexController@home')->name('home');
Route::resource('user', 'UserController');
1
2
3
4
public function boot()
{
Schema::defaultStringLength(191);
}