vendredi 1 juin 2018

Laravel model form does not load data from database

I am creating an WebApp using Laravel 5.6. I have the database table policies. I can able to add data to the table using validator and jQuery to save, edit and delete records. But the problems is the edit loads the data from database except name field.

Table records

Model doesn't fetch name field value

PolicyController

public function update(Request $request)
    {
        $validator = Validator::make($request->all() , [
        //  'branch_Id' => 'required|max:25|unique:branches,branchId',
        'name' => 'required|max:25',
        'description' => 'max:50']);

        if ($validator->passes())
        {
            $policy = Policy::find($request->policy_Id);
            $policy->name = $request->name;
            $policy->description = $request->description;
            $policy->save();
            return response()
                ->json($policy);
        }
        return response()->json(['error' => $validator->errors()
            ->all() ]);
    }

editPolicy.blade.php

<div class="box-body">
               <div class="form-group">
                  <label for="exampleInputEmail1">Policy ID
                  </label>
                  <input type="text" class="form-control" id="edpid" placeholder="Enter Policy Id" disabled>
               </div>
               <div class="form-group">
                  <label for="exampleInputPassword1">Name
                  </label>
                  <input type="text" class="form-control" id="edname" placeholder="Enter Policy Name">
               </div>
               <div class="form-group">
                  <label for="exampleInputPassword1">Description
                  </label>
                  <textarea id="eddescr" class="form-control"></textarea>
               </div>
            </div>

dashboard.blade.php

$(document).on("click", ".editpolicy", function()
    {
        //console.log($(this).data('contact2'));
        $('#edpid').val($(this).data('id'));
        $('#editpolicyid').val($(this).data('id'));
        $('#edname').val($(this).data('name'));
        $('#eddescr').val($(this).data('descr'));

        $('#policyeditmodel').modal('show');

    });

    $("#edpolicyupdate").click(function()
    {
        var data = new FormData();
        var id = $("#editpolicyid").val();
        data.append('old_Id', $("#editpolicyid").val());
        data.append('policy_Id', $("#edpid").val());
        data.append('name', $("#edname").val());
        data.append('description', $("#eddescr").val());
        data.append('_token', $("#tokenedit").val());
        console.log($("#editpolicyid").val());
        console.log($("#edpid").val());
        console.log($("#edname").val());
        console.log($("#eddescr").val());
        console.log($("#tokenedit").val());
        $.ajax(
        {
            type: "post",
            data: data,
            cache: false,
            processData: false,
            contentType: false,
            url: "<?php  echo url('/editpolicydata') ?>",
            success: function(data)
            {
                //console.log(data);
                if (data.error)
                {
                    $('#newerroredit').text(data.error[0]);
                    $("#errmsgedit").show();
                    return false;
                }
                console.log(data);

                $("#errmsgedit").hide();                

                $('#brc' + id).replaceWith("<tr id='brc" + data.id + "'><td>" + data.id + "</td><td>" + data.name + "</td><td><button type='button' class='btn btn-info btn-xs editpolicy' data-descr=" + data.description + " data-name=" + data.name + " data-id=" + id + "><i class='fa fa-edit'></i>Edit</button><button type='button' class='btn btn-info btn-xs viewpolicy' data-descr=" + data.description + " data-name=" + data.name + " data-id=" + id + "><i class='fa fa-eye'></i>View</button><button type='button' class='btn btn-danger btn-xs delpolicy' data-id=" + id + "><i class='fa fa-trash'></i>Delete</button></td></tr>");
            },
            error: function(json)
            {
                console.log(json);                            }
        });

    });



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire