# Laravel :::warning [TOC] ::: > 當前開發環境 > Laravel 8 > Win10 > VScode、phpstorm ## 隱藏Laravel資訊 session 將`.env`的`APP_NAME`改為你的app名稱(不要叫laravel都好) [參考](https://laravel-tricks.com/tricks/how-to-hide-laravel-from-wappalyzer-plugin) ## Model **定義與其他model的關係** ```php public function taskOrder(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(TaskOrder::class); } ``` **定義可被呼叫的成員變數**:get==CanOrder==Attribute ```php public function getCanOrderAttribute(): bool { return $this->is_open === 1 ?? false; } ... // in use $task->can_order ``` ## 登入者操作 ```php use Illuminate\Support\Facades\Auth; // 取得目前登入者的user物件... $user = Auth::user(); // 取得目前登入者的userid... $id = Auth::id(); ``` [參考官網](https://laravel.com/docs/8.x/authentication) ## 轉址 `redirect()` :::success ## DB指令 新增table ``` artisan make:migration create_tasks_table ``` 對已有的table操作 ``` artisan make:migration add_email_to_users_table ``` ::: ## 在新裝置啟用已有專案 [參考](https://campus-xoops.tn.edu.tw/modules/tad_book3/page.php?tbdsn=1255) 1. 利用composer安裝php相依套件 ``` composer install ``` 2. 利用npm安裝js相依套件 ``` npm install ``` 3. 還原.env設定檔 ``` cp .env.example .env ``` 4. 產生新的app key ``` php artisan key:generate ``` 5. 若有資料庫 ``` php artisan migrate ``` 若有可以匯入的fake data ``` php artisan migrate --seed ``` 6. 若有public 資料夾的資料要連結storage ```php php artisan storage:link ``` 可能會出現這錯誤 `symlink () has been disabled for security reasons` 在`php.ini`的`disable_functions`找到`symlink`將它刪除後重啟php 再次執行 ```php php artisan storage:link ``` [參考](https://learnku.com/laravel/t/36212) ## [切換時區](https://stackoverflow.com/questions/46423806/laravel-changing-timezone-not-reflecting-the-correct-time) 1. 修改config/app.php `'timezone' => 'Asia/Taipie'` 1. 執行下列三個指令 `php artisan cache:clear` `php artisan config:clear` `php artisan route:clear` 1. 完成 ## 安裝、使用Bootstrap 4 安裝 Laravel/ui ``` composer require laravel/ui ``` 使用 Laravel/ui 安裝 bootstrap ``` php artisan ui bootstrap ``` > 若需要直接使用到auth的畫面則 > ``` > php artisan ui bootstrap --auth > ``` 之後執行 ``` npm install && npm run dev ``` 在畫面引入 ```htmlembedded <head> <!-- Scripts --> <script src="{{ asset('js/app.js') }}" ></script> <!-- Styles --> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> </head> ``` ## Blade語法 ## 查看版本 ``` php artisan --version ``` ## 自訂User 註冊時的欄位 修改`Models\User.php` 和修改`app\Actions\Fortify\CreateNewUser.php` [參考](https://stackoverflow.com/questions/64298872/laravel-8-jetstream-adding-new-field-to-the-registration-process) ## 資料庫 Database: Migrations ### 執行遷移 ```php php artisan migrate ``` ### 清空資料表(含刪除資料表) ```php php artisan migrate:reset ``` > 通常清空完會搭配`php artisan migrate`重新建置起資料表 ## 使用Seeder 清除資料並使用seeder ``` php artisan migrate:fresh --seed ``` ## 清除快取 ### 清除==所有==快取 `php artisan optimize:clear` ### 清除設定快取 `php artisan config:clear` ### 清除畫面快取 `php artisan view:clear` ### 清除基本快取 `php artisan cache:clear` ### 清除路由快取(Route cache) `php artisan route:cache` ### 設定快取 `php artisan config:cache` ## 其他補充 ### [命名規則](https://webdevetc.com/blog/laravel-naming-conventions/#section_blade-view-files) ## 部屬 ### composer install會報錯 > 缺fileinfo、以及刪除被關閉的function(putenv、proc_open) - 被關閉的function ![](https://i.imgur.com/U89uZMg.png) 這裡找被關閉的function 刪掉即可 ![](https://i.imgur.com/6t8uaXN.png) - fileinfo ![](https://i.imgur.com/vBdagSo.png) ![](https://i.imgur.com/0bfkF0Y.png) ### 404了嗎 在nginx這個`index index.php index.html index.htm default.php default.htm default.html;`的下面加上 ``` location / { try_files $uri $uri/ /index.php?$query_string; } ``` [參考](https://stackoverflow.com/questions/64755730/laravel-8-routes-not-working-on-vps-using-aapanel-as-control-panel) ### laravel.log無法寫入? ``` sudo chown -R $USER:www storage sudo chown -R $USER:www bootstrap/cache ``` [參考](https://stackoverflow.com/questions/23411520/how-to-fix-error-laravel-log-could-not-be-opened) ## 加上swagger api https://hosomikai.medium.com/laravel-%E4%BD%BF%E7%94%A8swagger-%E7%94%A2%E5%87%BAapi%E6%96%87%E4%BB%B6-2f2a934c3b25