title: Laravel - How to call a controller function via AJAX (jQuery)
body: <p>I have a number of working controller functions, however, for usability I want to switch some of them over to be called via AJAX.</p>
  
  <p>Here is my jQuery code:</p>
  
  <pre><code>$.ajax({
          method: 'GET',
          url: $(this).data('url'),
          data: {},
          success: function( response ){
              console.log( response );
          },
          error: function( e ) {
              console.log(e);
          }
      });
  </code></pre>
  
  <p>And here is my controller function:</p>
  
  <pre><code>public function add( $user_id, $project_id )
  {
  
      $project = Project::findOrFail( $project_id );
  
      if( empty( $project ) ) {
  
          return view( 'home' )-&gt;with('message', 'Sorry, user not found');
  
      }
  
      if ( $project-&gt;user_id != $user_id ) {
  
          $scrapbook = Scrapbook::firstOrNew(['user_id' =&gt; $user_id]);
  
          $scrapbook-&gt;save();
  
          $scrapbook-&gt;projects()-&gt;attach($project_id);
  
          return true;
  
      } else {
  
          return false;
  
      }
  }
  </code></pre>
  
  <p>As it stands, the controller function actually does what it is supposed to (adds a project to a scrapbook). But what it returns is causing the AJAX request to class as an <strong>error</strong> so I am unable to handle this correctly. I assume I am supposed to return some kind of response object but I don't know what this is or what it needs to include.</p>