I am developing a laravel application which has the following eloquent models

  • Product hasMany('App/Sku','products_id')
  • Sku belongTO('App/Product')

我有一个控制器‘ProductController’,其中提供了以下代码

public function index()
{    
    $products = Product::all();
    foreach($products as $product){
            $products_id = $product->products_id;
    }
}

I am exposing RESTfull API which will allow my users to get all product details (including skus, shipping types etc..).

假设我有一个API GET : /products

获取所有产品详细信息的代码如下所示

public function index()
{              
      $products = Product::all();
      foreach($products as $product){
          $products_id = $product->products_id;
          $skus_data = Product::find($products_id)->skus;
      }
        // Now I have both the product details + skus which I can bundle into an array/json.
}

现在我的问题是,这个逻辑正确吗?在本例中,所有逻辑都在控制器中,因为IM使用Eloquent 的模型,我 for each 表都有一个模型,并且在其中定义了关系.有没有一种方法可以获得产品/相关型号的所有详细信息(产品详细信息(表1中)+SKU详细信息(表2中)),而不是使用以下内容

foreach($products as $product){
    $products_id = $product->products_id;
    $skus_data = Product::find($products_id)->skus;
}

I am pretty new to laravel development and eloquent models. I will be using repository pattern for the development and in that case where does the aboe logic (Product+Sku combining) resides.

Please help out.

推荐答案

Yes, you can get the details of the products and skus without making one additional query per product using eager loading ( this is referred as the typical N+1 query problem where N is the number of the products )

假设您的ProductSku模型之间的关系为:

Product

public function skus()
{
    return hasMany('App/Sku','products_id');
}

To fetch the products data along with the sku data you can use the with method. In your controller:

Controller

 $products = Product::with('skus')->get();

Then, in your views, you can get the info this way:

View

foreach ($products as $product) 
{
     //$product->skus is a collection of Sku models
     dd( $product->skus );
}

对于存储库问题:如果您想使用存储库,可以将代码中有说服力的访问部分放在存储库中.例如,您可以在存储库中使用以下方法:

ProductRepository

public function getProductsData() 
{
    //access eloquent from the repository
    return Product::with('skus')->get();    
} 

然后,您可以在控制器中使用存储库:

Controller

//inject the repository in the controller
public function __construct( ProductRepository $productRepo )
{
    $this->productRepo = $productRepo;
}

//use the injected repository to get the data
public function index()
{
    $products = this->productRepo->getProductsData();
}

Laravel相关问答推荐

CKEditor在laravel中的p标记之前和之后添加额外的p标记

当两个外键都在同一个表上时创建直通关系

如何从laravel中的另一个表中获取用户名

使用 App::environment() 与 app()->environment() 与 config('app.env') 之间的区别

Laravel 5.8 在从已清除的会话中单击注销后显示419 Page Expired

在 apache docker 容器中运行虚拟主机

Laravel 没有定义

将 Laravel MYSQL 更改为 utf8mb4 以在现有数据库中支持表情符号

Laravel 4,如何测试复选框是否被选中?

在 Laravel 5 中扩展请求类

未找到 PHP 5.4 和 Laravel 类Memcached

Twilio 查找 API 不起作用?

如何使用外部图像生成 html div 的 screenshot截图?

Laravel 在特定文件夹中创建迁移文件

Laravel League/flysystem 使用 AWS S3 获取文件 URL

laravel 队列 - 同步驱动程序如何工作?它是在单独的进程/线程还是主执行线程中执行?

如何在 Laravel 4 中组织不同版本的 REST API 控制器?

Lumen/Laravel 6:调用未定义的函数 array_except()

无法在控制器的构造函数上调用 Auth::user()

Laravel:Redis 无法建立连接:[tcp://127.0.0.1:6379]