title: Laravel 5.4 - form/API/view
body: <p>I created an API (<em>store</em>) that saves the data on the database and returns 201 if successful or 404 if not. </p>
  
  <pre><code>if ($visit-&gt;save()){
      $visit-&gt;view_visit = [
      'href' =&gt; 'api/v1/visit/' . $visit-&gt;id,
      'method' =&gt; 'GET'
       ];
       $response = [
           'msg' =&gt; 'Visit created.',
           'visit' =&gt; $visit
       ];
       return response()-&gt;json($response, 201);
  }
  $response = [
      'msg' =&gt; 'Error during creation.'
      ];
  return response()-&gt;json($response, 404);
  </code></pre>
  
  <p>It works perfectly. Using postman you can see that the status will be &lt;&lt;201 Created>>.</p>
  
  <p>This API should be used in two ways: called by another application or called by a Laravel form. This is the question: </p>
  
  <p>How do I call it in a way if it successful, it will load a given view on the browsers? 
  In other words, is there a way to make the form call a route (the api itself, something like ../api/visit/) and in case of success loads the other view? Also, I would like to pass the content of response['msg'] to this new view. </p>
  
  <p>I know I could do it inside the <em>store</em> method by filtering the HTTP referrer, but I would like to keep the controller code strictly to manage the record creation. Besides that, I have to send the 201/404 codes along with the returned data. </p>
  
  <p>I also considered creating another controller to handle the API response and then call the form, but it still sounds too much -- it's supposed to be easy, I guess. </p>