I usually like working with an array, so I json decode first, see:
https://gist.github.com/jimgwhit/cbbe5bb0d2556fdc7e37a86d3630239c
You can always use a collection on the parts you need to make them easier to work with.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
At the risk of asking a question that's been asked before, I'm running into some issues with the creation of a simple API with Laravel 7.
I've created a couple of simple functions to create/update a "Company" model, and everything works well when all of the input values are at the root of the JSON object; however, I'd like to nest a few elements as a child object under the "address" key.
{
"name": "Test Company",
"city": "City",
"line_1": "123 Test St.",
"state": "Wherever",
"country": "CA",
"postal_code": "A1B 2C3"
}
public function store(Request $request)
{
$company = Company::create($request->only('name', 'city', 'country', 'line_1', 'line_2', 'postal_code', 'state'));
return new CompanyResource($company);
}
{
"name": "Test Company",
"address": {
"city": "City",
"line_1": "123 Test St.",
"state": "Wherever",
"country": "CA",
"postal_code": "A1B 2C3"
}
}
Based on the Requests documentation # retrieving-input -> Retrieving JSON Input Values it appears that I should just be able to access these values using dot notation; however, this doesn't seem to work.
public function store(Request $request)
{
$company = Company::create($request->only('name', 'address.city', 'address.country', 'address.line_1', 'address.line_2', 'address.postal_code', 'address.state'));
return new CompanyResource($company);
}
The error message I receive is that there's no default value for the city field, which I believe is saying "I'm unable to access the field by the name you've given it"; however, is Eloquent actually attempting to write these values to a column called address.x?
{
"message": "SQLSTATE[HY000]: General error: 1364 Field 'city' doesn't have a default value (SQL: insert into `companies` (`name`, `id`, `updated_at`, `created_at`) values (Test Company, _uuidgoeshere_, 2020-09-02 23:12:41, 2020-09-02 23:12:41))",
"exception": "Illuminate\Database\QueryException",
Does anyone have any recommendations for this particular issue? I'm unable to determine whether or not this is an issue retrieving the correct inputs, or whether it's writing those inputs to the database.
Any recommendations/suggestions would be much appreciated!
Please or to participate in this conversation.