title: Conditionaly Laravel Resource
body: <p><strong>TicketResource.php</strong></p>
  
  <pre><code>public function toArray($request) {
      return [
              'id' =&gt; $this-&gt;id,
              'user_id' =&gt; $this-&gt;user_id,
              'title' =&gt; $this-&gt;title,
              'body' =&gt; $this-&gt;body,
              'status' =&gt; $this-&gt;status,
              'created_at' =&gt; $this-&gt;created_at-&gt;toDateTimeString(),
      ];
  }
  </code></pre>
  
  <p><strong>CommentResource.php</strong></p>
  
  <pre><code>public function toArray($request) {
      return [
              'id' =&gt; $this-&gt;id,
              'body' =&gt; $this-&gt;body,
              'user_id' =&gt; $this-&gt;user_id,
              'created_at' =&gt; $this-&gt;created_at-&gt;toDateTimeString()
      ];
  }
  </code></pre>
  
  <p><strong>TicketController.php</strong></p>
  
  <pre><code>public function index() {
      return TicketResource::collection(Ticket::all());
  }
  
  public function show(Ticket $id) {
      $ticket = $id;
      return new TicketResource($ticket);
  }
  </code></pre>
  
  <p><strong>Model Ticket.php</strong></p>
  
  <pre><code>public function comments() {
      return $this-&gt;hasMany('App\Comment');
  }
  </code></pre>
  
  <p><strong>Model Comment.php</strong></p>
  
  <pre><code>public function ticket() {
      return $this-&gt;belongsTo('App\Ticket');
  }
  </code></pre>
  
  <p><strong>routes/api.php</strong></p>
  
  <pre><code>Route::get('tickets', 'TicketController@index');
  Route::get('tickets/{id}', 'TicketController@show');
  </code></pre>
  
  <p>I want when I request to <code>tickets/{id}</code> URL, I expect to receive this response:</p>
  
  <pre><code>{
      "data": {
          "id": 1,
          "user_id": 2,
          "title": "lorem",
          "body": "epsum",
          "status": "open",
          "created_at": "2020-03-04 18:14:56",
          "comments": [
                  {
                      "id": 1,
                      "body": "equi",
                      "user_id": 1,
                      "created_at": "2020-03-05 18:14:56",
                  }
          ]
      }
  }
  </code></pre>
  
  <p>On the contrary, when I visit <code>tickets</code> URL, I don't want the <code>comments</code> to be added on each ticket.</p>
  
  <p>How can I implement that?</p>