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

andreasnovian's avatar

How to save input form into 3 different table database?

I created a form that will be inserted into different tables: 1.sales_order 2.sales_order_item 3.sequence_sales_order, but the data cannot be saved to the database, and when using dump it gets stuck before DB::beginTransaction();

Here, my controller code :

public function storesalesorder(Request $request)
    {
        $request->validate([
            'sales_order_no'=>'required',
        ]);
        dd('Before Transaction');
        DB::beginTransaction();
        try {
            $salesorder = new SalesOrder;
            $salesorder->quotation_no = $request->quotation_no;
            $salesorder->sales_order_no = $request->sales_order_no;
            $salesorder->company_name = $request->company_name;
            $salesorder->name = $request->name;
            $salesorder->date = $request->date;
            $salesorder->kbli = $request->kbli;
            $salesorder->email = $request->email;
            $salesorder->telephone = $request->telephone;
            $salesorder->po_no = $request->po_no;
            $salesorder->address = $request->address;
            $salesorder->npwp = $request->npwp;
            $salesorder->tax_address = $request->tax_address;
            $salesorder->scope_of_work_no = $request->scope_of_work_no;
            $salesorder->reference_number = $request->reference_number;
            $salesorder->work_unit = $request->work_unit;
            $salesorder->fax = $request->fax;
            $salesorder->radio_information = $request->radio_information;
            $salesorder->confirmation = $request->confirmation;
            $salesorder->special_requirement = $request->special_requirement;
            $salesorder->delivery_statement = $request->delivery_statement;
            $salesorder->term_of_payment = $request->term_of_payment;
            $salesorder->dod = $request->dod;
            $salesorder->image = $request->image;
            $salesorder->sample_reference = $request->sample_reference;
            $salesorder->assy_type = $request->assy_type;
            $salesorder->qc_statement = $request->qc_statement;
            $salesorder->packing_type = $request->packing_type;
            $salesorder->tax = $request->tax;
            $salesorder->discount = $request->discount;
            $salesorder->discount_percent = $request->discount_percent;
            $salesorder->total_amount = $request->total_amount;
            $salesorder->down_of_payment = $request->down_of_payment;
            $salesorder->down_of_payment_percent = $request->down_of_payment_percent;
            $salesorder->shippingpoint = $request->shippingpoint;
            $salesorder->salesman = $request->salesman;
            $salesorder->save();

            $sales_order_no = DB::table('sales_order')->orderBy('sales_order_no', 'DESC')->select('sales_order_no')->first();
            $sales_order_no = $sales_order_no->sales_order_no;
            dd('Inside Transaction');
            // Simpan item
            foreach ($request->item as $key => $items) {
                $salesorderitem['item'] = $items;
                $salesorderitem['sales_order_no'] = $sales_order_no;
                $salesorderitem['item_description'] = $request->item_description[$key];
                $salesorderitem['qty'] = $request->qty[$key];
                $salesorderitem['unit'] = $request->unit[$key];
                $salesorderitem['unit_price'] = $request->unit_price[$key];
                $salesorderitem['item_discount'] = $request->item_discount[$key];
                $salesorderitem['item_tax'] = $request->item_tax[$key];
                $salesorderitem['no_order'] = $request->no_order[$key];
                $salesorderitem['spec'] = $request->spec[$key];
                $salesorderitem['amount'] = $request->amount[$key];
                $salesorderitem['item_kbli'] = $request->item_kbli[$key];
                $salesorderitem['shipped'] = $request->shipped[$key];
                $salesorderitem['dept'] = $request->dept[$key];
                $salesorderitem['no_quote'] = $request->no_quote[$key];

                SalesOrderItem::create($salesorderitem);
            }

            DB::commit();
            dd('After commit');
            return redirect()->route('activities.salesorder')->with('success', 'Sales Order Added');
        } catch (\Exception $e) {
            DB::rollBack();
            dd('After rollback');
            return redirect()->back();
        }
    }

sales_order model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class SalesOrder extends Model
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'quotation_no',
        'sales_order_no',
        'company_name',
        'name',
        'date',
        'kbli',
        'email',
        'telephone',
        'po_no',
        'address',
        'npwp',
        'tax_address',
        'scope_of_work_no',
        'reference_number',
        'work_unit',
        'fax',
        'radio_information',
        'confirmation',
        'special_requirement',
        'delivery_statement',
        'term_of_payment',
        'dod',
        'image',
        'sample_reference',
        'assy_type',
        'qc_statement',
        'packing_type',
        'discount',
        'discount_percent',
        'total_amount',
        'down_of_payment' ,
        'down_of_payment_percent',
        'shippingpoint',
        'salesman' ,
    ];
}

