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

时间:2018-02-26 08:54:44

标签: laravel modal-dialog laravel-5.3 bootstrap-modal

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

=(这是我的代码,即生成模式:

// route

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

这正确地生成了表格和模态:

Modal

的网址

为什么表单操作仪表板@ update不会调用?

2 个答案:

答案 0 :(得分:1)

由于update实际上是PUT来电,并且由于form无法对PUT进行正确Laravel Controller来电,因此您需要form method spoofing并使用以下代码:

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

答案 1 :(得分:0)

添加不同的溃败: 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()变量/输出进行测试。 。 。