Odinovsky
Odinovsky

Reputation: 555

If statements not working in Laravel Controller

I've got a simple API that simple fetches _GET variables and use them as Where clause in my controller.

    public function MUIntervalAPICall(Request $dte)
    {
      $date = $dte->dte;
      $element_language = $dte->language;
      $element_customer = $dte->customer;
      $element_contract = $dte->contract;
      $element_subcontract = $dte->subcontract;
      $element = $dte->element;
      $mu_interval= MUInterval::select('element_customer', 'element_contract', 'element_subcontract', 'element_language', 'element_site', 'element', 'src_id', 'src_type_id', 'dte', 'intvl', 'val_src_id', 'exception_name', 'duration_seconds', 'duration_fte')
        ->where('dte', $date)
        if(isset($_GET['customer'])){
        ->where('element_customer', $element_customer)
        }
        ->get()
        ->toArray();
      function array_to_xml( $data, &$xml_data ) {
          foreach( $data as $key => $value ) {
              if( is_array($value) ) {
                  $key = 'Exception';
                  $subnode = $xml_data->addChild($key);
                  array_to_xml($value, $subnode);
              } else {
                  $xml_data->addChild("$key",htmlspecialchars("$value"));
              }
           }
      }
      $xml_data = new SimpleXMLElement('<?xml version="1.0"?><muExceptions></muExceptions>');
      array_to_xml($mu_interval,$xml_data);
      $result = $xml_data->asXML();
      return Response::make($result, '200')->header('Content-Type', 'text/xml');
    }
}

It just checks the URL if customer is set in the URL. e.g. 2016-14-03?customer=Apple. But for some reason I'm getting this error: enter image description here

I commented out the if statement and the closing brace and it actually works and fetches the date and the customer.

->where('dte', $date)
->where('element_customer', $element_customer)
->get()
->toArray();

I'm wondering if there's a namespace I'm missing or if the 'if' statement isn't applicable in such a controller.

Upvotes: 1

Views: 2013

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31739

You can't do method chaining that way. Try -

$mu_interval= MUInterval::select('element_customer', 'element_contract', 'element_subcontract', 'element_language', 'element_site', 'element', 'src_id', 'src_type_id', 'dte', 'intvl', 'val_src_id', 'exception_name', 'duration_seconds', 'duration_fte')
              ->where('dte', $date);

if(isset($_GET['customer'])){
     $mu_interval = $mu_interval->where('element_customer', $element_customer);
}

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

This is how it works:

$mu_interval= MUInterval::select('element_customer', 'element_contract', 'element_subcontract', 'element_language', 'element_site', 'element', 'src_id', 'src_type_id', 'dte', 'intvl', 'val_src_id', 'exception_name', 'duration_seconds', 'duration_fte')
        ->where('dte', $date);

if(isset($_GET['customer'])){
    $mu_interval = $mu_interval->where('element_customer', $element_customer)
}

$mu_interval = $mu_interval->get()->toArray();

Upvotes: 1

Related Questions