【问题标题】:Laravel Eloquent how to get data from 2 tables within a date rangeLaravel Eloquent 如何从日期范围内的 2 个表中获取数据
【发布时间】:2021-05-25 07:06:05
【问题描述】:

我有 2 个表(示例),其中包含类似的内容...在 Invoice 表中,我有 5 条记录,在 Quotation 表中,我有 3 条。

Invoice
id
price
inv_desc
invoice_date
balance
Quotation
id
qty
quo_desc
quo_date

我想从日期 1/1/2021 和 28/2/2021 之间的两个表中获取所有记录 (*),并将其保存在一个集合中,以便循环并显示在列表中。由于两个表的列数和日期列的名称不同,如何检索按日期升序排序的记录?预期的输出(示例)应该是:

No. Description Quantity Price Balance Date
1. Quotation 5 1/1/2021
2. Invoice 154.90 154.90 12/1/2021
3. Quotation 10 23/1/2021
4. Invoice 456.00 126.59 5/2/2021
5. Invoice 126.59 56.70 23/2/2021

【问题讨论】:

  • 你已经大大改变了你的问题。请在此处向我们展示您期望的示例输出。
  • @TimBiegeleisen 我已经更新了问题以显示示例输出。很抱歉给您带来不便
  • 您能否在问题中包含示例数据,以获取应导出预期输出的发票和报价表

标签: sql laravel eloquent


【解决方案1】:

这是我在另一个帖子中提出的答案,类似于这个问题:https://stackoverflow.com/a/66454203/15102985

【讨论】:

    【解决方案2】:

    你可以试试联合查询:

    $first = DB::table('Invoice')
        ->select('description', NULL, 'price', 'balance', 'invoice_date')
        ->where('invoice_date', '>=', '2021-01-01')
        ->where('invoice_date', '<', '2021-03-01');
    
    $result = DB::table('Quotation')
        ->select('description', 'qty', NULL, NULL, 'quo_date')
        ->where('quo_date', '>=', '2021-01-01')
        ->where('quo_date', '<', '2021-03-01');
        ->unionAll($first)
        ->get();
    

    这对应于以下原始 MySQL 查询:

    SELECT description, NULL, price, balace, invoice_date
    FROM Invoice
    WHERE invoice_date >= '2021-01-01' AND invoice_date < '2021-03-01'
    UNION ALL
    SELECT description, qty, NULL, NULL, quo_date
    FROM Quotation
    WHERE quo_date >= '2021-01-01' AND quo_date < '2021-03-01';
    

    【讨论】:

    • 请问,union是什么意思?是否可以一次查询?
    • @TanSiKai 检查我的答案的更新,它现在还向您显示原始 MySQL 查询。我所做的就是将这个查询翻译成 Laravel 代码。
    • unionAll() 是否对结果的日期进行排序?
    • 不,你需要一个 ORDER BY 子句,或者,在 Laravel 中,调用 orderBy(...)
    • 但是如何按日期排序,因为 2 个表的日期列名称不同?我认为不知何故需要选择 quo_date 和 invoice_date 作为同名来整理它们?无论如何,我已经更新了这个问题,所以你能再参考一下吗?给您带来的不便深表歉意
    【解决方案3】:

    既然有口才,那么 Quote 和 Invoice 是什么关系呢? 会不会是 1 个报价会有很多发票?我猜报价在发票之前。 如果是的话。

    1. 您需要在发票中添加另一列。所以quotation_id
    2. 创建您的报价模型
    class Quotation extends Model
    
    {
        use HasFactory;
    
        protected $fillable = [
            'qty',
            'description',
            'quo_date'
        ];
    
        public function Invoice()
        {
            return $this->hasMany(Invoice::class);
        } }
    

    3.创建您的发票模型

        class Invoice extends Model
        {
        use HasFactory;
    
        protected $fillable = [
            'price', 'description', 'invoice_date', 'subtotal' 
        ];
    
        public function Quotation() 
        {
             return $this->belongsTo(Quotation::class);
        }
    }
    
    1. 控制器中的下一步
    使用 App\Models\Invoice; 使用 App\Models\Quoatation; $query = Quoatation::wherebetween('quo_date', [2021-01-01,2021-03-01]) ->orderby('quo_date', 'ASC')->with('Invoice') ->wherebetween('invoice_date', [2021-01-01,2021-03-01]) ->orderby('invoice_date', 'ASC') ->获取();

    【讨论】:

    • 1 报价 1 发票。但是一些管理员会删除报价并留下发票。所以报价单和发票的数量是不一样的。无论如何,发票和报价的数量只是一个例子,我想确保我问的查询在两个表中有 2 个不同数量的记录时也可以工作
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 2021-07-17
    相关资源
    最近更新 更多