2016-08-20 11 views
0

ANGULARJS を使用してLaravelフレームワークでMySqlクエリを実行しようとしていますが、ボタンを通じてデータベースレコードを削除できません。 このコードは、データをデータベースに挿入し、JSONファイルにデータを取得し、JSONファイルからデータ行を賢明に表示します。今私は対応する削除ボタン広告が更新ボタンを介して既存のデータを更新したいと思うデータベースからデータを削除しようとしています。ここでLaravel 4フレームワークを使用してANGULARJSのすべてのMySqlクエリを実行する方法

は、ここに私のビューページ

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 

</head> 
<body ng-app="myApp" ng-controller="myCtrl"> 

<h2>Registration</h2><br/> 
<table border=1> 
<tr><td>Name:</td><td><input type="text" ng-model="nam" name="nam"><br/></td></tr> 
<tr><td>Email:</td><td><input type="email" ng-model="email" name="email"><br/></td></tr> 
<tr><td>Password:</td><td><input type="password" ng-model="password" name="password"><br/></td></tr> 
<tr><td>City:</td><td><input type="text" ng-model="city" name="city"><br/></td></tr> 
<tr><td><button ng-click="addFunc()" type="submit">Submit</button></td></tr> 
</table> 

</div> 
</form> 
<script> 
var app = angular.module('myApp', []); 
app.controller('myCtrl', function($scope,$http) { 

    $scope.addFunc = function() { 
     $http({ 
      method : 'POST', 
      url  : 'http://localhost/crud/public/registration_data_page', 
      data : {nam:$scope.nam,email:$scope.email,password:$scope.password,city:$scope.city} 
     }).then(function successCallback(response) { 
      console.log('successCallback'); 
      console.log(response); 

    }, function errorCallback(response) { 
      console.log('errorCallback'); 
      console.log(response); 
    }); 

} 
$http({method:'GET', url:'http://localhost/crud/public/registration_json_page'}).success(function(response){ 
$scope.query = response; 
}); 
}); 
</script> 

<table style="width:100%" border=1> 
    <tr> 
    <th>Id</th> 
    <th>Name</th> 
    <th>Email</th> 
    <th>Password</th> 
    <th>City</th> 
    <th>Delete</th> 
    </tr> 

    <tr ng-repeat="x in query"> 
    <td>{{x.id}}</td> 
    <td>{{x.name}}</td> 
    <td>{{x.email}}</td> 
    <td>{{x.password}}</td> 
    <td>{{x.city}}</td><br/> 
    <td><input type="submit" name="delet" value="Delete"></td> 
    </tr> 

</body> 
</html> 

は、私のコントローラです。ここで

<?php 

namespace App\Http\Controllers; 

use App\Http\Requests; 
use Illuminate\Http\Request; 
use DB; 
use Session; 
use Redirect; 

class NewController extends Controller 
{ 
    /** 
    * Create a new controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
    } 
public function registration_data_function(Request $request) 
    { 
     echo $nam_value = $request->nam; 
     echo $email_value = $request->email; 
     echo $password_value = $request->password; 
     echo $city_value = $request->city; 

     $reg = DB::table('angular_registration')->insert(['name' => $nam_value, 'email' => $email_value, 'password'=>$password_value, 'city'=>$city_value]); 
    } 

    public function registration_json_function(Request $request) 
    { 
     $query = DB::select('select * from angular_registration'); 
     return response($query); 
    } 

    public function registration_function(Request $request) 
    { 
     $query = DB::select('select * from angular_registration'); 
     return view('registration'); 
    } 
} 

は私のルートページ

Route::get('/registration', '[email protected]_function'); 

Route::post('/registration_data_page', '[email protected]_data_function'); 

Route::get('/registration_json_page', '[email protected]_json_function'); 

答えて

0

は、角度JSでremoveFunction(ID)関数を作成し、あなたはaddFunctionを持っているだけでも同様である 対応するコントローラ方法とルート

を作成

<?php 
 
//in your route 
 
Route::get('/delete/{id}', '[email protected]') 
 
->where('id'=>'[0-9]+'); 
 

 

 
//in your controller 
 
public function delete($id) 
 
{ 
 
    //please use your models to delete no raw db queries for security 
 
//prevent sql injection 
 
    Model::find($id)->delete() 
 
    return Response::json(['status'=>'deleted']) 
 
}

テンプレートの使用で

角度

$scope.removeFunction=function(id){ 
 
    //call your delete route with the parameters 
 
    ajax.get('/delete/'+id) 
 
}

<td><input type="button" ng-click={{removeFunction(x.id) }} name="delet" value="Delete"></td>

は申し訳ありませんが、PHPでの削除方法は文句を言わないように削除作業はすでにとても重要な仕事でありますdeleteAngularまたは他の名前に変更してください。道に沿って経路

関連する問題