Skip to content

Eloquent firstOrCreate() doesn't work with cast attributes #12238

@cviebrock

Description

@cviebrock

This might be related to #5578, but if I have a model that uses $casts, then the firstOrCreate() always creates a new instance. For example:

class MyModel extends \Illuminate\Database\Eloquent\Model {

  $casts = [
    'field' => 'array'
  ];
}

$data = [
  'field' => [1,2,3]
];

// creates a new model
$model = MyModel::firstOrCreate($data); 

//...

// also creates a new model instead of retrieving the first one
$model2 = MyModel::firstOrCreate($data);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions