1

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.

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

7 Comments

I tried the above and I'm getting a FatalErrorException in APIController.php line 4: Namespace declaration statement has to be the very first statement or after any declare call in the script. Line 4 is where the controller is located..namespace App\Http\Controllers\API;
Just check if you have anything before namespace declaration. File should start from <?php and in 3rd line you should have something like namespace App\Http\Controllers\;
Nice..this is really helpful..Thanks a lot sir.. :) I;ve got error_reporting(0); before the namespace..
I'm glad I could help. ) Please choose my answer as best answer and upvote my and Sougata's posts to thank us for our time.
I'm currently getting a blank XML now that appears to stop at first three lines when I viewed the source.
|
1

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

5 Comments

You got the error in a code, it should be $mu_interval = $mu_interval->where('element_customer', $element_customer);
My Bad. Missed that one.
I tried the above and I'm getting a FatalErrorException in APIController.php line 4: Namespace declaration statement has to be the very first statement or after any declare call in the script. Line 4 is where the controller is located..namespace App\Http\Controllers\API;
@jtejido Check the namespace declarations. It is alreay saying what is the issue.
Thanks a lot for this Sougata.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.