有點不太理解倉庫模式的用途和作用是什麼
建立倉庫目錄
app/Repository # 倉庫目錄
app/Repository/表名或者其他/Interfaces # 倉庫介面定義
app/Repository/表名或者其他/Repositories # 倉庫介面實現
建立介面和實現介面
<?php
<?php
namespace App\Repository\Order\Interfaces;
Interface OrderInterface{
// 獲取使用者的訂單列表
public function getUserList(int $userId);
}
<?php
namespace App\Repository\Order\Repositories;
use Illuminate\Database\Eloquent\Model;
use App\Repository\Order\Interfaces\OrderInterface;
class OrderRepositories extends Model Implements OrderInterface{
protected $table = 'orders';
public function getUserList(int $userId){
return $this->where('user_test_id', $userId)->get();
}
}
建立服務主要是為了繫結介面
php artisan make:provider RepositoryServiceProvider
// 將RepositoryServiceProvider服務配置到應用中
// config\app.php
'providers' => [
App\Providers\RepositoryServiceProvider::class
]
在RepositoryServiceProvider中繫結介面
public function register(){
$this>app>bind(
'App\Repository\Order\Interfaces\OrderInterface',
'App\Repository\Order\Repositories\OrderRepositories'
);
}
測試路由
use App\Repository\Order\Interfaces\OrderInterface;
Route::get('/get-user-order', function(OrderInterface $order){
return $order->getUserList(1);
});
[{"id":1,"user_test_id":1,"order_number":"yaoxs\u7684\u8ba2\u5355\u7f16\u53f71","created_at":null,"updated_at":null},{"id":2,"user_test_id":1,"order_number":"yaoxs\u7684\u8ba2\u5355\u7f16\u53f72","created_at":null,"updated_at":null},{"id":3,"user_test_id":1,"order_number":"yaoxs\u7684\u8ba2\u5355\u7f16\u53f73","created_at":null,"updated_at":null}]
本作品採用《CC 協議》,轉載必須註明作者和本文連結