lundi 2 mai 2016

Updating data from controller to database in laravel

I want to update data from laravel controller to database, I am able tto update data in model but I am not able to update data into database

here is my View or HTML coding

function editdata(data)
{
    //alert(data);
    $(function(){
      $.ajax({
        method : "GET",
        url: "welcome",        
        data: {id: data},        
        success : function(response)
        {
          //debugger;
          /*if(response==true)
            alert("success");
          else
            alert("Failure");*/
          var a=response;
          //$('#fname').value(a.fname);
          $('#firstName').val(a.fname);
          $('#lastName').val(a.lname);
          $('#qualification').val(a.qualification);
          $('#emailAddress').val(a.email);
          $('#contactmessage').val(a.desc);
          var elem = document.getElementById("add");
          elem.value="Update";

        }

      });


    });
}
function disableData(data)
{
    //alert(data);
    //if (confirm('Are You Sure You Want To Delete Data ?')) {


    $(function(){
      $.ajax({
        method : "GET",
        url: "welcome/disable",        
        data: {id: data},        
        success : function(response)
        {

          //alert(response);
          window.location.reload();
          //debugger;
          /*if(response==true)
            alert("success");
          else
            alert("Failure");*/
          //alert('Data is deleted Successfully!!!');

        }

      });


    });

    //} else {
    //alert('Data is not deleted');
    //}
}
function addUpdateData(data)
{
   var btnvalue = document.getElementById("add").value;
   //alert($('#firstName').val)
      //alert(btnvalue);
   if(btnvalue=="Add")
   {
      $(function(){
          $.ajax({
            method : "GET",
            url: "welcome",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: dataToSend,

            success : function(response)
            {
              alert("data sent successfully");
          //debugger;
          /*if(response==true)
            alert("success");
          else
            alert("Failure");*/

           }

          });
      });
   }
   else
   {
      $(function(){
          $.ajax({
            method : "GET",
            url: "welcome",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: dataToSend ,

            success : function(response)
            {
              alert("data sent successfully");
          //debugger;
          /*if(response==true)
            alert("success");
          else
            alert("Failure");*/

           }

          });
      });
   }
}

/*function addData(data)
{
    $function(){

      $.ajax({

        method : "post",
        url: "welcome",
        data: {id: data,}
      })
    }
}*/

</script>

</head>


<body>

<div class="container-fluid" style="background-color: #CCCCCC">

  <h1>Temp Form</h1>
            <form method="post" action="" role="form">


           <input type="hidden" name="_token" value="">

          <div class="panel panel-default ">

            <div class="container">

            <div class="panel-body">
              <div class="form-group">
                <label for="firstName">First Name *</label>
                <input name="fname" type="text" class="form-control" id="firstName" placeholder="Enter First Name" required>
              </div>

              <div class="form-group">
                <label for="lastName">Last Name *</label>
                <input name="lname" type="text" class="form-control" id="lastName" placeholder="Enter Last Name" required>
              </div>

              <div class="form-group">
                <label for="qualification">Qualification *</label>
                <input name="qualification" type="text" class="form-control" id="qualification" placeholder="BE, MCA, MBA Etc." required>
              </div>

              <div class="form-group">
                <label for="emailAddress">Email address *</label>
                <input name="email" type="email" class="form-control" id="emailAddress" placeholder="Enter Email" required>
              </div>

              <div class="form-group">
                <label for="contactmessage">Message</label>            
                <textarea name="desc" type="text" class="form-control" id="contactmessage" placeholder="Message" rows="2"></textarea>
              </div>              





                <input type="submit" id="add" class="btn btn-primary" onClick="addUpdateData(id)" value="Add"></button>
              </div>
          </div>
        </div></form>


        </div>
        <div class="container">
        <div class="container-fluid">
           <h1>All Data</h1>

           <div class="row">
            <table class="table table-hover">
              <thead>
                <tr>
                 <th> First Name </th> <th> Last Name </th>
                  <th> Qualification </th> <th> E-mail </th> <th> Description </th>
                </tr>
              </thead>
              <tbody>
                @foreach ($forms as $form)
                  <tr>

                    <td>  </td>
                    <td>  </td>
                    <td>  </td>
                    <td>  </td>
                    <td>  </td><br>
                    <td> <a id="" onClick="editdata(id)" class="btn btn-info">
                         <span class="glyphicon glyphicon-edit"></span></a></td>
                    <td><a id="" onClick="disableData(id)" class="btn btn-danger">
                          <span class="glyphicon glyphicon-trash"></span>
                         </a></td>
                    </td>
                  </tr>
                @endforeach
             </tbody>
           </div>
        </div>

        </table>
          </body>


  </html>

Here is code for controller

<?php

namespace App\Http\Controllers;

use DB;

use App\Basicusers;
use Request;

use App\Http\Requests;

class FormController extends Controller
{
    //$flag = "Add";
    public function show()
    {
        //return 'FormController';
        $forms = Basicusers::all()->where('flag',1);
        //$name = ['Nix','Shiv'];
        //return view('welcome',compact('name'));
        return view('welcome',compact('forms'));
    }
    /*public function addNew(Request $req)
    {
        $bs = new Basicusers;


        $bs->fname = $req->fname;
        $bs->lname = $req->lname;
        $bs->qualification = $req->qualification;
        $bs->email = $req->email;
        $bs->desc = $req->desc;

        $bs->save();

        return back();
        //return "Success";
    }*/
    public function editdata()
    {

        //return "Editdata called";
        //$data = Basicusers::find($id);
        //$flag = "Update";
        //return view('edit',compact('data')));
        $id = $_GET['id'];
        //$id = Request::get();
        $data = Basicusers::find($id);
        //return view('welcome',compact('id'));

        return $data;

        //return $id;
       // $category = Input::get('productCategory');
        //$id = Request::get('id');
        //$data = Basicusers::find($id);
        //return $data;
        // 1exit;
        //return $data;
        /*htmlForm::open();
        Form::textarea('fname','Nirav');
        Form::close();
        return back()*/;
        //$form = Basicusers::find($id);
        //return view('welcome',compact('form'));
    }
    public function disableData()
    {

        $id = $_GET['id'];

        //$update = DB::table('basicusers')->where('id',$id)->update('flag','0' );
        $alldata = Basicusers::find($id);

        //$original = $alldata->flag;

        $alldata->flag = 0;

        $alldata->save();
        //$alldata.update('flag',0);
        //$alldata.save();
        //$update->save();
        return $alldata->flag;
        //Basicusers::find($id)->delete();

        //Basicusers::where('id',$id)->update('flag',0);

        //return "success";

    }
    /*public function addUpdateData(Request $request)
    {
        /*$formupdate = Request::all();
        $form = Basicusers::find($id);
        $form->update($formupdate);

        return redirect('/');*/
        //if($flag=="Add")
            //print_r("Data Will be Added");
       // if($flag=="Update")
           // print_r("Data Will be Updated");
        //else
            //print_r("Error");
        //print_r($formupdate);
        //$input = Input::all();
        //return response()->json($input); //- See more at: http://ift.tt/1rbHuGH
    }*/
}

I am able to get data using following commands

    $id = $_GET['id'];//id of data to be disabled
    $alldata = Basicusers::find($id);//row for which data will be disabled
    $alldata->flag = 0;//setting values
    $alldata->save();//saving data
    return $alldata->flag;//returning new value

However from the above code Data is not updated in database How to update data in database please guide

after clicking on delete button, I just want to disable data(don't want to fetch that row) I have column called flag where binary values 0 or 1 stored.

I am fetching only those records where the value of flag is 1

so on delete I want to make flag value for that record to 0



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire