dimanche 7 avril 2019

How can i resolve my update record query in same page?


I am coding in add department module, there is one add department button, when I click on that button it will display a pop up box and i can add one department from that and when I click on save in the same page, in table view it will display me a list of department, and in action column there is one edit button, click on that button it will display pop up box to edit the record, but problem is that,when i hover my mouse on edit at that time it will display id of particular record but whenever i click on any record to edit, all the time it will display last inserted record in pop up box.

Here is my code of blade file

@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"></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="">
                                        @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></td>
                                            <td></td>
                                            <td></td>
                                            <td>
                                                <a href=""  data-toggle="modal" data-target="#myEditModal"><i class="fa fa-edit fa-lg" style="color:#0066ff" aria-hidden="true"></i></a>
                                                <a href=""><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="">
                                @csrf
                                @method('PUT')
                                    <div class="form-group">
                                        <div class="col-md-12">
                                             <input type="text" name="nameOfDepartment" class="form-control" placeholder="Edit Department" value="">
                                        </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

here is my code of department controller

<?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!');
    }
}

And Here Are My Routes

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');



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire