Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MrThePlague's avatar

Laravel API - Accessing nested JSON elements

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.

Where I started

Example Input

{
    "name": "Test Company",
    "city": "City",
    "line_1": "123 Test St.",
    "state": "Wherever",
    "country": "CA",
    "postal_code": "A1B 2C3"
}

Working Controller

public function store(Request $request)
    {   
        $company = Company::create($request->only('name', 'city', 'country', 'line_1', 'line_2', 'postal_code', 'state'));

        return new CompanyResource($company);
    }

Where I'm trying to go

Input

{
    "name": "Test Company",
    "address": {
        "city": "City",
        "line_1": "123 Test St.",
        "state": "Wherever",
        "country": "CA",
        "postal_code": "A1B 2C3"
    }
}

Updated Controller

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);
    }

Error Message

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!

0 likes
4 replies
MrThePlague's avatar

Thanks for the response @jlrdw!

How I managed to handle this was slightly less elegant than I'd hoped, but will definitely work. Instead of just sending create() the entire $request, I just accessed the elements of the array individually, and pulled the values from the array using their index.

public function store(Request $request)
    {   
        $company = Company::create([
            'name'          => $request->name,
            'city'          => $request->address['city'],
            'line1'         => $request->address['line1'],
            'line2'         => $request->address['line2'],
            'state'         => $request->address['state'],
            'country'       => $request->address['country'],
            'postal_code'   => $request->address['postal_code'],
        ]);

        return new CompanyResource($company);
    }
gkv's avatar

{ "name": "Test Company", "address": { "city": "City", "line_1": "123 Test St.", "state": "Wherever", "country": "CA", "postal_code": "A1B 2C3" } }

how can i manage in database using laravel

CamKem's avatar

@gkv using pivot table using eloquent relationships. Please make new posts when asking for help, don't try and revive old posts.

Please or to participate in this conversation.