2

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?

4
  • Can you create an entry for each phone_number element? Commented Aug 24, 2017 at 17:48
  • @IanRodrigues how do I achieve this? The array is in the same request as others. Commented Aug 24, 2017 at 17:50
  • Why not create a child table? One Transaction would have many phone numbers. It would make it easier to manage. Commented Aug 24, 2017 at 17:55
  • You spelled transaction wrong in your example btw Commented Aug 24, 2017 at 18:25

2 Answers 2

2

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'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

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.