In the Eloquent
does not yet have a plausible solution to work with this type of data array
existing resource at the Bank PostGresql
, that is to say, it does not support this yet, that including in its documentation the field intended for operations with array
sane text
and/or json
.
Back still in explanation the error message is because the data gets bad formatted with two quotes, example ""[20,45]""
because of the cast
present in Model
which does not apply to this type of data, that is to say, only the cast
of array
field text
or json
and to create the Migrations these two types are well explained
Due to this impossibility, change the field type in the database to one of these two (text
or json
) and do it the way you’re doing it:
Model
class Leituras extends Model
{
protected $table = 'tb_leituras';
protected $casts = ['ids_autores' => 'array'];
}
Inserting
$leitura = new Leituras();
$leituras->ids_autores = [20,45];
$leituras->save();
that will work and work with this data in array
both inserting both changing and returning a array
when so researched.
A tip:
Observing: The method insert
and update
does not pass through the conversion of cast
array
and needs to be converted, examples:
Works:
Leitura::insert(['ids_autores' => json_encode([20,45])]);
Leitura::create(['ids_autores' => [20,45]]);
$leitura = new Leitura();
$leitura->ids_autores = [20,45];
$leitura->save();
Does not work, you need to use the function json_encode
:
Leitura::insert(['ids_autores' => [20,45]])
Leitura::where('id', 1)->update(['ids_autores' => [20,45]]);
Leitura::where('id', 1)->update(['ids_autores' => [20,45]]);
References
You understand the reasons?
– novic