Msoft
1 year ago

how to save one form data to two data tables in laravel 5.6

Posted 1 year ago by Msoft

Hi, I am working with laravel 5.6 and Mysql db. I have form to save vehicle data to vehicle table. My VehicleController.php is this,

 public function store(Request $request)
    {
        $this->validate($request,[
            'district' => 'required',
            'town' => 'required',
            'brand' => 'required',
            'model' => 'required',
            'year' => 'required',
            'optradio' => 'required',
            'milage' => 'required',
            'tramsmission' => 'required',
            'fuel' => 'required',
            'enginecap' => 'required',
            'data' => 'required',
            'price' => 'required',
            'telephone' => 'required',
            'email' => 'required',
            'name' => 'required',
            'filename' => 'required',


        ]);

        $vehicle = new Vehicle;
        $vehicle->district = $request->input('district');
        $vehicle->town = $request->input('town');
        $vehicle->brand = $request->input('brand');
        $vehicle->model = $request->input('model');
        $vehicle->year = $request->input('year');
        $vehicle->condition = $request->input('optradio');
        $vehicle->milage = $request->input('milage');
        $vehicle->transmission = $request->input('tramsmission');
        $vehicle->fuel_type = $request->input('fuel');
        $vehicle->engine_capacity = $request->input('enginecap');
        $vehicle->detail = $request->input('data');
        $vehicle->price = $request->input('price');
        $vehicle->telephone = $request->input('telephone');
        $vehicle->email = $request->input('email');
        $vehicle->name = $request->input('name');
 $vehicle->save();
}

Now I need save some input data to another table in the form. like "Car", table. I am going to save following input values to new table

       $vehicle->milage = $request->input('milage');
       $vehicle->transmission = $request->input('tramsmission');
       $vehicle->fuel_type = $request->input('fuel');
       $vehicle->engine_capacity = $request->input('enginecap');

How can I do this? may I need another Controller like CarController or may I can save two table withing vehicleController?

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