sales_order_item model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class SalesOrderItem extends Model
{
    use HasApiTokens, HasFactory, Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'sales_order_no',
        'item',
        'item_description',
        'qty',
        'unit',
        'unit_price',
        'item_discount',
        'item_tax',
        'no_order',
        'spec',
        'amount',
        'item_kbli',
        'shipped',
        'dept',
        'no_quote',
    ];
}
0 likes
11 replies
tangtang's avatar

@andreasnovian

stuck before DB::beginTransaction();

using dd will stop the process on the line this dd is written.

		$request->validate([
            'sales_order_no'=>'required',
        ]);
        dd('Before Transaction');
        DB::beginTransaction();

in your code the dd is before the beginTransaction so it will stop the process here.

if you do something like this

		dd('here the dd');
		$request->validate([
            'sales_order_no'=>'required',
        ]);
        dd('Before Transaction');
        DB::beginTransaction();

it will stop the process before the validation.

so comment or delete all this dd an let the real error show.

1 like
andreasnovian's avatar

@tangtang Ah, i see. But if i remove the dd, the form cant store into database and return back to form. So i think to use dd to check it

tangtang's avatar

@andreasnovian

in this table

1.sales_order

2.sales_order_item

3.sequence_sales_order

if you check the db, where the data isn't stored ? or all of this table not even have the data from the post ?

and what the method for this post ? is this standar submit without js/ajax ?

tangtang's avatar

@andreasnovian

really and there no errors ?

and check the model, I just realized there no protected table here. so with this SalesOrder model, is your table name in database follow the plural form of the model's like sales_orders ?

and other model, is the table name have the s too ?

andreasnovian's avatar

@tangtang my SalesOrder models :

class SalesOrder extends Model
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'quotation_no',
        'sales_order_no',
        'company_name',
        'name',
        'date',
        'kbli',
        'email',
        'telephone',
        'po_no',
        'address',
        'npwp',
        'tax_address',
        'scope_of_work_no',
        'reference_number',
        'work_unit',
        'fax',
        'radio_information',
        'confirmation',
        'special_requirement',
        'delivery_statement',
        'term_of_payment',
        'dod',
        'image',
        'sample_reference',
        'assy_type',
        'qc_statement',
        'packing_type',
        'discount',
        'discount_percent',
        'total_amount',
        'down_of_payment' ,
        'down_of_payment_percent',
        'shippingpoint',
        'salesman' ,
    ];
}
tangtang's avatar

@andreasnovian

yes, I already see the model from your question.

what I mean is, like what this doc says https://laravel.com/docs/10.x/eloquent#table-names

if your table name not follow the plural form of the model's

you need to add the protected $table in your model

class SalesOrder extends Model
{
    use HasApiTokens, HasFactory, Notifiable;

	protected $table = 'sales_order'; // you need to add this code if the table name is not sales_orders

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
       // fillable here
    ];
}

you can spot the difference in this table sales_order and this sales_orders right ?

there an s in the table name if using plural from the model name

add this protected $table = 'your_table_name' in other model too

1 like
andreasnovian's avatar

@tangtang Done, know how to save 3 different tables? if I check with dd($request->all()); all my forms are completely filled. but if I comment dd and submit it, it returns to the forms page with no errors and all the forms are reset to empty fields

tangtang's avatar
tangtang
Best Answer
Level 6

@andreasnovian

that's odd, your other code is fine beside the recent model before you updated it.

is your validated item is just this sales_order_no ? or there another field ?

it returns to the forms page with no errors and all the forms are reset to empty fields

this happen because there something wrong with validation, and you haven't use the old method on your form. like

<input type="text" name="sales_order_no" value="{{ old('sales_order_no') }}" required>

you see there an {{ old('field_name') }} in the input. with this method, even the validation throw an error, the form will not reset to empty.

know how to save 3 different tables?

your code is in right way to do this save data for 3 tables, your issue is just how to catch this validation error (from the behavior of post data is back to form, but the form is reset and empty)

1 like
johndivam's avatar

So many columns in one table that not good for performance, split it in 2 tables or more!

1 like

Please or to participate in this conversation.