lundi 12 novembre 2018

i need help passing variable from my foreach loop to my controller (laravel)

this is a part of my code

            @foreach($fields as $field)
            <div class="flex-row d-sm-inline-flex w-100 mt-1" dir="rtl">
                <input type="text" class="form-control w-25" name="field" id="field"  aria-describedby="emailHelp" placeholder="" />
                <input type="text" class="form-control w-50" name="description" id="description"  aria-describedby="emailHelp" placeholder="" />

                <button type="submit" class="btn btn-primary" name="submitbutton" id="del-field" value="del-field">پاک کردن فیلد</button>
            </div>

            @endforeach

in this part every field of my products shows. i add a button for every field to delete the field. problem is that i can not delete the field until i get the id of that field so how do i get the id from my foreach loop to my controller?

my full code

<div class="w-100">
  <form method="post" action="" enctype="multipart/form-data">
  
    <div class="album">
        <div class="slider">
                             <div class="slide slide1 mySlides">
                                 <div>
                                      <img  src="/image/products/product1.jpg"/>
                                 </div>
                                 <div class="caption">
                                     <input type="file" class="form-control" name="image1" id="image1"  aria-describedby="emailHelp" style="background-color: #7f7f7f; color: #fff; height: 3em; "/>
                                     <button type="submit" name="submitbutton" class="btn" value="save1" style="width: 100%; height: 100%;">تغییر عکس اول</button>
                                 </div>
                             </div>
                             <div class="slide slide2 mySlides">
                                 <div>
                                      <img  src="/image/products/product2.jpg"/>
                                 </div>
                                 <div class="caption">
                                     <input type="file" class="form-control" name="image2" id="image2"  aria-describedby="emailHelp" style="background-color: #7f7f7f; color: #fff; height: 3em; "/>
                                     <button type="submit" name="submitbutton" class="btn" value="save2" style="width: 100%; height: 100%;">تغییر عکس دوم</button>
                                 </div>
                             </div>
                             <div class="slide slide3 mySlides">
                                 <div>
                                      <img  src="/image/products/product3.jpg"/>
                                 </div>
                                 <div class="caption">
                                     <input type="file" class="form-control" name="image3" id="image3"  aria-describedby="emailHelp" style="background-color: #7f7f7f; color: #fff; height: 3em; "/>
                                     <button type="submit" name="submitbutton" class="btn" value="save3" style="width: 100%; height: 100%;">تغییر عکس سوم</button>
                                 </div>
                             </div>
   `    </div>
    </div>

      <button type="button" class="btn btn-primary" onclick="plusDivs(-1)">&#10094;</button>
      <button type="button" class="btn btn-primary" onclick="plusDivs(+1)">&#10095;</button>

      <div class="form-group mt-lg-5" dir="rtl">
        <label for ="description">توضیحات محصول</label>
        <textarea class="form-control" id="description" placeholder="Enter Your Message" maxlength="1000"></textarea>
      </div>
    <div>
        <div>
            @foreach($fields as $field)
            <div class="flex-row d-sm-inline-flex w-100 mt-1" dir="rtl">
                <input type="text" class="form-control w-25" name="field" id="field"  aria-describedby="emailHelp" placeholder="" />
                <input type="text" class="form-control w-50" name="description" id="description"  aria-describedby="emailHelp" placeholder="" />

                <button type="submit" class="btn btn-primary" name="submitbutton" id="del-field" value="del-field">پاک کردن فیلد</button>
            </div>

            @endforeach
        </div>
        <div>
           <button type="submit" class="btn btn-primary mt-5" name="submitbutton" value="add-field">اضافه کردن فیلد </button>
        </div>
    </div>
   </form>

   <script src="" defer></script>
</div>

and my controller

public function update(Request $request, $id)
{

    switch($request->input('submitbutton')) {

        case 'save1':

            $user = Auth::user();
            $products = Product::findOrFail($id);
            $destinationPath = public_path(). '/image/products/';

            $avatarName = $products->id . 'product1' . '.' . 'jpg';
            request()->image1->move($destinationPath, $avatarName);

            return Redirect::back();

            break;

        case 'save2':



            $user = Auth::user();
            $products = Product::findOrFail($id);
            $destinationPath = public_path(). '/image/products/';

            $avatarName = $products->id . 'product2' . '.' . 'jpg';
            request()->image2->move($destinationPath, $avatarName);

            return Redirect::back();

            break;

        case 'save3':


            $user = Auth::user();
            $products = Product::findOrFail($id);
            $destinationPath = public_path(). '/image/products/';

            $avatarName = $products->id . 'product3' . '.' . 'jpg';
            request()->image3->move($destinationPath, $avatarName);

            return Redirect::back();

            break;

        case 'add-field':

            $products = Product::findOrFail($id);
            $products->fields()->create();

            return Redirect::back();

            break;


        case 'del-field':



            return Redirect::back();

            break;

    }


}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire