If I use the following code snippet in my model it inserts the data:
$instance = DB::table('users')->insert(compact('email', 'username'));
But if I do this instead:
$instance = static::create(compact('email', 'username'));
It inserts null, but created_at and updated_at are inserted.
Laravel's created_at
/updated_at
are part of Illuminate\Database\Eloquent\Model
. A raw DB::table
query builder isn't an Eloquent model and thus doesn't have those automatic parameters.
NULL
data is being inserted in the Eloquent query because Eloquent has a $fillable
parameter you need to define. This parameter sets which columns can be mass-assigned. Laravel strips attributes not in this array when you do a fill
, create
, or otherwise instantiate a new object. In your model, you'd want:
class User extends Eloquent {
protected $fillable = ['email', 'username'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With