I am using eloquent with slim framework outside of laravel, I have controllers that help perform CRUD operations. When I try to perform mass assignment operation Eloquent throws an error saying:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed:
emoji.name (SQL: insert into "emoji" ("user_id", "updated_at", "created_at")
values (1, 2016-01-02 02:56:43, 2016-01-02 02:56:43))
Bellow is my controller and Model:
public function create(ServerRequestInterface $request, ResponseInterface $response)
{
$data = $request->getParsedBody();
$uid = $data['uid'];
$keywords = $data['keywords'];
$body = $response->getBody();
$user = User::find($uid);
$user->emojis()->create([
'name' => $data['name'],
'char' => $data['char'],
'category' => $data['category'],
]);
// $emoji = new Emoji();
// $emoji->name = $data['name'];
// $emoji->char = $data['char'];
// $emoji->category = $data['category'];
// $emoji->save();
return $response;
}
The application works when I use the lines that are commented above, but not the other:
My model is below:
namespace BB8\Emoji\Models;
use BB8\Emoji\Models\BaseModel;
class Emoji extends BaseModel
{
protected $table = 'emoji';
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
protected $fillable = array('name', 'char', 'category', 'created_at', 'updated_at', 'user_id');
public function user()
{
return $this->belongsTo("BB8\Emoji\Models\User");
}
public function keywords()
{
return $this->hasMany("BB8\Emoji\Models\EmojiKeyword");
}
}
When I var_dump($data)
I get the below:
array (size=6)
'name' => string 'Happy Face' (length=10)
'char' => string ')' (length=1)
'keywords' =>
array (size=1)
0 => string 'happy' (length=5)
'category' => string 'Happy' (length=5)
'created_by' => int 1
'uid' => int 1
Below is my UserModel
namespace BB8\Emoji\Models;
use BB8\Emoji\Models\BaseModel;
class User extends BaseModel
{
public $timestamps = false;
protected $fillable = ['username', 'password', 'jit'];
public function emojis()
{
return $this->hasMany('BB8\Emoji\Models\Emoji');
}
public static function auth($username, $password)
{
$user = static::where('username', '=', $username)->first();
if (isset($user->exists) && $user->exists) {
if (strcmp(hash('sha256', $password), $user->password) == 0) {
return $user;
}
}
return false;
}
public static function isAuthenticated($token)
{
}
}
BaseModel.php :
namespace BB8\Emoji\Models;
use BB8\Emoji\Database\Connection;
class BaseModel extends \Illuminate\Database\Eloquent\Model
{
public function __construct()
{
$dotenv = new \Dotenv\Dotenv(__DIR__.'/../../');
$dotenv->load();
}
}
I have multi-triple checked my code without seeing what is wrong, yet it throws the constraint violation error. Is this a bug with eloquent or am I doing something wrong.