Reputation: 62366
When creating a related message I've noticed the message
column is not being set but remains set to null
.
$message = $lastSeason->messages()->make([
'type' => \App\Message::SEASON,
'message' => ['this should be converted to json on save'],
]);
dd($message->toArray());
The above outputs:
array:3 [
"type" => "season"
"message" => null
"season_id" => 1
]
Here's both the Season
and Message
models:
class Message extends Model
{
const SEASON = 'season';
protected $guarded = [
'id',
];
protected $casts = [
'message' => 'array',
];
public function season() : BelongsTo
{
return $this->belongsTo(Season::class);
}
}
class Season extends Model
{
protected $guarded = [
'id',
];
public function messages() : HasMany
{
return $this->hasMany(Message::class);
}
}
This isn't a guarded attribute, so is there something else I'm overlooking on these models?
Upvotes: 0
Views: 124
Reputation: 1800
Not sure where you got make()
from or why there is no exception thrown. In the Laravel API docs, there is no such method defined. I think you are looking for create()
as described here.
$message = $lastSeason->messages()->create([
'type' => \App\Message::SEASON,
'message' => ['this should be converted to json on save'],
]);
Optionally, if make()
method actually exists, you can try running ->save()
method on top of it.
Upvotes: 0
Reputation: 342
Try to save encoded data
$message = $lastSeason->messages()->make([
'type' => \App\Message::SEASON,
'message' => json_encode('this should be converted to json on save'),
]);
Dont forget enable
$fillable = ['message','type']
on Message model
And your your database should have a JSON or TEXT field type that contains serialized JSON.
Regarding by documentation
Upvotes: 2