mercredi 29 janvier 2020

Laravel resource route is not updating in database using

Laravel resource Route is not updating when i click update button, but it has store at same process.

I am using data-toggle="modal" for updating my value, but it is not update my category?

Where exactly my problem? i check my id is passed properly, but it is not going to my update controller function. i check using dd($request->all()); in my update function in controller.

my route is

Route::resource('/mother-category', 'MotherCategoryController');

my update and store function is

public function store(Request $request)
    {
        $validator =Validator::make($request->all(), [
            'mother_name' => 'required',

        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator);
        }

        MotherCategory::insert([
            'mother_name'=>$request->mother_name
        ]);

        return redirect()->back()->with('message','Mother Category Created Successfully');
    }



    public function update(Request $request, MotherCategory $motherCategory)
    {

        $motherCategory->update([
            'mother_name' => $request->mother_name
        ]);

        return redirect()->back()->with('message','Mother Category Updated successfully');
    }

and my blade file is


@extends('layouts.master')

@section('title', 'Add Mother Category')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-lg-10 mx-auto">
                <form action="" method="post">
                    @csrf
                    <div class="card">
                        <div class="card-header">
                            <h4 style="text-align: center">Add a mother category</h4>
                        </div>
                        <div class="card-body">
                            <div class="form-row mb-3">
                                <div class="col">
                                    <small class="text-uppercase text-dark">Mother Category<span
                                            class="required text-danger">*</span></small>
                                    <input type="text" id="mother_name" name="mother_name" class="form-control"
                                           placeholder="Mother Category" required>

                                    @if ($errors->has('mother_name'))
                                        <small
                                            class="form-control-feedback text-danger"></small>
                                    @endif
                                </div>
                            </div>
                            <div class="form-row mb-3">
                                <div class="col">
                                    <button type="submit" class="btn btn-success"><i class="fa fa-check"></i> Add
                                    </button>
                                    <button type="button" class="btn btn-inverse">Cancel</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <br>

        <div class="row">
            <div class="col-md-12">
                <div class="card comp-card">
                    <div class="card-body">
                        <h5 class="w-100 text-center">All Mother categories</h5>
                        <div class="table-responsive">
                            <table class="table table-hover" id="motherTable">
                                <thead>
                                <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">Name</th>
                                    <th scope="col">Action</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach(App\Models\MotherCategory::all() as $index => $mc)
                                    <tr>
                                        <th scope="row"></th>
                                        <td></td>
                                        <td>
                                            <a href="#" class="btn btn-sm btn-info" data-toggle="modal"
                                               data-target="#exampleModalCenter">Edit</a>
                                            <a id="deleteBtn" data-id="" href="#"
                                               class="btn btn-sm btn-danger">Delete</a>
                                        </td>
                                    </tr>
                                    <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog"
                                         aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                                        <div class="modal-dialog modal-dialog-centered" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h5 class="modal-title" id="exampleModalCenterTitle">Update</h5>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    <form action=""
                                                          method="post">
                                                        
                                                        
                                                        <div class="form-group">
                                                            <label for="exampleInputEmail1">Name</label>
                                                            "> --}}
                                                            <input name="mother_name" type="text" class="form-control"
                                                                   id="exampleInputEmail1" aria-describedby="emailHelp"
                                                                   placeholder="Enter mother category"
                                                                   value=""><br><br>
                                                            <input type="submit" class=" btn btn-success"
                                                                   value="Update">
                                                        </div>
                                                    </form>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary"
                                                            data-dismiss="modal">Close
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection



@section('script')

    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.5/js/bootstrap-select.min.js"></script>

    <script>
        $('#motherTable').DataTable({

        });
    </script>

    <script>
        $(document).on('click', '#deleteBtn', function (el) {
            var mcId = $(this).data("id");
            swal({
                title: "Are you sure?",
                text: "Once deleted, you will not be able to recover this category!",
                icon: "warning",
                buttons: true,
                dangerMode: true,
            })
                .then((willDelete) => {
                    if (willDelete) {
                        swal("You have deleted a mother category!", {
                            icon: "success",
                        });
                        window.location.href = window.location.href = "mother-category/delete/" + mcId;
                    }


                });
        });

        $(document).on('click', '#deleteNo', function (el) {
            swal("Oops", "You can't delete this. Some item belongs to it!!", "error")
        })
    </script>
@endsection



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire