如何解决同一页面中的更新记录查询?

时间:2019-04-08 04:58:17

标签: laravel mongodb laravel-5 eloquent


在我的列表视图中,我具有部门的所有详细信息。但是,当我单击详细信息时,它将显示一个弹出窗口。在“弹出”框中,它必须获取并提供给我特定字段的所有详细信息,但取而代之的是,它始终为我提供最后插入的记录的详细信息

这是我的刀片文件代码

@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">{{__('Department')}}</h4>
                </div>
            </div>

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

                    <div id="myModal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title" id="myModalLabel">Add Department </h4>
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                </div>
                                <div class="modal-body">
                                    <form id="myform" class="form-horizontal" method="POST" action="{{route('store_department')}}">
                                        @csrf
                                        <div class="form-group">

                                        @if(Session::has('key'))
                                            <?php $createdBy = Session::get('key')['username']; ?>
                                        @endif

                                            <div class="col-md-12">
                                                <input type="hidden" name="createdBy" value="<?php echo $createdBy ?>">
                                                <input type="text" class="form-control" name="nameOfDepartment" placeholder="Add New Department">
                                            </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>
                        </div>
                    </div>

                    <div class="table-responsive m-t-40">
                            <table class="table table-bordered table-striped ">
                                <thead>
                                    <tr>
                                        <th>Department Name</th>
                                        <th>Created By</th>
                                        <th>Created On</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                @if($listOfDepartment != null)
                                    @foreach($listOfDepartment as $departmentList)
                                        <tr>
                                            <td>{{$departmentList->nameOfDepartment}}</td>
                                            <td>{{$departmentList->createdBy}}</td>
                                            <td>{{$departmentList->created_at}}</td>
                                            <td>
                                                <a href="{{route('edit_department', $departmentList->id)}}"  data-toggle="modal" data-target="#myEditModal"><i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i></a>
                                                <a href="{{route('delete_department', $departmentList->id)}}"><i class="fa fa-trash fa-lg" style="color:red" aria-hidden="true"></i></a>
                                            </td>
                                        </tr>
                                    @endforeach
                                @endif
                                </tbody>
                            </table>
                        </div>
                    </div>

                <div id="myEditModal" 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 Department</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('update_department', $departmentList->id) }}">
                                @csrf
                                @method('PUT')
                                    <div class="form-group">
                                        <div class="col-md-12">
                                             <input type="text" name="nameOfDepartment" class="form-control" placeholder="Edit Department" value="{{$departmentList->nameOfDepartment}}">
                                        </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\Department;
use Illuminate\Http\Request;

class DepartmentController extends Controller
{
    public function createDepartment()
    {
        return view('pages.department');
    }

    public function storeDepartment(Request $request)
    {
        $department = new Department();
        $department->createdBy = $request->get('createdBy');
        $department->nameOfDepartment = $request->get('nameOfDepartment');
        $department->save();

        return redirect('list-department')->with('Success', 'Department Added Successfully!');
    }

    public function listDepartment()
    {
        $listOfDepartment = Department::all();
        return view('pages.department', compact('listOfDepartment'));
    }

    public function editDepartment($id)
    {
        $departments = Department::find($id);
        return view('pages.department', compact('departments', 'id'));
    }

    public function updateDepartment(Request $request, $id)
    {
        $department = Department::find($id);
        $department->createdby = $request->get('createdBy');
        $department->nameOfDepartment = $request->get('nameOfDepartment');
        $department->save();

        return redirect('list-department')->with('Success', 'Department Updated Successfully!');
    }

    public function deleteDepartment($id)
    {
        $department = Department::find($id);
        $department->delete();

        return redirect('list-department')->with('Success', 'Department Deleted SuccessFully!');
    }
}

这是我的路线

Route::get('add-department', 'DepartmentController@createDepartment')->name('create_department');
Route::post('store-department', 'DepartmentController@storeDepartment')->name('store_department');
Route::get('list-department', 'DepartmentController@listDepartment')->name('list_department');
Route::get('edit-department/{id}', 'DepartmentController@editDepartment')->name('edit_department');
Route::put('update-department/{id}', 'DepartmentController@updateDepartment')->name('update_department');
Route::get('delete-department/{id}', 'DepartmentController@deleteDepartment')->name('delete_department');

1 个答案:

答案 0 :(得分:0)

您的模态总是使用最后一个$departmentList

创建

您正在使用它来创建某种列表

@foreach($listOfDepartment as $departmentList)                                      
<tr>                                        
    <td>{{$departmentList->nameOfDepartment}}</td>                           
    <td>{{$departmentList->createdBy}}</td>
    <td>{{$departmentList->created_at}}</td>
    <td>                                    
        <a href="{{route('edit_department', $departmentList->id)}}"  data-toggle="modal" data-target="#myEditModal">
            <i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i>
        </a>                                            
        <a href="{{route('delete_department', $departmentList->id)}}">
            <i class="fa fa-trash fa-lg" style="color:red" aria-hidden="true"></i>
        </a>
    </td>
</tr>
@endforeach

您在此处结束循环,因此$departmentList是循环中的最后一个

稍后再添加一些使用$departmentList

的html
<form class="form-horizontal" method="POST" action="{{ route('update_department', $departmentList->id) }}">

此变量仍包含循环中的最后一个部门。

您可能可以利用一些JavaScript来打开模式,并在其中放置正确的发布网址。