在PHP Laravel中将变量从foreach循环传递给我的控制器

问题描述 投票:0回答:1

这是我的代码的一部分

            @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" id="{{$field->id}}field"  aria-describedby="emailHelp" placeholder="{{$field->name}}" />
                <input type="text" class="form-control w-50" name="{{$field->id}}description" id="{{$field->id}}description"  aria-describedby="emailHelp" placeholder="{{$field->description}}" />

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

            @endforeach

在这部分我的产品的每个领域都显示出来。我为每个字段添加一个按钮来删除该字段。问题是我不能删除该字段,直到我得到该字段的ID,所以如何从我的foreach循环到我的控制器的ID?

我的完整代码

<div class="w-100">
  <form method="post" action="{{route('ProductEdit', ['id' => $products->id])}}" enctype="multipart/form-data">
  {{ csrf_field() }}
    <div class="album">
        <div class="slider">
                             <div class="slide slide1 mySlides">
                                 <div>
                                      <img  src="/image/products/{{$products->id}}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/{{$products->id}}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/{{$products->id}}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" id="{{$field->id}}field"  aria-describedby="emailHelp" placeholder="{{$field->name}}" />
                <input type="text" class="form-control w-50" name="{{$field->id}}description" id="{{$field->id}}description"  aria-describedby="emailHelp" placeholder="{{$field->description}}" />

                <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="{{ asset('js/slider.js') }}" defer></script>
</div>

和我的控制器

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;

    }


}
javascript php html5 laravel-5
1个回答
1
投票
<script>
            $(document).ready(function(){
                var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
                $("#del-field").click(function(){
                    $.ajax({
                        /* the route pointing to the update function */
                        url: '/',
                        type: 'POST',
                        /* send the csrf-token and the input to the controller */
                        data: {_token: CSRF_TOKEN, id:$(this).val()},
                        dataType: 'JSON',
                        /* remind that 'data' is the response of the Controller */
                        success: function (data) {

                        }
                    }); 
                });
           });    
</script>
© www.soinside.com 2019 - 2024. All rights reserved.