2020/09/05

[Laravel] Laravel 8 搶先玩

markdown Laravel 8 即將在 September 8, 2020 釋出! 今天就搶先來玩一下囉~ #### 更新 Laravel Installer 先來把 Laravel Installer 更新到最新版本 4.0.0 ### 方法 1: 直接修改本機全域的 `composer.json` 把 `laravel/installer` 的版本改成 "^4.0" ```json { "require": { ... "laravel/installer": "^4.0" } } ``` 再執行全域更新 ``` composer global update ``` ### 法方 2: 移除再重裝 Laravel Installer 先移除 ``` composer global remove laravel/installer ``` 再安裝最新版本 ``` composer global require laravel/installer ``` 確認版本是否為新版 ``` laravel -v ```
#### Laravel 8 我們來用剛剛新裝的 Laravel Installer 來建立 Laravel 8 專案吧! 因為目前還沒正式 Release, 所以先來裝 dev 版本 暫時先把專案名稱命名為 lv8-dev ``` laravel new lv8-dev --dev ```
新版本安裝畫面變得不一樣囉~ 有 Laravel 圖文字樣煥然一新! 再來看一下 Laravel 8 的新呈現畫面 (本範例使用 Valet 開發環境, 若以指令瀏覽結果,請輸入 `php artisan serve`)

2020/08/23

[macOS] Mac 解決 gyp: No Xcode or CLT version detected! 的問題

markdown ### 環境 - macOS Catalina 版本 10.15.6 - Node v14.5.0 - npm v6.14.5 ### 問題 最近換新電腦,在開新專案執行 npm install 時出現下列訊息 ``` No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'. No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'. No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'. gyp: No Xcode or CLT version detected! gyp ERR! configure error gyp ERR! stack Error: `gyp` failed with exit code: 1 ``` ### 解決方法 參考此文:[Installation notes for macOS Catalina (v10.15)](https://github.com/nodejs/node-gyp/blob/master/macOS_Catalina.md) ``` sudo rm -rf $(xcode-select -print-path) ``` 執行過程它會重新安裝 command line tools 完成安裝後就行了 參考連結中還有其他指令,但我僅執行這行就成功了,若有朋友也遇到相同問題,可以參考接下來的動作 ``` xcode-select --install ```

2020/08/05

[.NET Core] 如何移除不需要的 .NET Core SDK 版本

markdown ### 前言 開發環境日積月累,安裝了不少 SDK 版本,用不到也很佔空間,於是到了該清理的時候
目前我的機器存在的版本
### Environment - Windows 10 ### 安裝 dotnet-core-unistall 工具 - 下載 msi 檔案: https://github.com/dotnet/cli-lab/releases - .NET Core Uninstall Tool 文件: https://aka.ms/dotnet-core-uninstall-docs - DotNet Core Uninstall v1.1.122401 安裝完 .NET Core Uninstall Tool 後,開啟 PowerShell 切換到 `C:\Program Files (x86)\dotnet-core-uninstall` 目錄 ``` cd "C:\Program Files (x86)\dotnet-core-uninstall" ``` 列出可移除的 dotnet core 版本 ``` .\dotnet-core-uninstall list ``` 有被 Visual Studio 使用的版本也會標記出來,這部分我要保留版本號的資訊
#### dry-run / whatif 在實際移除前,可用 dry-run 或 whatif 來看一下執行後會移除的版本 ``` .\dotnet-core-uninstall dry-run [option] [VERSION] .\dotnet-core-uninstall whatif [option] [VERSION] ``` 移除僅特定版本 SDK
移除除了指定版本外的 SDK
把 dry-run 或 whatif 改成 remove 則會真的執行移除動作 (PowerShell 需使用系統管理員身分執行) 因為我要移除掉 Visual Studio 有使用以外的版本所以列出要保留的版本號,並加上 `--all-but` 的選項 ``` .\dotnet-core-uninstall remove --sdk --all-but 2.1.202 2.1.513 2.1.802 2.2.104 ``` 刪除版本之前,也會再詢問是否確定要移除,執行後就會列出一個個被移除的版本
清掉之後僅留下的 SDK 版本
重新釋放掉不少空間
### References - [How to remove the .NET Core Runtime and SDK](https://docs.microsoft.com/zh-tw/dotnet/core/install/remove-runtime-sdk-versions?tabs=windows&pivots=os-windows&WT.mc_id=DOP-MVP-5002629) - [.NET Core Uninstall Tool](https://docs.microsoft.com/zh-tw/dotnet/core/additional-tools/uninstall-tool?tabs=windows&WT.mc_id=DOP-MVP-5002629)

