在laravel中将数据从控制器更新到数据库

时间:2016-05-02 06:17:00

标签: database laravel-5

我想将laravel控制器中的数据更新到数据库, 我能够更新模型中的数据,但我无法将数据更新到数据库

这是我的视图或HTML编码

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="{{ csrf_token() }}">

          <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> {{  $form->fname }} </td>
                    <td> {{  $form->lname }} </td>
                    <td> {{  $form->qualification }} </td>
                    <td> {{  $form->email }} </td>
                    <td> {{  $form->desc }} </td><br>
                    <td> <a id="{{ $form->id }}" onClick="editdata(id)" class="btn btn-info">
                         <span class="glyphicon glyphicon-edit"></span></a></td>
                    <td><a id="{{ $form->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://yuluer.com/page/dehbedif-laravel-5-controller-wont-receive-data-from-ajax-form.shtml#sthash.gj2cIar7.dpuf
    }*/
}

我可以使用以下命令获取数据

    $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

但是从上面的代码中,数据不会在数据库中更新 如何更新数据库中的数据请指导

点击删除按钮后,我只想禁用数据(不想获取该行) 我有一个名为flag的列,其中存储了二进制值0或1。

我只获取flag的值为1的记录

所以在删除时我想将该记录的标志值设为0

1 个答案:

答案 0 :(得分:0)

也许您可以尝试替代laravel更新方法$alldata->update(['flag' => 0]);,看看你是否有更好的运气......