I tried to create a minimal version of your API and create a similar test in the latest version of Laravel 5.3.22. I also used MariaDB 10.1.13 for the database. And it turned out that the test failed, as we expected, due to the empty api_token .
1. User migration file
Here is my migration file to create the users table:
use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('first_name'); $table->string('last_name'); $table->string('email_address')->unique(); $table->string('phone'); $table->string('username'); $table->string('password'); $table->string('api_token'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('users'); } }
2. User model
Here is my app\User.php . Quite minimally, I only update the $fillable property to match our users table structure:
namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; protected $fillable = [ 'first_name', 'last_name', 'email_address', 'phone', 'username', 'password', 'api_token', ]; protected $hidden = [ 'password', ]; }
3. API routes the file
I am creating a new API route in the routes/api.php to create a new user. Super simple without any verification:
Route::post('users', function (Request $request) { $data = $request->only([ 'first_name', 'last_name', 'email_address', 'phone', 'username', 'password', 'api_token', ]); if (is_string($data['password'])) { $data['password'] = bcrypt($data['password']); } $user = App\User::create($data); return response()->json([ 'status' => 'success', ], 201); });
4. Create a custom API test
And finally, here is my test file for testing our user API.
class ApiTest extends TestCase { public function a_user_can_be_created() { $user = [ 'first_name' => 'Jane', 'last_name' => 'Smith', 'email_address' => 'jsmith@mailinator.com', 'phone' => '1234567890', 'username' => 'jsmith', ]; $this->json('POST', 'api/users', $user) ->assertResponseStatus(201) ->seeJsonEquals(['status' => 'success']) ->seeInDatabase('users', array_except($user, ['password'])); } }
The only difference is that I used array_except() to exclude the password column from the database test, since we hadhed the value before storing it in the database. But of course, this is not related to your problem.
When I run the test without setting api_token , the test failed as expected. The API returns a status code of 500, and we expect it to be 201.

Just like a link, when I set the api_token column as a NOT-null value, then force create a new user with an empty api_token , the error message I received looks like this:
QueryException on line 769 Connection.php:
SQLSTATE [23000]: violation of integrity constraint: 1048 The column "api_token" cannot be zero (SQL: insert into user ...
Hope this help helps you debug your problem!