dougu1990 2016-07-11 15:00
浏览 152
已采纳

Laravel Sub在select中使用查询构建器进行查询

I want to get this query using query builder:

SELECT *, 
(    SELECT sum(vendor_quantity) 
     from inventory 
     WHERE product_id = products.id
) as qty from products

I'm stuck with this part

(SELECT sum(vendor_quantity) from inventory where product_id = products.id)

I can do it using raw query but I want to know if there is a way to do it in query builder.

My Table Schema for products:

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('product_type',50);
            $table->string('product_name',255);
            $table->string('internal_reference',255);
            $table->string('barcode',255);
            $table->decimal('sale_price', 10, 2);
            $table->decimal('cost', 10, 2);
            $table->decimal('weight', 10, 2);
            $table->decimal('volume', 10, 2);
            $table->integer('added_by')->unsigned();
            $table->timestamps();
        });
// Foreign Keys
Schema::table('products', function(Blueprint $table) {
  $table->foreign('added_by')->references('id')->on('users');
});

Stocks Table:

Schema::create('stocks', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('product_id')->unsigned();
            $table->integer('vendor')->unsigned();
            $table->string('vendor_product_code',255);
            $table->string('vendor_product_name',255);
            $table->integer('vendor_quantity');
            $table->decimal('vendor_price', 10, 2);
            $table->date('vendor_produce');
            $table->date('vendor_expiry');
            $table->integer('added_by')->unsigned();
            $table->timestamps();
        });
    // Foreign Keys
    Schema::table('stocks', function(Blueprint $table) {
       $table->foreign('product_id')->references('id')->on('products');
       $table->foreign('vendor')->references('id')->on('customers');
       $table->foreign('added_by')->references('id')->on('users');
    });
  • 写回答

1条回答 默认 最新

  • dpepbjp126917 2016-07-14 08:20
    关注

    can you just add what exactly do you need as output? Like what do you plan to throw at your view so I can give you the eloquent setup. From the migration above it looks like you're missing some tables like "inventory".

    In any way - you first need to setup eloquent relationships between your models. For the two above, something like this:

    class Stock extends Model{
    
        public function product(){
            return $this->belongsTo(Product::class);
        }
    
    }
    

    and

    class Product extends Model{
    
        public function stock(){
            return $this->hasMany(Stock::class);
        }
    
    }
    

    Now, that sum of yours has me confused a bit... since vendor_quantity is a column in your stocks table... Do you need to get all the products and the corresponding foreign key values from the stocks table and then sum all the values in the vendor_quantity? If that's the case do something like this:

    $products = Products::with('stock')->get();
    

    This will return the eloquent collection with all your products AND foreign key values from the stock table. Since you have the values from the related table you can just iterate through each of those an add it to a variable or just append it to the initial object for passing to your view. For example

    $products = Product::with('stock')->get();
    
        foreach ($products as $key => $product){
    
            $vendorQuantitySum = $product->stock->sum('vendor_quantity');
    
            $products[$key]->vendorQuantity = $vendorQuantitySum;
    
        }
    

    Now, when you pass $products to your view, you can easily get the sum in your view like so:

    {{ $product->vendorQuantity }}
    

    I just tested it on my Laravel install and it seems to work :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