You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Database Driver & Version: MongoDB community version 5.0
Description:
I've chosen Mongo DB as a database for my Laravel project for the primary reason of it supporting schema-less data, which is what my project is using.
I'm saving data in my project from another system and have hundreds of fields, so using Mongo DB is perfect for this.
For this reason, I cannot create a migration as such with every possible field with their types as I do not know them, so, my migration simply contains the Laravel specifics with the id, and timestamps, maybe I'll add a few more:
My migration is called: 2022_05_25_133123_create_applications_table.php
/** * Run the migrations. * * @return void */publicfunctionup()
{
Schema::create('applications', function (Blueprint$collection) {
$collection->id();
$collection->timestamps();
});
}
My model, is called Application, and it's here where I'm trying to cast a field that's been saved:
<?phpnamespaceApp\Models;
useIlluminate\Database\Eloquent\Factories\HasFactory;
useJenssegers\Mongodb\Eloquent\Model;
class Application extends Model
{
use HasFactory;
/** * The collection associated with the model. * * @var string */protected$collection = 'applications';
/** * The attributes that aren't mass assignable. * * @var array */protected$guarded = [
'id',
'created_at',
'updated_at'
];
/** * The attributes that should be cast. * * @var array */protected$casts = [
'processing_duration_secs' => 'integer'
];
/* ** Linked application payday */publicfunctionpayday()
{
return$this->hasOne(ApplicationPayday::class);
}
/* ** Linked application response */publicfunctionresponse()
{
return$this->hasOne(ApplicationResponse::class);
}
/* ** Linked application api links */publicfunctionapiLinks()
{
return$this->hasMany(ApplicationApiLink::class);
}
}
Description:
I've chosen Mongo DB as a database for my Laravel project for the primary reason of it supporting schema-less data, which is what my project is using.
I'm saving data in my project from another system and have hundreds of fields, so using Mongo DB is perfect for this.
For this reason, I cannot create a migration as such with every possible field with their types as I do not know them, so, my migration simply contains the Laravel specifics with the id, and timestamps, maybe I'll add a few more:
My migration is called: 2022_05_25_133123_create_applications_table.php
My model, is called
Application
, and it's here where I'm trying to cast a field that's been saved:Data is saved like:
But my casting when, for instance, looking through tinker, I see that it's still a string?
Steps to reproduce
Model::create(['processing_duration_secs'=>'5'])
to save a field as a stringExpected behaviour
I should be able to cast fields
Actual behaviour
Casting is not running on fields, what's the solution?
The text was updated successfully, but these errors were encountered: