每次单击“编辑”时,我都会获得最后插入的记录。当我单击特定记录的编辑按钮时,如何获得特定ID?

时间:2019-04-04 09:16:47

标签: laravel mongodb laravel-5 eloquent edit

每次,当我单击“编辑”时,我都会得到最后插入的记录。当我单击特定记录的编辑按钮时,如何获得特定ID?

我在其中添加了2个弹出框,其中“添加假日”将弹出,添加假日的弹出窗口,而编辑假日将弹出,编辑假日的弹出框。

如何解决该问题?

这是我的查看文件代码

@extends('layouts.master')
@section('content')
<section>
<div class="page-wrapper">
    <div class="container-fluid">
        <div class="row page-titles">
            <div class="col-md-5 align-self-
            </div>
        </div>


        <div class="card">
            <div class="card-body">
                <a href="javascript:void(0)" data-toggle="modal" data-target="#myModalAdd" class="btn btn-info text-white">+ Add Holidays</a>

                <div id="myModalAdd" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabelAdd" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title" id="myModalLabelAdd">Add Holidays</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>

                            <div class="modal-body">
                                <form class="form-horizontal" method="POST" action="{{route('store_holidays')}}">
                                @csrf
                                    <div class="form-group">
                                        <label class="col-md-12">Date:</label>
                                        <div class="col-md-12">
                                            <input type="date" name="holidaydate" class="form-control" placeholder="dd/mm/yyyy">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-12">Occasion</label>
                                        <div class="col-md-12">
                                            <input type="text" class="form-control" name="occasion" placeholder="Occasion">
                                        </div>
                                    </div>

                            <div class="modal-footer">
                                <button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Save</button>
                                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
                            </div>
                        </form>
                    </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                    <!-- /.modal-dialog -->
                </div>

                    <div class="table-responsive m-t-40">
                            <table class="table table-bordered table-striped ">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>Occasion</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($publicHolidays as $holiday)
                                        <tr>
                                            <td>{{$holiday->holidaydate}}</td>
                                            <td>{{$holiday->occasion}}</td>
                                            <td>
                                                <a href="{{url('/edit-holiday', $holiday->id)}}"  data-toggle="modal" data-target="#myModal"><i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i></a>
                                                <a href="{{url('/delete-holiday', $holiday->id)}}"><i class="fa fa-trash fa-lg" style="color:red" aria-hidden="true"></i></a>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>

                <div id="myModal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabelEdit" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title" id="myModalLabelEdit">Edit Holiday</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>

                            <div class="modal-body">
                                <form class="form-horizontal" method="POST" action="{{ url('edit-holiday', $holiday->id) }}">
                                @csrf
                                    <div class="form-group">
                                        <label class="col-md-12">Date:</label>
                                        <div class="col-md-12">
                                            <input type="date" name="holidaydate" class="form-control" placeholder="dd/mm/yyyy" value="{{$holiday->holidaydate}}">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-12">Occasion</label>
                                        <div class="col-md-12">
                                            <input type="text" class="form-control" name="occasion" placeholder="Occasion" value="{{$holiday->occasion}}">
                                        </div>
                                    </div>
                                </form>
                            </div>

                            <div class="modal-footer">
                                <button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Update</button>
                                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
</div>
</section>
@endsection

这是我的控制器文件代码

<?php

namespace App\Http\Controllers;

use App\Holiday;
use Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Hash;


class HolidayController extends Controller
{
    public function addHolidays(Request $request)
    {
        $holiday = new Holiday();

        $holiday->holidaydate = $request->get('holidaydate');
        $holiday->occasion = $request->get('occasion');
        $holiday->save();

        return redirect('list-holidays')->with('success', 'Holiday Added Successfully');
    }

    public function listHolidays()
    {
        $publicHolidays = Holiday::all();
        return view('pages.listholiday', compact('publicHolidays'));
    }

    public function editHoliday($id)
    {
        $holiday = Holiday::find($id);
        return view('pages.listholiday', compact('holiday', 'id'));
    }

    public function updateHoliday(Request $request, $id)
    {
        $holiday = Holiday::find($id);
        $holiday->holidaydate = $request->get('holidaydate');
        $holiday->occasion = $request->get('occasion');
        $holiday->save();
        return redirect('list-holidays')->with('success', 'Holiday Updated Successfully');
    }

    public function deleteHoliday($id)
    {
        $holiday = Holiday::find($id);
        $holiday->delete();
        return redirect('list-holidays')->with('Success', 'Record Deleted SuccessFully!');
    }

}

我的刀片文件是

