title: ErrorException: 'Trying to get property of non-object' while sending a POST request from Angular 5 Service to Laravel 5.4 API
body: <p>I am trying to access data from a Laravel API for my Angular front-end.
  To receive the required data, I have to send an 'id' as POST to the url which in turn queries the database and returns the result as JSON.
  USING POSTMAN and other API tester tools, I have verified that the endpoint is working correctly.
  WHEN I try to send the POST request from Angular, I do not receive the POST data on my Laravel API and the following error is given.</p>
  
  <p>Please note that I have removed the actual URLs from the code</p>
  
  <pre><code>(1/1) ErrorException
  Trying to get property of non-object
  
  in ServiceRequestController.php line 209
  at HandleExceptions-&gt;handleError(8, 'Trying to get property of non-object', '/home/removed_name/public_html/KoiKaam/app/Http/Controllers/ServiceRequestController.php', 209, array('request' =&gt; object(Request), 'serviceRequests' =&gt; object(Collection), 'id' =&gt; null, 'category' =&gt; null, 'cats' =&gt; object(Collection), 'cat' =&gt; null))
  in ServiceRequestController.php line 209
  </code></pre>
  
  <p>While the actual result as tested on POSTMAN and from an Android Application should be <a href="https://i.stack.imgur.com/wFMND.png" rel="nofollow noreferrer">this (a screenshot of the result)</a></p>
  
  <p>This is the code of the angular service I am using to fetch the results</p>
  
  <pre><code>import { Http, Headers, RequestOptions } from '@angular/http';
  import { Injectable } from '@angular/core';
  import { Observable } from 'rxjs/Observable';
  
  import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
  import { BuyDeliverRequests } from '../classes/buy-deliver-requests';
  
  
  const httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  };
  const httpPostOptions = {
    headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
  };
  
  @Injectable()
  export class BuyDeliverRequestsService {
      private url = 'http://removed_name/KoiKaam/public/api/serviceRequestByCategory'
    constructor(private http: HttpClient) { }
  
  
    getBuyDeliverServices(serverResponse: BuyDeliverRequests) {
  
        return this.http.post(this.url, {id: 1},  httpPostOptions).subscribe(
          data =&gt; { serverResponse = data.data; console.log(data); },
          err =&gt; console.log(err),
          () =&gt; console.log('Successful')
        );
    }
  }
  </code></pre>
  
  <p>This is the function that the URL/endpoint goes to, to fetch the results:</p>
  
  <pre><code> /*this function returns all rows of service request wrt category with their picture paths from service request pictures table
      and bids on that request request*/
      public function showServiceRequestbyCategory(Request $request)
      {
          $serviceRequests = new Collection();
          $id = $request-&gt;id; 
  
          $category = Category::find($id);
          $this-&gt;categories-&gt;push($category);
          $cats = $this-&gt;getChilds($id);
          try{
              foreach ($cats as $cat){
                  $srv = $this-&gt;getServiceRequest($cat-&gt;id); //This is line 209 from the error
                  foreach ($srv as $service){
                      $serviceRequests-&gt;push($service);
                  }
              }
  
              $response["message"] = "Post Created Successfully";
              $response["error"] = false;
              $response["data"] = $serviceRequests;
              return json_encode($response);
          }
          catch (QueryException $e){
              $response["message"] = $e-&gt;errorInfo[2];
              $response["error"] = true;
              $response["sja"] = "2nd";
              return json_encode($response);
          }
      }
  </code></pre>
  
  <p>Following is the function that is being called on the line where error is identified by Laravel</p>
  
  <pre><code>private function getServiceRequest($category_id){
      try{
          $serviceRequest = ServiceRequest::where([['category_id','=', $category_id],
              ['in_process_flag', '=', false]])-&gt;paginate(5);
          if($serviceRequest){
              return $serviceRequest;
          }
      }
      catch(QueryException $e){
  
      }
  }
  </code></pre>