首页 > 解决方案 > POST 方法

在 laravel 5.6 中的刀片文件上的 @foreach 内不起作用

问题描述

在@foreach 中使用表单后,按钮不起作用。关于如何在 @foreach 循环中使用表单的任何建议?

甚至可以在 laravel 刀片文件的循环中使用表单吗?

这是我的 laravel 刀片文件的代码。我在 foreach 中使用了表单来获取账面金额的数据。关于如何解决这个问题的任何建议?

@extends('layouts.app')

@section('content')
<div class="container">
  <h1>Items On Cart:</h4>
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif
                        <table style="border-collapse: collapse;width: 100%;">
                        <tr style="border: 1px solid #dddddd;text-align: left;padding: 8px;font-size:20px;color: gray">
                            <th>Book Name</th>
                            <th>Author Name</th>
                            <th>Book Amount</th>
                            <th>Price</th>
                            <th>Action</th>
                            <th>Action</th>
                        </tr>
                        @foreach($cart_items as $data)
                        <form action="{{ route('change_item_amount',[$cart_info->id,$data->book_id]) }}" method="POST">
                        <tr>
                            <th>{{ show_books_name($data->book_id) }}</th>
                            <th>{{ show_books_author($data->book_id)  }}</th>
                            <th><input type="number" name="amount" id="amount" value="{{ $data->item_amount }}"></th>
                            <th>{{ $data->price }} tk</th>
                            <th><button type="submit" class="btn btn-success">Change</button></th>
                            <th><a class="btn btn-success" href="{{ route('delete_book',[$cart_info->id,$data->book_id] ) }}">Delete</a></th>
                            </form>
                                @endforeach
                                </table>
                            </div>
                            <h2 style="padding-left:865px">Total = {{ $cart_info->total_price }} tk</h2>
                        </div>
                    </form>
                </div>
            </div>
  </div>
@endsection

标签: phplaravelformsforeach

解决方案


最简单的解决方案可以是:将表单标签放入td, (顺便说一句,它应该是td,而不是th,你使用thin thead):

<table style="border-collapse: collapse;width: 100%;">
    <tr style="border: 1px solid #dddddd;text-align: left;padding: 8px;font-size:20px;color: gray">
        <th>Book Name</th>
        <th>Author Name</th>
        <th>Book Amount</th>
        <th>Price</th>
        <th>Action</th>
        <th>Action</th>
    </tr>
@foreach($cart_items as $data)
    <tr>
        <td>{{ show_books_name($data->book_id) }}</td>
        <td>{{ show_books_author($data->book_id)  }}</td>
        <td>&nbsp; <!-- Here we have empty td now --> </td>
        <td>{{ $data->price }} tk</td>
        <td>
            <!-- Form is here now -->
            <form action="{{ route('change_item_amount',[$cart_info->id,$data->book_id]) }}" method="POST">
                <input type="number" name="amount" id="amount" value="{{ $data->item_amount }}">
                <button type="submit" class="btn btn-success">Change</button>
            </form>
        </td>
        <td><a class="btn btn-success" href="{{ route('delete_book',[$cart_info->id,$data->book_id] ) }}">Delete</a></td>
@endforeach
</table>

推荐阅读