-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Difference of using Trait DocumentModel and extending using Mongodb Model #3106
-
Just want to clarify if there are some drawbacks of using one to the other thanks
Beta Was this translation helpful? Give feedback.
All reactions
Answered by
GromNaN
Aug 21, 2024
Using MongoDB\Laravel\Eloquent\Model should be preferred:
- there is an optimisation in the
Model::isDocumentModelfunction. - you don't have to set the properties
$primaryKeyand$keyType
The trait MongoDB\Laravel\Eloquent\DocumentModel was introduced in order to support 3rd party model classes.
Replies: 1 comment 1 reply
-
Using MongoDB\Laravel\Eloquent\Model should be preferred:
- there is an optimisation in the
Model::isDocumentModelfunction. - you don't have to set the properties
$primaryKeyand$keyType
The trait MongoDB\Laravel\Eloquent\DocumentModel was introduced in order to support 3rd party model classes.
Beta Was this translation helpful? Give feedback.
All reactions
1 reply
-
Will this be ok I know Laravel mongodb also has Auth class I could use, but using DocumentModel would keep things clean.
I run a test of breeze
<?php namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use MongoDB\Laravel\Eloquent\DocumentModel; use Spatie\Activitylog\LogOptions; use Spatie\Activitylog\Traits\LogsActivity; use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use DocumentModel,HasFactory, HasRoles, LogsActivity,Notifiable; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array<string, string> */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() ->logAll() ->logOnlyDirty(); } }
Beta Was this translation helpful? Give feedback.
All reactions
Answer selected by
masterbater
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment