How to enter stipulated amount of the same record in the database with different id in LARAVEL

Asked

Viewed 49 times

2

I have a form that already creates one record at a time in the bank. I would like to inform in this same form through a quantity field ($request->Quant) where I can enter the amount to be created or duplicated from that same record with different id.

Follow the code of my Controller:

public function criarRegistro(Request $request)
{


    $this->validate($request,[
        'category_id' => 'required',
        'card_details' => 'required',
        'card_image' => 'mimes:jpg,jpeg,png,svg',
        'impuls' => 'required',
    ]);



    if($request->hasFile('card_image')){
        $image = $request->file('card_image');
        $imageName = time().'.jpg';
        $location = 'assets/images/cardimage/'.$imageName;
        Image::make($image)->save($location);
        $name = $imageName;
    }else{
        $name = '';
        $impuls = '';

    }

    $cat = cardsubcategory::find($request->category_id );


    // AQUI ONDE ESTOU TENTANDO FAZER A MULTIPLICAÇÃO DO MESMO REGISTRO 
     $numero = $request->quant * $cat;




    card::create([
       'sub_category_id' => $request->category_id,
       'card_details' => $request->card_details,
       'card_image' => $name,
       'category_id' => $cat->id,
       'status' => 1,
       'tipo' => 1,
       'impuls' => $request->impuls,
    ]);


    return back()->with('success', 'Criado com Sucesso');
}
  • You want to duplicate the cardsubcategory?

1 answer

2


You can put the create in a loop for to solve this problem

public function criarRegistro(Request $request)
{
    $this->validate($request,[
        'category_id' => 'required',
        'card_details' => 'required',
        'card_image' => 'mimes:jpg,jpeg,png,svg',
        'impuls' => 'required',
    ]);

    if($request->hasFile('card_image')){
        $image = $request->file('card_image');
        $imageName = time().'.jpg';
        $location = 'assets/images/cardimage/'.$imageName;
        Image::make($image)->save($location);
        $name = $imageName;
    }else{
        $name = '';
        $impuls = '';
    }

    //Laço para percorrer a quantidade definida em $request->quant
    for ($i = 0; $i < $request->quant; $i++) {
        card::create([
           'sub_category_id' => $request->category_id,
           'card_details' => $request->card_details,
           'card_image' => $name,
           'category_id' => $request->category_id,
           'status' => 1,
           'tipo' => 1,
           'impuls' => $request->impuls,
        ]);
    }

    return back()->with('success', 'Criado com Sucesso');
}
  • That’s right, it was worth too much. Solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.