@extends('layouts.master')
@section('content')
<section>
<div class="page-wrapper">
    <div class="container-fluid">
        <div class="row page-titles">
            <div class="col-md-5 align-self-center">
                <h4 class="text-themecolor">{{__(' Holiday Schedule')}}</h4>
            </div>
        </div>

        {{-- POP-UP Form --}}
        <div class="card">
            <div class="card-body">
                <a href="javascript:void(0)" data-toggle="modal" data-target="#myModalAdd" class="btn btn-info text-white">+ Add Holidays</a>

                <div id="myModalAdd" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabelAdd" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h4 class="modal-title" id="myModalLabelAdd">Add Holidays</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>

                            <div class="modal-body">

                                <form class="form-horizontal" method="POST" action="{{route('store_holidays')}}">
                                @csrf
                                    <div class="form-group">
                                        <label class="col-md-12">Date:</label>
                                        <div class="col-md-12">
                                            <input type="date" name="holidaydate" class="form-control" placeholder="dd/mm/yyyy">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-12">Occasion</label>
                                        <div class="col-md-12">
                                            <input type="text" class="form-control" name="occasion" placeholder="Occasion">
                                        </div>
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Save</button>
                                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                    <!-- /.modal-dialog -->
                </div>
                {{-- END POPUP FORM --}}
                    <div class="table-responsive m-t-40">
                            <table class="table table-bordered table-striped ">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>Occasion</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($publicHolidays as $holiday)
                                        <tr>
                                            <td>{{$holiday->holidaydate}}</td>
                                            <td>{{$holiday->occasion}}</td>
                                            <td>
                                                {{-- <a href="{{url('/edit-holiday', $holiday->id)}}"  data-toggle="modal" data-target="#myModal"><i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i></a> --}}
                                                <a class="holiday_modal" href="{{url('/edit-holiday', $holiday->id)}}"  data-toggle="modal" data-target="#myModal" data-holidaydate="{{ $holiday->holidaydate }}" data-action="{{ route('edit_holidays', $holiday->id) }}" data-occasion="{{$holiday->occasion}}"><i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i></a>
                                                <a href="{{url('/delete-holiday', $holiday->id)}}"><i class="fa fa-trash fa-lg" style="color:red" aria-hidden="true"></i></a>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div id="myModal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabelEdit" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                     <div class="modal-header">
                                          <h4 class="modal-title" id="myModalLabelEdit">Edit Holiday</h4>
                                              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                     </div>
                                   <div class="modal-body">
                                        <form class="form-horizontal" method="POST" id="action_modal">
                                         @csrf
                                             <div class="form-group">
                                                 <label class="col-md-12">Date:</label>
                                                  <div class="col-md-12">
                                                         <input type="date" name="holidaydate" class="form-control" placeholder="dd/mm/yyyy" id="holidaydate">
                                                    </div>
                                              </div>
                                               <div class="form-group">
                                                     <label class="col-md-12">Occasion</label>
                                                     <div class="col-md-12">
                                                         <input type="text" class="form-control" name="occasion" placeholder="Occasion" id="occasion">
                                                       </div>
                                                 </div>
                                          </form>
                                     </div>

                                     <div class="modal-footer">
                                           <button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Update</button>
                                           <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
                                       </div>
                                   </div>
                                </div>
                              </div>
                           </div>
            </div>
        </div>
</div>
<script type="text/javascript">
    $('.holiday_modal').click(function (event) {
        $('#holidaydate').attr('value', $(this).data('action'));
        $('#occasion').attr('value', $(this).data('occasion'));
        $('#action_modal').attr('action', $(this).data('action'));;

    })
</script>
</section>

@endsection

1 个答案:

答案 0 :(得分:0)

尝试一下

在您的编辑按钮中

<a class="holiday_modal" href="{{url('/edit-holiday', $holiday->id)}}"  data-toggle="modal" data-target="#myModal" data-holidaydate="{{ $holiday->holidaydate }}" data-action="{{ route('edit-holiday', $holiday->id) }}" data-occasion="{{$holiday->occasion}}"><i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i></a>

您的模态

<div id="myModal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabelEdit" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
             <div class="modal-header">
                  <h4 class="modal-title" id="myModalLabelEdit">Edit Holiday</h4>
                      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
             </div>
           <div class="modal-body">
                <form class="form-horizontal" method="POST" id="action_modal">
                 @csrf
                     <div class="form-group">
                         <label class="col-md-12">Date:</label>
                          <div class="col-md-12">
                                 <input type="date" name="holidaydate" class="form-control" placeholder="dd/mm/yyyy" id="holidaydate">
                            </div>
                      </div>
                       <div class="form-group">
                             <label class="col-md-12">Occasion</label>
                             <div class="col-md-12">
                                 <input type="text" class="form-control" name="occasion" placeholder="Occasion" id="occasion">
                               </div>
                         </div>
                  </form>
             </div>

             <div class="modal-footer">
                   <button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Update</button>
                   <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
               </div>
           </div>
        </div>
      </div>
   </div>

在文件末尾添加脚本

<script type="text/javascript">
        $('.holiday_modal').click(function (event) {
            $('#holidaydate').attr('value', $(this).data('holidaydate'));
            $('#occasion').attr('value', $(this).data('occasion'));
            $('#action_modal').attr('action', $(this).data('action'));;

        })
</script>

希望这会有所帮助:)