2020/06/15

[PHP] 解決執行 Composer 出現 PHP Fatal error: Allowed memory size exhausted 的問題

markdown ### 問題 今天在執行 composer require 套件安裝時出現 PHP Fatal error: Allowed memory size of xxx bytes exhausted 的錯誤訊息 ### 解決方法 1. 在 `php.ini` 加大 `memory_limit` 或是設 `memory_limit = -1` (不限制) 2. 使用命令 `php -d memory_limit=-1 composer.phar` ### 參考連結 - [Composer trouble shooting: memory-limit-errors](https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors)

2020/06/14

[Laravel] 解決 Sqlite 在 migration 退版時發生不支援 dropColumn 或 renameColumn 多重呼叫的問題

markdown ### 問題 在 Laravel 使用 Sqlite 為資料庫使用, 執行 migration rollback 的時候發生下列錯誤訊息: SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification. ### 發生原因 在 migration 的 down() 方法裡使用多個 dropColumn 但 Sqlite 不支援
$table->dropColumn('field1');
$table->dropColumn('field2');
### 解決方法 用 array() 方式來改寫 dropColumn 即可
$table->dropColumn([
    'field1',
    'field2',
]);
### 參考連結 - [Database: Migrations -- Dropping Columns](https://laravel.com/docs/7.x/migrations#dropping-columns)

2020/05/06

[CSS] 列印網頁強制換頁設定

markdown ### 換頁的 CSS 相關設定 ```css page-break-before: auto|always|avoid|left|right|initial|inherit; page-break-after: auto|always|avoid|left|right|initial|inherit; page-break-inside: auto|avoid|initial|inherit; ``` 在標籤前或後換頁 ```css @media print { /* 在標籤前換頁 */ h1 { page-break-before: always; } /* 在標籤後換頁 */ footer { page-break-after: always; } } ``` 避免在文中換頁 `page-break-inside: avoid;` ```css @media print { /* 避免標籤內換頁 */ pre, blockquote { page-break-inside: avoid; } } ``` ### References - https://www.w3schools.com/cssref/pr_print_pageba.asp

2020/04/10

[EF] 使用 SqlFunctions.DatePart 解決 DataTime 日期格式化問題

markdown ### 環境 - Entity Framework 6 ### 問題 在 EF 要處理日期時間的特定格式,不能用 DateTime.ToString() 的方式來處理 例如: ``` var orders = context.Orders.Where(x => x.OrderDate.ToString("yyyy-MM").Equals("2020-04")).ToList(); ``` 執行會出現錯誤 ``` System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression. ``` 因為 ToString() 是 C# 程式語法, SQL 不認識它是正常的 ### 解決方法 透用 [DbFunctions](https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbfunctions?view=entity-framework-6.2.0&WT.mc_id=DOP-MVP-5002629) 或 [SqlFunctions](https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.sqlserver.sqlfunctions?view=entity-framework-6.2.0&WT.mc_id=DOP-MVP-5002629) 來處理日期格式問題 以下是採用 SqlFunctions.DatePart() 來取得我要的日期部分 ``` var orders = db.Orders.Where(x => SqlFunctions.DatePart("year", x.OrderDate) == 2020 && SqlFunctions.DatePart("month", x.OrderDate) == 4 ).ToList(); ``` 常用的 datepart 參數: `year`, `month`, `day`, `week`, `hour`, `minute` 詳細的 DatePart 可以查詢 [SQL 文件: DATEPART](https://docs.microsoft.com/zh-tw/sql/t-sql/functions/datepart-transact-sql?view=sql-server-ver15&WT.mc_id=DOP-MVP-5002629) ### 參考連結 - [DbFunctions](https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbfunctions?view=entity-framework-6.2.0&WT.mc_id=DOP-MVP-5002629) - [SqlFunctions](https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.sqlserver.sqlfunctions?view=entity-framework-6.2.0&WT.mc_id=DOP-MVP-5002629) - [SqlFunctions.DatePart Method](https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.sqlserver.sqlfunctions.datepart?view=entity-framework-6.2.0&WT.mc_id=DOP-MVP-5002629) - [SQL 文件 DATEPART](https://docs.microsoft.com/zh-tw/sql/t-sql/functions/datepart-transact-sql?view=sql-server-ver15&WT.mc_id=DOP-MVP-5002629) - [Linq Convert DateTime? to DateTime in ("dd/MM/yyyy")](https://forums.asp.net/t/1854597.aspx) - [EF DateTime格式化](https://xbuba.com/questions/51970339)