Bob Mwenda
Bob Mwenda

Reputation: 89

Eloquent multiple rows insert and in one array loop

I have user input following the rules below;

public function rules()
    {
        return [
            'phone_number' => 'required|array',
            'amount' => 'required|string|max:4',
            'phone_number_debit' => 'required|string|max:15',
        ];
    }

I would want to save the data in a model Transaction. For the phone_number it is an array that could have one value or multiple. So that leaves for foreach loop.

This is what I want to achieve, save different rows determined by the number of records in the array.

$transaction = new Trasaction();
$transaction->phone_number = $req->phone_number; //Value in the array
$transaction->amount = $req->amount;
$transaction->phone_number_debit = $req->phone_number_debit;
$transaction->save();

Save diffrent records according to the records in the phone_number array.

However I can not think of a way to achieve this.

Anyone?

Upvotes: 2

Views: 2517

Answers (2)

Ian Rodrigues
Ian Rodrigues

Reputation: 743

There are many ways to do this, in a nutshell:

collect(request('phone_number'))->each(function ($phone) use ($req) {
    $transaction = new Trasaction();
    $transaction->phone_number = $phone; // element of the array
    $transaction->amount = $req->amount;
    $transaction->phone_number_debit = $req->phone_number_debit;
    $transaction->save();
});

TL;DR

One-to-Many Relationship

In order to get a better code, you can create a transaction_phones table, creating a one-to-many relationship.

You'll create a TransactionPhone model and add this:

public function transaction()
{
    return $this->belongsTo(Transaction::class);
}

The TransactionPhone migration:

Schema::create('transaction_phones', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('transaction_id');
    $table->string('phone_number');
    $table->timestamps();
});

In your Transaction model you'll have the inverse:

public function phones()
{
    return $this->hasMany(TransactionPhone::class);
}

public function addPhone($phone)
{
    return $this->phones()->create(['phone_number' => $phone]);
}

And in you Controller:

$transaction = Trasaction::create(request()->only('amount', 'phone_number_debit'));

collect(request('phone_number'))->each(function ($phone) use ($transaction) {
    $transaction->addPhone($phone);
});

I hope this answer can help you.

Upvotes: 1

yoeunes
yoeunes

Reputation: 2945

try this :

$data = request(['amount', 'phone_number', 'phone_number_debit']);

foreach($data['phone_number'] as $phone_number) {
    Trasaction::create([
       'amount' => $data['amout'],
       'phone_number' => $phone_number,
       'phone_number_debit' => $data['phone_number_debit']
    ]);
}

make sure in your Trasaction modal you've set to fillable property like this :

class Trasaction extends Model 
{
    protected $fillable = ['amount', 'phone_number', 'phone_number_debit'];
}

Upvotes: 2

Related Questions