Laravel使用查询生成器嵌套联接?

问题描述

我的情况是我有3个不同的表

tblProducts-产品列表-

     id (Primary Key)
     product_title
     deleted (Default value is 0 if not deleted,if deleted value is 1. Same below)

tblClass-类列表-

     id (Primary Key)
     class_title
     deleted 

tblClassperProduct-每个特定产品的分配类-

     id (Primary Key)
     product_id (Key : tblProducts)
     class_id  (Key : tblClass)
     deleted 

所以我想做的就是获取这种关系的嵌套json查询

使用查询生成器的预期输出

 [{
    "id" : 1,"product_title" : "Product1","deleted " : 0,"classes_per_product" : 
      {
         "id": 1,"product_id" : 1,"class_id": 1,"class_title":"class 1","deleted_class_per_product" : 0,"deleted_class" : 0
      },{
         "id": 2,"class_id": 2,"class_title":"class 2",{
         "id": 3,"class_id": 3,"class_title":"class 3",.....so on

我只想分配每种产品上的所有类

当前我有代码

 $data = DB::table('tblProducts')
->select( 'tblProducts.id','tblProducts.product_title','tblProducts.deleted' )
->where('tblProducts.deleted',0)
->get();

 return $data;

尝试执行嵌套的json响应。

一种选择是,以雄辩的模型关系进行操作。但是我的问题是我需要遵循现有查询。因此,我简化了此问题中需要的事情。

解决方法

您可以尝试解决给定的问题。

产品型号: Products.php

<?php
 
namespace App;
use Illuminate\Database\Eloquent\Model;
 
class Products extends Model
{
    public $table = 'tblProducts';

    public function classPerProductRel()
    {
        return $this->hasMany(ClassPerProduct::class,'product_id','id')->where('deleted',0);       
    }
}
?>

ClassPerProduct模型: ClassPerProduct.php

<?php
 
namespace App;
use Illuminate\Database\Eloquent\Model;
 
class ClassPerProduct extends Model
{
    public $table = 'tblClassPerProduct';
}
?>

您可以获取输出

<?php 
$productsAll = Products::with('classPerProductRel')->orderBy('product_title')->get();

// Gel All product
#echo '<pre>';print_r($productsAll);exit;

// Get All product with iteration
foreach ($productsAll as $key => $value) {
    # code...
    echo $value->product_title."<br/>";

    // Get All  Assigned Classes per specific product--
    if(!empty($value->classPerProductRel)){
        foreach ($value->classPerProductRel as $innerKey => $innerValue) {
            # code...
            echo $innerValue->product_id."<br/>";   
            echo $innerValue->class_id."<br/>";
        }
    }
}
?>
,

我建议您使用Eloquent关系,因为它效率更高,如果您使用这种数据库查询构建器方法,那么当数据增长并且用户尝试访问该资源时,将会遇到严重的性能问题。不过,我将向您展示如何使用数据库查询构建器来获取所需的数据,然后向您展示正确的处理口才的方法

$data = \DB::table('tblProducts')
    ->select( 'tblProducts.id','tblProducts.product_title','tblProducts.deleted' )
    ->where('tblProducts.deleted',0)
    ->get();
foreach($data as $product)
{
 $product->classes_per_product[] = \DB::table('tblClassPerProduct')->where("product_id",$product->id)->get();
}
return $data;

解决人际关系的正确方法是这个

//in TblProduct.php 
protected $table = "tblProducts";
public function classes_per_product()
{
 return $this->hasMany('App\ClassProduct','id'); //where ClassProduct is the model that corresponds to the table tblClassPerProduct
}

//in your controller
return $data = TblProduct::with('classes_per_product')->get();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...