6 months ago

Tinkering with json array request in the controller (SOLVED)

Posted 6 months ago by SteamDiesel

So I've got a SPA on the front end (VueJS) and Laravel backend 5.8.*.

I keep bumping into this problem, I'm sure there is a simple and common fix for it, but I have been unable to solve it with my usual google kung fu.

My objective: save a new street address, and at the same time send through the id of each tennant and attach them to the address with their own relationship info on the pivot table.

Example: address is 1 Rode road, Brisbane. michael lived here for 21 months and was boarding. Jenny lived here for 60 months and was the property owner.

I imagine I can save the address and then iterate through an array of people I send to the server as "tennants" to attach each of them after the address is created.

my request to store I get this error message: "Trying to get property 'id' of non-object"

On the front end:

// First I iterate through the people I want to attach, to the address, and I use this bit to convert the years and months into a single "months" value.
                var array = => {
                    return {
                        name: person.first_name,
                        status: person.status,
                        months: +person.months + (person.years * 12)

// then I send it through to the backend like this. note the tennants is the new array with the specific fields i want to send
      '/api/address', {
                    order: this.order,
                    property_name: this.property_name,
                    unit: this.unit,
                    level: this.level,
                    street_number: this.street_number,
                    street_name: this.street_name,
                    street_type: this.street_type,
                    suburb: this.suburb,
                    state: this.state,
                    postcode: this.postcode,
                    tenants: array

so I dd($request->input()) and get:

array:12 [
  "order" => null
  "property_name" => null
  "unit" => null
  "level" => null
  "street_number" => "1"
  "street_name" => "Rode"
  "street_type" => "Road"
  "suburb" => "Brisbane"
  "state" => "QLD"
  "postcode" => "4000"
  "country" => "Australia"
  "tenants" => array:2 [
    0 => array:3 [
      "id" => 1
      "status" => "Boarding Spouse"
      "months" => 21
    1 => array:3 [
      "id" => 2
      "status" => "Mortgage"
      "months" => 60

This is what my store method looks like in the AddressController:

    public function store(Request $request)
    // Save the new address
        $address = new Address([
            'property_name' => $request->property_name,
            'unit' => $request->unit,
            'level' => $request->level,
            'street_number' => $request->street_number,
            'street_name' => $request->street_name,
            'street_type' => $request->street_type,
            'suburb' => $request->suburb,
            'state' => $request->state,
            'postcode' => $request->postcode,
            'country' => $request->country,

// Iterate through the tenants 
        foreach($request->tenants as $tenant){
            $address->tenant()->attach($tenant->id, [ //this is the line
                'status' => $tenant->status,
                'months' => $tenant->months
    // Return the newly saved address
        return response()->json([
            'address' => $address
        ], 200);


Has anyone else found a solve for this?

I've been through the laravel docs here:

I haven't figured out how to iterate through the array in the controller to be able to use the key=>value pairs in a useful way.

but when I dd($tenant) in the foreach I get the first tenant array input, so that's something...


Please sign in or create an account to participate in this conversation.