重複使用query builder
https://stackoverflow.com/questions/30235551/laravel-query-builder-re-use-query-with-amended-where-statement使用 clone
$oQuery = Model::where('is_test', '=', '0')->groupBy('name');
$oQuery1 = clone $oQuery;
$oQuery1Results = $oQuery1->where('content','like','%2%')->get();
$oQuery2 = clone $oQuery;
$oQuery2Results = $oQuery2->where('content','like','%0%')->get();
安裝Start Bootstrap Admin 2( SB Admin 2 )
https://startbootstrap.com/template-overviews/sb-admin-2/Step 1. 安裝新的Laravel
$ composer create-project --prefer-dist laravel/laravel blog
Step 2. 下載 SB Admin
Download SB Admin Theme
Point 1. 新建 public/theme/ 目錄
Point 2. 將SB Admin解壓縮出來的data, dist, vendor目錄放到theme目錄
Step 3. 生成路由
routes/web.php 新增
Route::get('my-home', 'HomeController@myHome');
Route::get('my-users', 'HomeController@myUsers');
Step 4. 新增控制器
app/Http/Controllers/HomeController.php
(內容見上面教程)
Step 5. 設定theme 的blade檔案
新建 resources/views/theme/ 目錄,並生成三個文件
resources/views/theme/default.blade.php
resources/views/theme/header.blade.php
resources/views/theme/sidebar.blade.php
(內容見上面教程)
ps . <script src="{!! asset('theme/data/morris-data.js') !!}"></script> 必須有畫圖表再加載,否則會報js錯誤
Step 6. 使用theme
新建下面兩個檔案
resources/views/myHome.blade.php
resources/views/myUsers.blade.php
(內容見上面教程)
生成.env的APP_KEY
$ php artisan key:generate名詞
Accessor - 訪問器 => getFirstNameAttribute()Mutator - 修改器 => setFirstNameAttribute()
guards - 看守器
providers - 提供器
用戶認證(Authentication)
https://laravel-china.org/docs/laravel/5.7/authentication/2269https://laravel.com/docs/5.7/authentication
快速生成身分驗證所需的路由和視圖
$ php artisan make:auth
打開註冊頁
http://laravel.localhost/login
點Register註冊一個新帳號,users.password 將會以Hash儲存,然後就能以此新帳號登入
使用username登入
https://laracasts.com/discuss/channels/laravel/how-to-use-authentication-using-username-instead-of-email
app/Http/Controllers/Auth/LoginController.php 新增
public function username(){
return 'username';
}
resources/views/auth/login.blade.php 前端也要做處理,以免被input.type=email擋住
使用email或username登入
https://stackoverflow.com/questions/42708942/allow-login-using-username-or-email-in-laravel-5-4
app/Http/Controllers/Auth/LoginController.php
public function username() {
$login = request()->input('login');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$field => $login]);
return $field;
}
讓Ardent能夠使用Authenticatable
https://stackoverflow.com/questions/46419933/laravel-ardent-with-authenticatable-user-model/46421896#46421896
建議參考 Illuminate/Foundation/Auth/User 的User類別的實作方式到你的model上,然後繼承Ardent
使用Tinker
https://www.sunzhongwei.com/using-laravel-tinker-for-laravel-admin-add-background-administratorTinker(修補匠) - 與你的laravel應用程式互動(Interact with your application)
$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.1.7 — cli) by Justin Hileman
>>> $user = new App\Models\User();
=> App\Models\User {#3116}
>>> $user->password = Hash::make('1234qwer');
=> "$2y$10$KRgwZyc8j.3rgE0kBMW/8usLnsc3DGuZ6GtT3JiI1zgsBlxstq8RG"
>>> $user->email = 'tinker@example.com';
=> "tinker@example.com"
>>> $user->save();
=> true
可以直接在命令行執行laravel
不同網站同一個專案
https://stackoverflow.com/questions/40604788/multiple-websites-with-one-laravel-installation
Route::group(['domain' => 'example.com')], function () {
// All routes for the first domain go here.
}
多個域名走同一個路由
https://stackoverflow.com/questions/18603330/can-i-group-multiple-domains-in-a-routing-group-in-laravel
$appRoutes = function() {
Route::get('/',function(){
...
});
};
Route::group(array('domain' => 'app.example.com'), $appRoutes);
Route::group(array('domain' => 'dev.app.example.com'), $appRoutes);
子域名路由不作用
https://stackoverflow.com/questions/41703192/laravel-subdomain-routing-is-not-working
因為路由有先後順序(先來先服務),所以子域名同路徑的路由必須寫在前面
Route::group(['domain' => 'admin.localhost'], function () {
Route::get('/', function () {
return "This will respond to requests for 'admin.localhost/'";
});
});
Route::get('/', function () {
return "This will respond to all other '/' requests.";
});
下面例子子域名路由不作用
Route::get('/', function () {
return "This will respond to all '/' requests before the route group gets processed.";
});
Route::group(['domain' => 'admin.localhost'], function () {
Route::get('/', function () {
return "This will never be called";
});
});
在路由中動態加載域名
https://stackoverflow.com/questions/34501476/laravel-5-routes-group-domains-from-rows-in-database
這邊可以使用Eloquent
$domains = \App\Domain::where('status', '1')->get()->pluck('email');
foreach ($domains as $domain) {
Route::group(['domain' => $domain], function () {
Route::get('/{linkName}', 'RetargetController@retarget');
Route::get('/', function () {
abort(404);
});
});
}
php artisan make:auth 與Ardent的衝突
laravel 官方驗證 php artisan make:auth後會生成 Http/Controllers/Auth/RegisterController.php 其中的創建用戶方法create()是這麼寫的
return User::create([
...
'password' =>
如果你的User已經繼承了Ardent,而且 autoHashPasswordAttributes 了 password這個欄位,password將會被Hash兩次造成登入不了的bug... 這時候需在 RegisterController::create()中取消password字段的Hash