1
votes

I have a transaction table in database. The relation for the transaction table is, customer can send many transactions. A branch can have many transactions and a transaction handler can serve many transactions. I have defined models and relations. But don't know how to save the relations as save one first will make other fk null. Is there any method to save all at once or my database relation is wrong?

My schema is

enter image description here

And my controller code to save the relation is

//Save models related to transactions
        $transaction = new Transaction($request->input('partCTransaction'));
        //Customer can have many transactions
        $customer->transactions()->save($transaction);
        //branch has many reporting entities and a branch handles many transactions
        $branch->transactions()->save($transaction);
        //A transaction handler handles different transactions
        $transactionHandler->transactions()->save($transaction);

Any idea on how to resolve this issue.

My error is

General error: 1364 Field 'branch_id' doesn't have a default value (SQL: insert into `transactions` (`date`, `ref_number`, `customer_id`, `updated_at`, `created_at`) values (, , 9, 2018-06-25 23:36:11, 2018-06-25 23:36:11))
1
The easiest way around this is probably just make all the foreign key fields nullable. You can enforce that they are properly set later in code.Brian Lee
uh, i dont see you set the branch relation for the transaction..Bagus Tesa
@BagusTesa branch relation is already initialised. Just saving relation is producing the error.Jivan Bhandari
@DigitalDrifter Is this the good idea to set foreign key null?Jivan Bhandari
I think you should change your database design to use above code (add transaction_id to branch table and customer tables), or you should do "new Transaction" with manually added branch_id and customer_idMurat Tutumlu

1 Answers

2
votes

Your Transaction model should have customer, branch, and transactionHandler belongsTo relationships:

public function customer()
{
    return $this->belongsTo(Customer::class);
}

public function branch()
{
    return $this->belongsTo(Branch::class);
}

public function transactionHandler()
{
    return $this->belongsTo(TransactionHandler::class);
}

The belongsTo relationship has an associate() method that sets the relationship field, but does not save the record. You can use these relationships to setup all the appropriate relations, and then call save() when you're done.

$transaction = new Transaction($request->input('partCTransaction'));
$transaction->customer()->associate($customer);
$transaction->branch()->associate($branch);
$transaction->transactionHandler()->associate($transactionHandler);
$transaction->save();

Just a side note, associate() returns the child model, so you can chain all these together if you prefer that look:

$transaction = new Transaction($request->input('partCTransaction'));
$transaction
    ->customer()->associate($customer)
    ->branch()->associate($branch)
    ->transactionHandler()->associate($transactionHandler)
    ->save();