相關系列文章:
涵蓋主題如下:
一、Query Scopes:打造更乾淨且可重複使用的查詢邏輯
二、Soft Deletes:進行軟刪除並可還原資料
三、CRUD(Read):用 Eloquent 讀取資料
四、Table Filters:動態搜尋與條件篩選
五、Pagination:最佳化資料瀏覽與分頁
六、CRUD(Create):建立資料
七、CRUD(Update):更新資料
八、CRUD(Delete):刪除資料
一、Query Scopes:打造更乾淨且可重複使用的查詢邏輯
觀念:Query Scope(查詢作用域) 是 Laravel Eloquent 提供的一種功能,用來把常用的查詢條件封裝起來,讓你的查詢程式碼更乾淨、可重複使用、可讀性更高。簡單說:把常用的 where、orderBy… 等查詢邏輯集中管理,並用簡短語法呼叫。
1.建立Query Scope(查詢作用域)---一個參數
在models\Student.php
輸入如下:
public function scopeMale($query) {
return $query->where('gender','m');
}
如下圖:
在Http\Controller\StudentController.php
輸入如下:
public function queryScope() {
$item = Student::male()->get();
return $item;
}
public function secondQuery() {
$item = Student::male()->get();
return $item;
}
如下圖:
在\routes\web.php 加入
Route::get('query-scope',[StudentController::class,'queryScope']);
Route::get('second-query',[StudentController::class,'secondQuery']);
如下圖
接下來,下指令 php artisan serve
在網址輸入: http://127.0.0.1:8000/query-scope
http://127.0.0.1:8000/second-query
就會出現
接下來,修改Query Scope(查詢作用域)
在models\Student.php
輸入如下:
public function scopeMale($query) {
return $query->where('gender','m')->where('age','18');
}
如下圖:
接下來,其餘不變。下指令 php artisan serve
在網址輸入: http://127.0.0.1:8000/query-scope
http://127.0.0.1:8000/second-query
就會出現
2.建立Query Scope(查詢作用域)---一個參數
在models\Student.php
輸入如下:
public function scopeFemale($query) {
return $query->where('gender','f')->where('age','18');
}
如下圖:
在Http\Controller\StudentController.php
輸入如下:
public function thirdScope() {
$item = Student::female()->get();
return $item;
}
public function fourQuery() {
$item = Student::female()->get();
return $item;
}
如下圖:
在\routes\web.php 加入
Route::get('third-scope',[StudentController::class,'thirdScope']);
Route::get('four-query',[StudentController::class,'fourQuery']);
如下圖
接下來,下指令 php artisan serve
在網址輸入: http://127.0.0.1:8000/third-scope
http://127.0.0.1:8000/four-query
就會出現
3.建立Query Scope(查詢作用域)---二個參數
在models\Student.php
輸入如下:
public function scopeMale($query,$age=18) {
return $query->where('gender','m')->where('age',$age);
}
public function scopeFemale($query,$age=18) {
return $query->where('gender','f')->where('age',$age);
}
如下圖:
在Http\Controller\StudentController.php
輸入如下:
public function queryScope() {
$item = Student::male(25)->get();
return $item;
}
public function thirdScope() {
$item = Student::female(25)->get();
return $item;
}
如下圖:
在\routes\web.php 加入
Route::get('query-scope',[StudentController::class,'queryScope']);
Route::get('third-scope',[StudentController::class,'thirdScope']);
如下圖
接下來,下指令 php artisan serve
在網址輸入: http://127.0.0.1:8000/query-scope
http://127.0.0.1:8000/third-query
就會出現
二、Soft Deletes:進行軟刪除並可還原資料
觀念:在 Laravel 12(以及早期版本)中,Soft Deletes(軟刪除)是 Eloquent ORM 提供的一種資料刪除方式,讓你的「刪除」操作不會真的把資料從資料庫移除,而是將資料標記為已刪除,以便未來可恢復、查詢紀錄或避免資料遺失。
1.在學生表建立一個新列,然後清除所有資料。
指令:php artisan make:migration addSoftDeleteToStudentsTable
加上這句
$table->softDeletes();
如下圖:可看到
三、CRUD(Read):用 Eloquent 讀取資料
四、Table Filters:動態搜尋與條件篩選
五、Pagination:最佳化資料瀏覽與分頁
六、CRUD(Create):建立資料
七、CRUD(Update):更新資料
八、CRUD(Delete):刪除資料
資料來源:



































































