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.