0

Here i given below my table schema

Schema::create('yarn_work_order_items', function (Blueprint $table) {
        $table->id();
        $table->foreignId('yarn_work_order_id')->constrained('yarn_work_orders');
        $table->foreignId('yarn_type_id')->constrained('yarn_types');
        $table->foreignId('yarn_count_id')->constrained('yarn_counts');
        $table->foreignId('unit_id')->constrained('units');
        $table->foreignId('color_id')->nullable()->constrained('colors');
        $table->bigInteger('unit_price', false, true);
        $table->bigInteger('total_qty', false, true);
        $table->bigInteger('org_qty', false, true);
        $table->string('remarks')->nullable();
        $table->timestamps();

        $table->unique(['yarn_type_id', 'yarn_count_id', 'unit_price'], 'yarn_work_order_unique_combination');
    });

I want to insert record multiple times using for loop. Here are given below my validation ruels. But unique composite validation not work. How do i find out where is my error.

public function rules(): array
{
    return [
        'buyer_id'                  => ['required', 'integer', 'exists:buyers,id'],
        'style_id'                  => ['required', 'integer', 'exists:styles,id'],
        'order_id'                  => ['required', 'integer', 'exists:orders,id'],
        'supplier_id'               => ['required', 'integer', 'exists:suppliers,id'],
        'company_id'                => ['required', 'integer', 'exists:companies,id'],
        'season_id'                 => ['required', 'integer', 'exists:seasons,id'],
        'type_of_wo'                => ['required'],
        'wo_date'                   => ['required', 'date_format:Y-m-d'],
        'items'                     => ['required', 'array', 'min:1'],
        'items.*.yarn_type_id'      => [
                                            'required',
                                            'integer',
                                            'exists:yarn_types,id',
                                            Rule::unique('yarn_work_order_items')
                                                ->where(function($query) {
                                                    return $query->where('yarn_type_id', $this->input('items.*.yarn_type_id'))
                                                        ->where('yarn_count_id', $this->input('items.*.yarn_count_id'))
                                                        ->where('unit_price', $this->input('items.*.unit_price'));
                                                }),
                                        ],
        'items.*.yarn_count_id'     => ['required', 'integer', 'exists:yarn_counts,id'],
        'items.*.unit_id'           => ['required', 'integer', 'exists:units,id'],
        'items.*.color_id'          => ['nullable', 'integer', 'exists:colors,id'],
        'items.*.unit_price'        => [ 'required', 'numeric'],
        'items.*.total_qty'         => ['required', 'numeric']
    ];
}
2
  • 1
    Share what error you are getting regarding validation ?does your migration file successfully created the table in db? Commented Sep 22, 2024 at 17:11
  • Yes, my migration file was successfully created the table in db. I got this kind of error message when try to give duplicate data. Exception is throwing but validation not work. Here is exception SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1-500' for key 'yarn_work_order_items.yarn_work_order_unique_combination' (Connection: mysql, SQL: insert into.... Illuminate  \  Database  \  UniqueConstraintViolationException Commented Sep 23, 2024 at 3:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.