文章

顯示從 2月, 2024 起發佈的文章

[laravel] Steps of create api of model

 php artisan make:model Blog -mcr edit migration file php artisan migrate edit "routes/api.php" Route::resource('blogs', BlogController::class); edit Models/Blog.php protected $fillable = [         'title',         'body',     ]; edit Controller (index,store,show,update,destroy)

[laravel] use 'OR' middleware for route (2 roles or~)

To allow both roles to access the API using the OR logic, you can use the middleware('restrictRole:admin,admin2') method.

Laravel(Migrations)timestamp 和dateTime

 Laravel資料遷移(Migrations)中提供了幾種創建時間的幫助函數,這裡說下他們的不同點。 $table->timestamp('published_at'); $table->dateTime('published_at'); $table->date('published_at'); $table->timestamps(); timestamp () 和dateTime () timestamp 和dateTime 類似,他們儲存了日期(YYYY-MM-DD) 加上時間(HH:MM:SS) ,如:YYYY-MM-DD HH:MM:SS。 不同的是timestamp 可以利用MySQL 的CURRENT_TIMESTAMP 方法,從資料庫層面去維護created_at 和updated_at ,參考Illuminate/Database 原始碼中MYSQL 建立Timestamp 類型欄位程式碼。 要注意的是,timestamp 的資料範圍是1970-01-01 00:00:01 UTC ~ 2038-01-19 03:14:07 UTC ,也就是說大於2038-01-19 03:14:07 的值資料庫是無法儲存的,會變成0000-00-00 00:00:00,這也是MySQL 知名的 2038 年問題。 dateTime的儲存範圍是1000-01-01 00:00:00 ~ 9999-12-31 23:59:59,所以,在選擇欄位的儲存類型的時候,請仔細考慮下用例。 date() 只是儲存了日期格式,如:YYYY-MM-DD (1000-00-01~ 9999-12-31) timestamps() 建立了created_at 和updated_at 字段,格式為timestamp。 【番外篇】更改timestamps 為dateTime 既然timestamps 有 2038 年問題,你想使用dateTime 的話,在Laravel 中,你只需要在創建的時候,指定為dateTime 類型即可: $table->dateTime('created_at'); $table->dateTime('updated_at'); Larave...

[laravel 9] rename project

  Answers Sorted by:                                                Highest score (default)                                                                     Trending (recent votes count more)                                                                     Date modified (newest first)                        ...