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

romulo27's avatar

Record the relationship between 3 tables.

I have a form where I choose the bikes for a dealership at the time of registering I want to link and synchronize the data to save in the bank, I am breaking the head so I can register, but to no avail. I made a dd () in request ()

In the first array Brands I have the id of the brand and soon after in the position motorcycles the motorcycles that I need to register.

Result of dd () with the data coming from the form.

array:3 [▼
  "_method" => "PUT"
  "_token" => "XFZ5BlmBA8gajxjenJuGvFl7PZVXLYUG702Nxchw"
  "brands" => array:2 [▼
    1 => array:1 [▼
      "motorcycles" => array:2 [▼
        0 => "1"
        1 => "2"
      ]
    ]
    5 => array:1 [▼
      "motorcycles" => array:2 [▼
        0 => "5"
        1 => "6"
      ]
    ]
  ]
]

dealership(Concessionária)
dealership
- id
- name
- slug
- nanoid
Model Dealership
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function dealershipBrands()
    {
        return $this->hasMany(BrandDealerships::class)
            ->with('brand');
    }

BrandDelaership(MarcasConcessionára)
brand_dealerships
- id
- dealership_id
- brand_id
Model BrandDealership
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function dealershipMotorcycle()
    {
        return $this->hasMany(DealershipMotorcycle::class, 'brand_id');
    }

DealershipMotorcycle(Concessionária Motocicleta)
dealership_motorcycles
- id
- brand_id
- motorcycle_id
Model DealershipMotorcycle
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function brandDealership()
    {
        return $this->belongsTo(BrandDealerships::class, 'brand_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function motorcycle()
    {
        return $this->belongsTo(Motorcycle::class, 'motorcycle_id');
    }
Método do Controller responsavel pelo Sync
    /**
     * Sync de Motorcycles e Stores
     *
     * @param Request $request
     * @param Model $model
     */
    public function syncMotorcycleStore(Request $request, Model $model)
    {
        dd($request->all());
        // $model->brands()->sync($request->input('brands'));

        $model->dealershipBrands();
    }
0 likes
1 reply

Please or to participate in this conversation.