【问题标题】:How to avoid column name conflict in Laravel?如何避免 Laravel 中的列名冲突?
【发布时间】:2015-03-21 00:44:12
【问题描述】:

在 Laravel 中加入 3 个表格后,我试图查看特定表格的日期。但它只显示一个表的信息。

这是连接 3 个表的代码:

路由文件:

 $invoices= DB::table('sales_accounts')
    ->join('invoices', 'sales_accounts.id', '=', 'invoices.sales_Accounts_id')
    ->join('subscribers', 'invoices.receiver_id', '=', 'subscribers.id')
    ->where('sales_accounts.sender_id', $fieldForceID)
    ->get();

return Response::json($invoices);

这是在 Blade Template 中查看信息的脚本

刀片中的代码:

function(data) {

            $.each(data, function(index, element) {

                console.log(element);
                infoShare.append("<pre> Date Of Invoice : "+element.created_at+" | Pos Address : "+element.subscriber_address+"| Total Amount: "+element.cost+" </pre>");
            });
        });

在这里,我想查看 Invoice 的创建日期,但它显示了 subscribers 表中订阅者的创建日期。但我想从发票表中查看发票的具体日期。

我该怎么做?问候

【问题讨论】:

    标签: php mysql laravel-4 multiple-columns


    【解决方案1】:

    我做到了!!!

    如果我像这样更改连接查询,它会显示表的特定值。

    在路由文件中查询:

    $invoices= DB::table('sales_accounts')
    ->join('invoices', 'sales_accounts.id', '=', 'invoices.sales_Accounts_id')
    ->join('subscribers', 'invoices.receiver_id', '=', 'subscribers.id')
    ->where('sales_accounts.sender_id', $fieldForceID)
    ->get(['invoices.created_at','invoices.debit','invoices.credit','invoices.cost','subscribers.subscribers_address']);
    

    返回响应::json($invoices);

    现在可以正常使用了!!!

    使用 SaleAccount 模型更新查询:

    $fieldForceID=Input::get('option');
    $invoices= SaleAccount::where('sales_accounts.sender_id', $fieldForceID)
        ->join('invoices', 'sales_accounts.id', '=', 'invoices.sales_Accounts_id')
        ->join('subscribers', 'invoices.receiver_id', '=', 'subscribers.id')
        ->get(['invoices.created_at','invoices.debit','invoices.credit','invoices.cost','subscribers.subscriber_address']);
    return Response::json($invoices);
    

    【讨论】:

      【解决方案2】:

      试试看

      路由文件

      中的发票查询中添加此项
      ->addSelect(\DB::raw('invoices.created_At as invoce_created'))
      

      获取发票 crated_at date on Blade File

      element.invoce_created
      

      【讨论】:

      • 好的,我试试。谢谢!
      猜你喜欢
      • 2011-07-02
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      相关资源
      最近更新 更多