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

Amalmax's avatar

How to Update one form data to different tables?

I am working with Laravel 5.6 and MySQL. in My app I have some data edit form and its updated values saving with 3 different tables. called vehicles, cars and upload My edit form url as following,

http://localhost:8000/myads/17/edit

number (17) is vehicle table id number, like this,

id    name         owner  
1         toyota      dane
2        BMW        peter
3        Benz         samantha
4        volvo        Alex

and car table is like this,

id     fuel    vehicle_id
1          P              1
2         D              3
3          E              2

I need update cars table data with regarding to vehicle_id , My VehicleController vehicle table data update function is like this,

public function update(Request $request, $id)
    {
        $vehicle = Vehicle::find($id);

        $vehicle->name = $request->input('name');
        $vehicle->owner = $request->input('owner');
        
        $vehicle->save();
}

now I need update cars table fuel values with same form optional values input. how can I do this in the Controller?

My fuel selection input is like this,

<div class="form-group">
        <label for="exampleFormControlSelect1">Fuel</label>
        <select class="form-control" id="fuel" name="fuel">
            @foreach($vehicles->cars as $car)
            <option value="{!! $car->fuel  !!}">{!! $car->fuel  !!}</option>
            @endforeach 
        <option value="P">Petrol</option>
        <option value="D">Diesel</option>
        <option value="H">Hybrid</option>
        <option value="E">Electric</option>
       
        </select>
        </div> 
0 likes
7 replies
Cronix's avatar

What do your relationships look like? If you have a car relationship on the vehicle model, and it's a one-to-one relationship (each vehicle is tied to only one car) you could just do

    public function update(Request $request, $id)
    {
        $vehicle = Vehicle::find($id);

        $vehicle->name = $request->input('name');
        $vehicle->owner = $request->input('owner');
        
        $vehicle->save();

        //update the fuel for this car belonging to the vehicle
        $vehicle->car()->update(['fuel' => $request->input('fuel')]);
    }

https://laravel.com/docs/5.7/eloquent-relationships#inserting-and-updating-related-models

Amalmax's avatar

@Cronix I have many to many relationship with vehicles and cars that means

in My Vehicle Model

public function cars()
    {
        return $this->hasMany(Car::class);
    }

and Car Model,

public function vehicle()
    {
        return $this->belongsTo(Vehicle::class);
    }
Amalmax's avatar

@Cronix its generated following error

 (1/1) BadMethodCallException

Call to undefined method Illuminate\Database\Query\Builder::car()
Cronix's avatar
Cronix
Best Answer
Level 67

I can't tell by your partial incomplete form snippet how you'd know what car to update the fuel then. All I see is a select element for the fuel. Do you send the car id in addition to the vehicle id?

If you are then you'd just need to specify it on the car() relationship

$vehicle->cars()->where('id', $car_id)->update(['fuel' => $request->input('fuel')]);
2 likes
Cronix's avatar

its generated following error

Yes, I said if you have a car() relationship. I didn't know the names of your relationships. Change it to cars() since that's what you're calling it.

Anthonynzube's avatar

This answer just bailed me out after almost a whole day's research

Please or to participate in this conversation.