将数据从 Modal 传递到 Laravel Controller@update

Pass data from Modal to Laravel Controller@update

我需要使用模态更新数据库中的某些列...问题是如何将数据从模态传递到 Controller@update?我可以获取模态的动态数据,但表单操作似乎不起作用。有人能帮我吗?我对此感到非常沮丧。

=( 这是我的代码,根据以下内容生成模态:

//路线

Route::resource('/dashboard', 'DashboardController');

//DashboardController.php

public function index(Request $request)
    {
        $equipments = Equipments::all();
        $serviceProviders = ServiceProviders::all();
        $engineers = Engineers::all();
        $equipmentsDue = Equipments::Where('due1', '<>', '1990-01-01')
                                ->orderBy($due1)
                                ->get();
        $customerSite = CustomerSites::all();
        return view('dashboard.index', compact('sort', 'customerSite', 'equipmentsDue', 'serviceProviders', 'engineers'));
    }

// /dashboard/index.blade.php

@foreach ($equipmentsDue as $equipment)
    <!------modal -->
    <div class="modal fade" id="myModal-{{ $equipment->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Next Due</h4>
            </div>
            <div class="modal-body">
                <form class="app-form" id="modal-form" action="/dashboard/update/{{ $equipment->id }}" method="POST">
                {{ csrf_field() }}
                <div class="form-group">
                    <label for="title">Next Due*</label>
                    <input type="text" class="form-control" id="datepicker1" name="due1" placeholder="Next Due">
                </div>
                <div class="row">
                    <div class="col-xs-6">
                        <div class="form-group">
                            <label for="title">Completed By:</label>
                            <!-- Service Provider -->
                            <select name="note1" class="form-control" id="note1">
                                @foreach ($serviceProviders as $serviceProvider)
                                    <option value="{{ $serviceProvider->id }}">{{ $serviceProvider->name }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-xs-6">
                        <div class="form-group">
                            <!-- Engineer -->
                            <label for="title">Engineer</label>
                            <select name="note2" class="form-control" id="note2">
                                @foreach ($engineers as $engineer)
                                    <option value="{{ $engineer->id }}">{{ $engineer->name }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group">
                        <div class="col-xs-12"> 
                            <label for="title">Comments:</label>
                            <textarea name="note3" class="form-control" id="note3" placeholder="comments"></textarea>
                        </div>
                    </div>
                </div>      
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-default" data-dismiss="modal">Save</button>
                </div>
                </form>
            </div>
        </div>
        </div>
    </div>
    <!-- modal ends-->  
    <tr class="alert-danger">
        <td><input type="checkbox"></td>
        <td align="center">{{ date("d-m-Y", strtotime($equipement->due1)) }}</td>
        <td align="center"><button type="button" class="btn btn-danger btn-xs">TMU</button></td>
        <td>{{ $equipment->CustomerSites->customer->name . " -> " . $equipment->CustomerSites->sitename }}</td>
        <td>{{ $equipment->CustomerSites->SiteRegions->name }}</td>
        <td>{{ $equipment->sn  }}</td>
        <td>{{ $equipment->cap  }}</td>
        <td></td>
        <td><button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#myModal-{{ $equipment->id}}">COMPLETE</button></td>
    </tr>
    @endif
@endforeach

这会正确生成 table 和模态:

Modal

URL为

为什么表单操作 dashboard@update 不调用?

因为 update 实际上是一个 PUT 调用,并且由于 form 无法对 Laravel Controller 进行正确的 PUT 调用,所以您需要 form method spoofing 并使用下面的代码:

<form class="app-form" id="modal-form" action="{{ route('dashboard', $equipment->id) }}" method="POST">
     {{ method_field('PUT') }}
     {{ csrf_field() }}
     ...

添加一条不同的路线: Route::POST('/dashboard/update/{id}', 'DashboardController@update');

控制器:

public function update(Request $request, $id) { $the_id = $id; //this is the id. dd($id); //to check your id value. }

并尝试 dd() 您的变量/输出以进行测试。 . .