title: "Object of class App\User could not be converted to int" error in Laravel
body: <p>I'm trying to save new user with Ajax request in Laravel and i'm getting the following error, </p>
  
  <pre><code>Object of class App\User could not be converted to int
  </code></pre>
  
  <p>I must add the the user is saved, so i'm not sure from where this error comes.</p>
  
  <p>Here is the UserController:</p>
  
  <pre><code>public function save_user(Request $request)
      {
          try {
              if (request()-&gt;ajax()) {
                  $lastUserId = User::where('user_id', '&gt;', 0)-&gt;orderBy('user_id', 'desc')-&gt;get('user_id')-&gt;first()-&gt;toArray();
  
                  $user = new User;
                  $data = Input::all();
                  $user-&gt;user_id = intval($lastUserId['user_id'] + 1);
                  $user-&gt;user_type = $data['user_type'];
                  $user-&gt;email = $data['email'];
                  $user-&gt;password = 'e10adc3949ba59abbe56e057f20f883e';
                  $user-&gt;first_name = $data['first_name'];
                  $user-&gt;last_name = $data['last_name'];
  
                  $user-&gt;save();
                  if ($user &gt; 0) {
                      return response()-&gt;json('Success');
                  }
                  return response()-&gt;json(['status' =&gt; 200, 'message' =&gt; 'save success']);
              }
          } catch (\Exception $e) {
              echo $e-&gt;getMessage();
          }
  </code></pre>
  
  <p>Here is the Ajax request:</p>
  
  <pre><code>$('#saveUser').on('click', function (e) {
              e.preventDefault();
              var $inputs = $('#new-user-form :input');
  
              var values = {};
              $inputs.each(function () {
                  if (this.name != '_token' &amp;&amp; this.name.length &gt; 0) {
                      values[this.name] = $(this).val();
                  }
  
              });
  
              $.ajax({
                  url: '/api/save_user',
                  type: "post",
                  data: values,
                  dataType: 'JSON',
                  success: function (data) {
                      /// location.reload();
                  }
              });
  
          })
  </code></pre>
  
  <p>Here is the User Model </p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use App\Detail;
  class User extends Authenticatable
  {
  
      public function users(){
          return $this-&gt;hasMany('\App\User'); //Product Model Name
      }
      use Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name', 'email', 'password',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      protected $casts = [
          'email_verified_at' =&gt; 'datetime',
      ];
  }
  </code></pre>
  
  <p>I've tried to convert all the input values to their type - like in the DB but it didn't worked</p>
  
  <p><a href="https://i.stack.imgur.com/YDxgK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YDxgK.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/dWwOP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dWwOP.png" alt="enter image description here"></a></p>