【问题标题】:Loading multiple select options into array将多个选择选项加载到数组中
【发布时间】:2015-01-17 11:26:49
【问题描述】:

我的报告视图中有一个选择菜单,应该允许用户选择多个“状态”,然后在我的控制器中,查询结果应该是所有选定的“状态”。但是我不确定如何格式化 ->whereIn 以包含这些状态。关于我需要做什么的任何想法,我才刚刚开始使用 Laravel。

这是我尝试使用数组的查询部分。也是控制器

public function reportFailedPayments(){
    $paymentstatus = Input::get('PaymentStatus');
    $startdate = Input::get('startdate');
    $enddate = Input::get('enddate');
    $status = Input::get('status');
    $data = DB::table('Payments')
        ->join('Customer_Purchases', 'Payments.Purchase_ID','=','Customer_Purchases.Purchase_ID')
        ->leftJoin(
        DB::raw('(Select Payment_ID, max(Process_Hist_ID) AS Process_Hist_ID FROM Payment_Process_History GROUP BY Payment_ID) PHID'),                  function($join){ 
                $join->on('PHID.Payment_ID', '=', 'Payments.Payment_ID'); 
            })
        ->leftJoin('Payment_Process_History', 'Payment_Process_History.Process_Hist_ID', '=', 'PHID.Process_Hist_ID')
        ->leftJoin('Billing_Information', 'Payment_Process_History.Billing_Info_ID', '=', 'Billing_Information.Billing_Info_ID')
        ->join('Users', 'Users.User_ID', '=', 'Customer_Purchases.User_ID')
        ->join('Product_Instances', 'Product_Instances.Instance_ID', '=', 'Customer_Purchases.Instance_ID')
        ->join('Products', 'Products.Product_ID', '=', 'Product_Instances.Product_ID')
        ->select('Users.First_Name AS First_Name', 'Users.Last_Name AS Last_Name', 'Users.Email AS Email', 
        'Payments.Payment_Date AS Scheduled_Payment_Date', 'Payments.Pay_Amount As Pay_Amount', 
        'Products.Name AS Product_Name',
        'Customer_Purchases.Purchase_Date As Purchase_Date', 'Payment_Process_History.Process_Time AS Last_Processed',
        'Payment_Process_History.Transaction_ID AS Transaction_ID', 'Payment_Process_History.Trans_Response_Code AS Transaction_Code',
        'Payment_Process_History.Trans_Response_Text as Transaction_Message', 'Billing_Information.Payment_Type AS Payment_Type',           'Billing_Information.CCType AS CCType', 'Billing_Information.CCLast4 AS CCLast4')
        ->whereIn('Payments.Status',array($paymentstatus))
        ->whereBetween('Payments.Payment_Date', array($startdate, $enddate))->get();
        return View::make('admin.reports.failedPayments.report')->with(array('payments'=>$data));

在我的视图中选择菜单。

<label for="startdate">Start Date</label><input type="date" id="startdate" value="{{      date('Y-m-d', strtotime("-1 days")) }}" />
<label for="enddate">End Date</label><input type="date" id="enddate" value="{{ date('Y-m-    d') }}" />
<label for="status">Status</label><br>
<select multiple="multiple" id="status" name='PaymentStatus'status="Status" size="1">
<option value='ALL'>-- ALL --</option>
<option value='{{ PaymentStatus::Pending}}'>{{     PaymentStatus::toString(PaymentStatus::Pending) }}</option>
<option value='{{ PaymentStatus::Charged}}'>{{ PaymentStatus::toString(PaymentStatus::Charged) }}</option>
<option value='{{ PaymentStatus::Denied}}'>{{ PaymentStatus::toString(PaymentStatus::Denied) }}</option>
<option value='{{ PaymentStatus::Refunded}}'>{{ PaymentStatus::toString(PaymentStatus::Refunded) }}</option>
<option value='{{ PaymentStatus::PendingReview}}'>{{ PaymentStatus::toString(PaymentStatus::PendingReview) }}</option>
<option value='{{ PaymentStatus::Cancelled}}'>{{ PaymentStatus::toString(PaymentStatus::Cancelled) }}</option>


【问题讨论】:

  • $paymentstatus 不应该已经是一个数组了吗?所以你可以做whereIn('Payment.Status', $paymentstatus)。如果没有,请告诉我们您是如何获取变量的以及其中的内容 (var_dump)
  • 我已经尝试过了,并得到“必须是数组类型”错误。我只是想让它按我选择的 Payments.Status 对列表进行排序。在查询上方的控制器中,我正在使用 '$paymentstatus = Input::get('PaymentStatus');
  • select 上是否有 name 属性?因为我在您发布的代码中找不到它
  • 我没有,只是 id='status'
  • 好吧,如果你不使用一些javascript来提交你的表单,你肯定需要一个name属性。这是用于将数据发送到服务器的标识符(并通过Input::get() 访问它)

标签: laravel laravel-4 eloquent


【解决方案1】:

使用name 属性作为name="PaymentStatus[]" 返回一个数组,因此您可以通过以下方式获取所选选项:

$paymentstatus = Input::get('PaymentStatus');

【讨论】:

    猜你喜欢
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2014-10-11
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    相关资源
    最近更新 更多