1
votes

I'm planning to send id on supplier to transaction_in

but i have this error

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (database_name.transaction_ins, CONSTRAINT transaction_ins_supplier_id_foreign FOREIGN KEY (Supplier_id) REFERENCES suppliers (id) ON UPDATE CASCADE) (SQL: insert into transaction_ins (Supplier_id, updated_at, created_at) values (0, 2019-10-21 07:54:15, 2019-10-21 07:54:15))

This is transaction_in migration table

public function up()
    {
        Schema::create('transaction_ins', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->date('tanggal_transaksi');
            $table->unsignedBigInteger('Supplier_id');
            $table->timestamps();

            //Foreign Key
            $table->foreign('Supplier_id')->references('id')->on('suppliers')->onUpdate('cascade');
        });
    }

transaction_in model

class Transaction_in extends Model
{
    protected $guarded = [];

    public function suppliers(){
        return $this->belongsTo(Supplier::class);
    }
}

This is supplier migration table

 public function up()
    {
        Schema::create('suppliers', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('nama');
            $table->timestamps();
        });
    }

supplier model

class Supplier extends Model
{
    protected $guarded = [];

    public function transactions_in(){
        return $this->hasMany(Transaction_in::class);
    }
}

I already try solution from SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row (Laravel 6)

When I run the query from accepted answer it does return empty, but I don't understand this part

If you get back an empty result set, then it means your insert is referring to data in order which does not exist.

But the data does exist in supplier table

enter image description here

What should i do to fix this?

This is create.blade.php for transaction_in

{{ Form::open(['action' => 'TransactionsINController@store', 'method' => 'POST']) }}
        <div class="form-group">
            {{ Form::label('supplier_name', 'Supplier Name') }}
            {{ Form::select('supplier_name', $supplierList->pluck('nama'), null, ['class' => 'form-control', 'placeholder' => 'Pick one Supplier...']) }}
        </div>
        <hr>
        {{ Form::button('<i class="far fa-save"></i> Submit', ['type' => 'submit', 'class' => 'btn btn-info'] )  }}
{{ Form::close() }} 

and this is the store method on transaction_in controller

public function store(Request $request)
    {
        $request->validate([
            'supplier_name' => 'required'
        ]);

        $transaction_in = new Transaction_in();
        $transaction_in->Supplier_id = $request->input('supplier_name');
        $transaction_in->save();
    }
1
the answer from that question you are referring to is demonstrating to you that you do not have a supplier matching the id you are trying to assign to Supplier_id it would seem - lagbox

1 Answers

1
votes

The select needs a list keyed by what the options value is supposed to be:

Form::select('supplier_id', $supplierList->pluck('nama', 'id'), null, ....);

Adjusted the input name to be more accurate.

Form::select('size', ['L' => 'Large', 'S' => 'Small'], ...);

LaravelCollective - HTML - Drop-Down Lists

Laravel 6.x Docs - Collections - pluck