1

I am using mass assignment to create a server model like so:

/** @var Model $server */
$server = Server::create($request->all());

return response($server->jsonSerialize(), Response::HTTP_CREATED);

Now my server also has a status property which maps to a MySQL status column which has a default value of 0 in MySQL.

Because the status is not being set implicitly in my request attributes(no need since the default is fine) it does not return the status property on the model. It will return all of the properties in my $request->all() though. These are validated and coming from a form.

How can I return my full $server model including the default value for status? I am forced to fire off another query to re-fetch the model I have just created, just so I can also include the status property with the MySQL default?

Stephan-v
  • 19,255
  • 31
  • 115
  • 201
  • Possible duplicate [how-to-set-the-default-value-of-an-attribute-on-a-laravel-model](https://stackoverflow.com/questions/39912372/how-to-set-the-default-value-of-an-attribute-on-a-laravel-model) – Farooq Ahmed Khan Aug 15 '18 at 07:15

2 Answers2

1

It doesn't do a selecet after an insert so you wont get any values that the DB has to set itself. You can refresh the model instance though (cause it to select that row):

$model->refresh();
lagbox
  • 48,571
  • 8
  • 72
  • 83
0

If you're returning it as JSON, you can add status to protected $appends = ["status"];

class Server extends Model {
   protected $appends = ["status"];
   ...
}

That way, even if you don't set status in your Server::create() method, it should still be visible in your JSON response as server.status. Also, perhaps adjust your return to

return response()->json(["server" => $server], 200); // or whatever HTTP code you need.

Since $appends only works with accessors, perhaps use $request->merge() to "fill in the gaps" for data you want returned in your JSON response:

$request->merge([
  "status" => "active", // Or whatever the default is from the database
  ...
]);

Since the response is returning everything in $request->all(), merge it with default values from MySQL. Note: currently hardcoded, would need to be updated if migration/schema changed, or via SQL command to get defaults.

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
  • Appends only work for `accessors` though and it does not grab the default value which is specified within MySQL. – Stephan-v Aug 14 '18 at 19:43
  • Hmm... That's unfortunate, but makes sense now that you say it. I haven't run into this specific issue, but I have been doing a lot of JSON returns recently (API stuff). Could you do a `$request->merge(["status" => "default_status_from_db"]);`? That _should_ make `status` attribute available to your `$server` model. Would have to be updated if the default in the DB changed though, so maybe not the best approach. – Tim Lewis Aug 14 '18 at 19:46
  • 1
    This works but like you said it is still prone to error. Thanks for thinking along though this seems like this is a pretty good solution. – Stephan-v Aug 14 '18 at 20:03
  • Perhaps update the answer? The current answer does not work for the question and only works with accessors. – Stephan-v Aug 14 '18 at 20:14