title: Laravel - Trying to get property "id" of non-object while using POST Method
body: <p>Am writing an endpoint with Laravel using using. When I tested on postman using POST Method, I got this error:</p>
  
  <blockquote>
    <p>ErrorException: Trying to get property &#039;id&#039; of non-object in file C:\xampp\htdocs\testing-file\testing\app\Http\Controllers\ApiController.php on line 912</p>
  </blockquote>
  
  <p>Controller</p>
  
  <pre><code>    public function storeBilling(Request $request)
      {
          // $billing = Billing::create($request-&gt;all());
          // return response()-&gt;json(['success' =&gt; $billing], $this-&gt; successStatus);
          $validator = Validator::make($request-&gt;all(), [
              'network' =&gt; 'required'
          ]);
          if ($validator-&gt;fails()) {
              return response()-&gt;json($validator-&gt;errors(), 422);
          }
  
          // Creating a record in a different way
          $createBilling = Billing::create([
              'user_id' =&gt; $request-&gt;user()-&gt;id,
              'network' =&gt; $request-&gt;network,
              'sender' =&gt; $request-&gt;sender,
              'recipient' =&gt; $request-&gt;recipient,
              'message' =&gt; $request-&gt;message,
              'amount' =&gt; $request-&gt;amount,
              'billing_type' =&gt; $request-&gt;billing_type,
          ]);
  
          return new BillingResource($createBilling);      
      } 
  
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>class Billing extends Model
  {
      protected $table = 'billing';
  
      protected $fillable = [
          'network' ,
          'sender',
          'recipient',
          'message',
          'timestamp',
          'created_at',
          'updated_at',
          'amount',
          'billing_type',
          'user_id',
          'service_name',
          'package',
          'email',
          'user_id'
      ];    
  
  
      public function user() {
          return $this-&gt;belongsTo('App\User');
      }
  
  }
  </code></pre>
  
  <p>Resource</p>
  
  <pre><code>namespace App\Http\Resources;
  
  use Illuminate\Http\Resources\Json\JsonResource;
  use App\Billing;
  
  class BillingResource extends JsonResource
  {
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function toArray($request)
      {
          return [
              'id' =&gt; $this-&gt;id,
              'network' =&gt; $this-&gt;network,
              'sender' =&gt; $this-&gt;sender,
              'recipient' =&gt; $this-&gt;recipient,
              'message' =&gt; $this-&gt;message,
              'amount' =&gt; $this-&gt;amount,
              'billing_type' =&gt; $this-&gt;billing_type,
              'email' =&gt; $this-&gt;email,
              'user' =&gt; $this-&gt;user,
              'service' =&gt; $this-&gt;service,
              'package' =&gt; $this-&gt;package,
              // Casting objects to string, to avoid receive create_at and update_at as object
              'timestamp' =&gt; (string) $this-&gt;timestamp,          
              'created_at' =&gt; (string) $this-&gt;created_at,
              'updated_at' =&gt; (string) $this-&gt;updated_at
            ];
      }
  }
  </code></pre>
  
  <p>If I use this POST Method:</p>
  
  <blockquote>
    <p><a href="http://localhost/testing-file/stesting/api/storeBilling?network=100" rel="nofollow noreferrer">http://localhost/testing-file/stesting/api/storeBilling?network=100</a></p>
  </blockquote>
  
  <p>It suppose to post into the database, but I got this error:</p>
  
  <blockquote>
    <p>ErrorException: Trying to get property &#039;id&#039; of non-object in file C:\xampp\htdocs\testing-file\testing\app\Http\Controllers\ApiController.php on line 912</p>
  </blockquote>