在使用 Laravel 模型(model)时,获取数据到 Yajra DataTables 时遇到了问题。

huangapple go评论60阅读模式
英文:

Facing trouble while fetching data to yajra datatables with model laravel

问题

以下是翻译好的部分:

我在Laravel应用程序中有以下三个表格:

购买表

id,名称,价格

产品表

id,purchase_id,价格

销售表

id,product_id,数量,总价

销售表属于产品表,引用产品ID,而产品表属于购买表,引用购买ID。

现在我想从销售表中获取数据,但我还需要产品的名称,该名称来自购买表,最终结果必须是:

新表格

id,product_id,数量,总价,名称

我的查询仅获取销售表的数据:

$sales = Sales::whereBetween(DB::raw('DATE(created_at)'), array($from_date, $to_date))->get();

在这里,我返回了Yajira数据表:

return Datatables::of($sales)->addIndexColumn()->make(true);

模型中的其他代码:

销售模型

public function product()
{
    return $this->belongsTo(Product::class, 'product_id');
}

public function purchase()
{
    return $this->belongsTo(Purchase::class, 'purchase_id');
}

产品模型

public function purchase()
{
    return $this->belongsTo(Purchase::class);
}

购买模型

public function category()
{
    return $this->belongsTo(Category::class);
}

public function supplier()
{
    return $this->belongsTo(Supplier::class);
}
英文:

I have the following three table in laravel app

purchases table

id, name, price

Products table

id, purchase_id, price

sales table

id, product_id, quantity, total-price

The sales table belongs to products table refers product id while products table belong to purchases table refers purchase id

Now i I want to fetch data in sales tables but also in need the name of the product which come from purchases table the final results must be

new table

id, product_id, quantity, total-price, name

my query below fetch only sales table data

$sales = Sales::whereBetween(DB::raw('DATE(created_at)'), array($from_date, $to_date))->get();

here I return yajira datatable

return Datatables::of($sales)->addIndexColumn()->make(true);

other code in models

sales model

public function product()
{
    return $this->belongsTo(Product::class, 'product_id');
}

public function purchase()
{
    return $this->belongsTo(Purchase::class,'purchase_id');
}

products model

public function purchase()
{
    return $this->belongsTo(Purchase::class);
}

purchases model

public function category()
{
    return $this->belongsTo(Category::class);
}

public function supplier()
{
    return $this->belongsTo(Supplier::class);
}

答案1

得分: 0

你需要对这个进行连接操作。

Sales::query()
      ->leftJoin('products', 'sales.product_id', '=', 'products.id')
      ->leftJoin('purchases', 'products.purchase_id', '=', 'purchases.id')
      ->whereBetween(DB::raw('DATE(sales.created_at)'), [$from_date, $to_date])
      ->select('sales.id', 'sales.product_id', 'sales.quantity', 'sales.total-price', 'purchases.name');

你可以从文档中了解更多。

英文:

You need to do join operation for this.

Sales::query()
      ->leftJoin('products', 'sales.product_id', '=', 'products.id')
      ->leftJoin('purchases', 'products.purchase_id', '=', 'purchases.id')
      ->whereBetween(DB::raw('DATE(sales.created_at)'), [$from_date, $to_date])
      ->select('sales.id', 'sales.product_id', 'sales.quantity', 'sales.total-price', 'purchases.name');

You can learn more from the documentation

huangapple
  • 本文由 发表于 2023年6月12日 23:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定