title: Why AJAX GET/POST on Laravel does "Internal Server Error" when I try to use data for database request?
body: <p>I have a special issue that i'm not able to fix...</p>
  
  <p>I drag &amp; drop some event in a calendar. When I drop them, I send an Ajax Post Request to my controller, which is able to insert in database some stuff with the event name that I sent via ajax.</p>
  
  <p>first, there is my code in my JS file :</p>
  
  <pre><code>$("#someIDstuffHere").droppable({
      appendTo: "body",
      tolerance: "pointer",
      accept: ".external-event",
      drop: function(event, ui) {
          $(this).append($(ui.draggable));
          $.ajax({
              type: 'POST',
              url: 'events/add-cron-event',
              data: {
                  "eventName": $(ui.draggable).text() // get the span text
              },
              success: function(data) {
                  console.log("success");
              },
              error: function() {
  
              }
          });
      }
  });
  </code></pre>
  
  <p>My EventController.php</p>
  
  <pre><code>public function postAddCronEvent(){ // Laravel use RESTful, so function can have post/get + url like add-cron-event
      $data['input'] = Input::get('eventName'); // get the data from POST
      $cronState = CronState::where('name', '=', $data['input'])-&gt;take(1)-&gt;get(); // Laravel Eloquent ORM make me able to do that kind of request... 
      foreach ($cronState as $state) {
          var_dump($state);
      }
  
  }
  </code></pre>
  
  <p>And the var_dump result (beautified) when I use that via GET (not POST).</p>
  
  <pre><code>object(
  CronState
  )#234 (
  18
  ) {
  [\"table\":protected]=&gt; string(
  9
  ) \"cronState\"
  [\"connection\":protected]=&gt; NULL
  [\"primaryKey\":protected]=&gt; string(
  2
  ) \"id\"
  [\"perPage\":protected]=&gt; int(
  15
  )
  [\"incrementing\"]=&gt; bool(
  true
  )
  [\"timestamps\"]=&gt; bool(
  true
  )
  [\"attributes\":protected]=&gt; array(
  4
  ) {
  [\"id\"]=&gt; string(
  2
  ) \"14\"
  [\"name\"]=&gt; string(
  5
  ) \"test2\"
  [\"created_at\"]=&gt; string(
  19
  ) \"2014-04-16 12:35:35\"
  [\"updated_at\"]=&gt; string(
  19
  ) \"2014-04-16 12:35:35\" }
  [\"original\":protected]=&gt; array(
  4
  ) {
  [\"id\"]=&gt; string(
  2
  ) \"14\"
  [\"name\"]=&gt; string(
  5
  ) \"test2\"
  [\"created_at\"]=&gt; string(
  19
  ) \"2014-04-16 12:35:35\"
  [\"updated_at\"]=&gt; string(
  19
  ) \"2014-04-16 12:35:35\" }
  [\"relations\":protected]=&gt; array(
  0
  ) { }
  [\"hidden\":protected]=&gt; array(
  0
  ) { }
  [\"visible\":protected]=&gt; array(
  0
  ) { }
  [\"appends\":protected]=&gt; array(
  0
  ) { }
  [\"fillable\":protected]=&gt; array(
  0
  ) { }
  [\"guarded\":protected]=&gt; array(
  1
  ) {
  [0]=&gt; string(
  1
  ) \"*\" }
  [\"touches\":protected]=&gt; array(
  0
  ) { }
  [\"with\":protected]=&gt; array(
  0
  ) { }
  [\"exists\"]=&gt; bool(
  true
  )
  [\"softDelete\":protected]=&gt; bool(
  false
  ) }
  </code></pre>
  
  <p>When I do that, it works.</p>
  
  <pre><code>    success: function(data) {
          console.log("success");
      }
  </code></pre>
  
  <p>Show me "success" in the console.log...</p>
  
  <p>But, NOW, when I do :</p>
  
  <pre><code>foreach ($cronState as $state) {
      $cronEvent = CronEvent::find($state-&gt;id);
      $cronEvent-&gt;h = 8;
      $cronEvent-&gt;save();
  }
  </code></pre>
  
  <p>My Chromium console said "Internal Server Error 500" (GET and POST tested)...</p>
  
  <p>I think the mistake comes from the var_dumped data... It looks like very complicated...</p>
  
  <p>Thanks for help !</p>
  
  <p><strong>EDIT to anwser to comments :</strong></p>
  
  <p>127.0.0.1 - - [17/Apr/2014:10:52:48 +0200] "POST /stage/public/events/add-cron-event HTTP/1.1" 500 1536 "localhost/stage/public/events" "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/33.0.1750.152 Chrome/33.0.1750.152 Safari/537.36"</p>
  
  <p>That is the only error I can see in my error.log or access.log (this error is from access.log...)</p>
  
  <p><strong>EDIT AGAIN :</strong></p>
  
  <p>It's seem to be a problem with $cronEvent = CronEvent::find($state->id); ...</p>
  
  <p>$state->id don't works...</p>
  
  <p>But if I put "21" or another id, it works !</p>
  
  <p>I tried to convert to Int with intval() but it doesn't works again...</p>
  
  <p><strong>ANSWERED</strong></p>
  
  <p>I auto answered with some investigations...</p>
  
  <p>It's seem to be the Eloquent request...</p>
  
  <p>I did a mistake. I tried to update the primary key, but primary key with "13" or "14" doesn't exists in my DB.</p>
  
  <p>But I tried to use Eloquent after correcting this mistake, but it doesn't works again...</p>
  
  <p>So,</p>
  
  <p>I change that by :</p>
  
  <pre><code>DB::table('cronEvent')
    -&gt;where('state_id', $state-&gt;id)
    -&gt;update(array('h' =&gt; 10));
  </code></pre>
  
  <p>I used the Query Builder Instead of the Eloquent, and it's works !</p>