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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在PHP Laravel中将变量从foreach循环传递给我的控制器相关的知识,希望对你有一定的参考价值。

这是我的代码的一部分

            @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;

    }


}
答案
<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>

以上是关于在PHP Laravel中将变量从foreach循环传递给我的控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何在 laravel 5.8 中将变量传递给路由?

我们如何在 laravel php 刀片模板中为 foreach 循环定义循环变量

Laravel 7:foreach 循环中未定义的变量

如何在控制器传递变量(laravel)中使用刀片进行 foreach

php foreach为啥比for效率高

laravel:当计数达到给定值时如何从 foreach 循环中删除选定的数据