0

I have a Select menu in my reports view that should allow a user to select multiple 'status', then in my controller the query results should be in all the selected 'status'. However I am unsure how to format the ->whereIn to include those statuses. Any Ideas on what I need to do, I am just getting started with Laravel.

This is the part in the query where I am trying to use the array. also the controller

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));

Select menu in my View.

<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>


amazingacademy
  • 143
  • 1
  • 9
  • 1
    Shouldn't `$paymentstatus` already be an array? so you can just do `whereIn('Payment.Status', $paymentstatus)`. If not please show us how you're getting the variable and whats in it (`var_dump`) – lukasgeiter Nov 19 '14 at 19:54
  • I have tried that and get "must be of type array" error. I am just trying to get it to sort the list by my Payments.Status that's selected. in the controller above the query i am using '$paymentstatus = Input::get('PaymentStatus'); – amazingacademy Nov 19 '14 at 20:11
  • Do you have a `name` attribute on the `select`? Because I can't find it in the code you posted – lukasgeiter Nov 19 '14 at 20:18
  • i do not, just id='status' – amazingacademy Nov 19 '14 at 20:21
  • Well if you're not using some javascript to submit your form you definitely need a `name` attribute. That's the identifier used to send your data to the server (and access it via `Input::get()`) – lukasgeiter Nov 19 '14 at 20:23
  • I tried adding a name element, just returns an empty table now – amazingacademy Nov 19 '14 at 20:34
  • Ok. Please change `->get()` to `->toSql()` and update the question with the output the function generates. – lukasgeiter Nov 19 '14 at 20:36
  • I updated it to the complete function, and tried ->toSql(). The problem is that there is no output. No error or output so I think i may not be storing the selection properly – amazingacademy Nov 19 '14 at 21:22
  • Maybe the term *output* was misleading. The function is actually **returning** the sql string. so do: '$data = .... ->toSql();` and then `dd($data);` – lukasgeiter Nov 19 '14 at 21:25

1 Answers1

0

Use name attribute as name="PaymentStatus[]" to return an array, so you can get selected options with:

$paymentstatus = Input::get('PaymentStatus');
Razor
  • 9,577
  • 3
  • 36
  • 51