So I've been working on a project recently, and I noticed something that I'm not sure if it's intentional or not but it's incredibly annoying.
I've been explicitly filling my $fillable arrays on my models just for peace of mind as well as because it just seems a lot more descriptive than just disabling it completely.
Well anyway, obviously I don't let my id fields be fillable, but I've now run into the issue that if I call Model::Create, it doesn't return the id. Which is obviously incredibly annoying as I can't really associate it with anything now.
I was wondering whether this was intentional, or a bug, and also what I can do to fix it (OTHER than setting ID to fillable since that just seems like a really bad idea all around to be honest.)
My code looks similar to the below. (Modified slightly for simplification)
public function store(ModelRequest $request)
{
$this->authorize('create', Model::class);
$model = Model::create($request->validated());
if ($request->has('properties')) {
$model->properties()->attach($request->get('properties'));
}
$model->load('properties');
return response()->json($model);
}
As a side question as well, I'm doing $model->load('properties'); to return the properties association with the json response. Is this the "cleanest" way of doing this, or is there a simpler way?