Laravel

當前開發環境
Laravel 8
Win10
VScode、phpstorm

隱藏Laravel資訊

session
.envAPP_NAME改為你的app名稱(不要叫laravel都好)
參考

Model

定義與其他model的關係

public function taskOrder(): \Illuminate\Database\Eloquent\Relations\HasMany
{
    return $this->hasMany(TaskOrder::class);
}

定義可被呼叫的成員變數:getCanOrderAttribute

public function getCanOrderAttribute(): bool
{
    return $this->is_open === 1 ?? false;
}
...
// in use 
$task->can_order

登入者操作

use Illuminate\Support\Facades\Auth;

// 取得目前登入者的user物件...
$user = Auth::user();

// 取得目前登入者的userid...
$id = Auth::id();

參考官網

轉址

redirect()

DB指令

新增table

artisan make:migration create_tasks_table

對已有的table操作

artisan make:migration add_email_to_users_table

在新裝置啟用已有專案

參考

  1. 利用composer安裝php相依套件
composer install
  1. 利用npm安裝js相依套件
npm install
  1. 還原.env設定檔
cp .env.example .env
  1. 產生新的app key
php artisan key:generate
  1. 若有資料庫
php artisan migrate

若有可以匯入的fake data

php artisan migrate --seed
  1. 若有public 資料夾的資料要連結storage
php artisan storage:link

可能會出現這錯誤
symlink () has been disabled for security reasons
php.inidisable_functions找到symlink將它刪除後重啟php
再次執行

php artisan storage:link

參考

切換時區

  1. 修改config/app.php
    'timezone' => 'Asia/Taipie'

  2. 執行下列三個指令
    php artisan cache:clear
    php artisan config:clear
    php artisan route:clear

  3. 完成

安裝、使用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

在畫面引入

<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

參考

資料庫 Database: Migrations

執行遷移

php artisan migrate

清空資料表(含刪除資料表)

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

其他補充

命名規則

部屬

composer install會報錯

缺fileinfo、以及刪除被關閉的function(putenv、proc_open)

  • 被關閉的function


    這裡找被關閉的function 刪掉即可

  • fileinfo


404了嗎

在nginx這個index index.php index.html index.htm default.php default.htm default.html;的下面加上

location / {
      try_files $uri $uri/ /index.php?$query_string;
}

參考

laravel.log無法寫入?

sudo chown -R $USER:www storage
sudo chown -R $USER:www bootstrap/cache

參考

加上swagger api

https://hosomikai.medium.com/laravel-使用swagger-產出api文件-2f2a934c3b25