[英]Delete a row from Angular and Laravel
I need to delete a course from my course table. 我需要从课程表中删除课程。 Front end is angular, back end is laravel. 前端是有角的,后端是laravel。 What am I doing wrong? 我究竟做错了什么?
This is my delete course route: 这是我的删除课程路线:
Route::post('/course/delete/', 'AdminController@courseDelete'); 路线:: post('/ course / delete /','AdminController @ courseDelete');
This is my controller: 这是我的控制器:
public function courseDelete(){
$data = Input::all();
$course =Course::where('name', '=', $data["name"]);
if($course->delete()) {
Course::find($data["name"])->delete();
return json_encode(array('success' => true));
} else {
return json_encode(array('success' => false, 'errors' => "Unable to remove course."));
}
}
This is my angular: 这是我的角度:
app.register.controller('adminDeleteCourse', ['$scope', '$http', '$modal', '$location', 'pinesNotifications', '$route', 'SessionService', 'WebService', function($scope, $http, $modal, $location, pinesNotifications, $route, SessionService, WebService){
var base_url = $("meta[name='base_url']").attr('content');
$scope.courses = {};
$scope.open = function(name, size) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: function($scope, $modalInstance, items) {
$scope.items = items;
$scope.ok = function() {
$data = {
user_id: name
};
$http({
method: 'post',
url: base_url + '/course/getcourses',
data : $data
}).
success(function(data, status, headers, config) {
if(data['success']) {
$route.reload();
pinesNotifications.notify({
title: 'Success',
text: 'Course successfully deleted.',
type: 'success',
hide: true
});
} else {
pinesNotifications.notify({
title: 'Error',
text: data['errors'],
type: 'error',
hide: true
});
}
}).
error(function(data, status, headers, config) {
$('#error').html('code: ' + status);
$('#alert').fadeIn(1000).removeClass('hide');
});
$modalInstance.close();
};
}]);
This is my html 这是我的HTML
<a ng-click="open(course.name)" class="btn btn-brown btn-sm">Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</panel>
</div>
</div>
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">Confirmation</h3>
</div>
<div class="modal-body">
Are you sure you want to delete course?
</div>
<div class="modal-footer">
<button class="btn btn-brown" ng-click="ok()">Delete</button>
<!--<button class="btn btn-primary" ng-click="cancel()">Cancel</button>-->
</div>
</script>
I cannot delete the course from DB. 我无法从数据库删除课程。 What am I doing wrong? 我究竟做错了什么? I am pretty new to angular and laravel 我对angular和laravel很陌生
In your courseDelete function, You have done a little mistake. 在courseDelete函数中,您犯了一些错误。
Add This line 添加此行
$course =Course::where('name', '=', $data["name"])->first();
Instead of 代替
$course =Course::where('name', '=', $data["name"]);
And You function look like: 您的功能如下所示:
public function courseDelete(){
$data = Input::all();
$course =Course::where('name', '=', $data["name"])->first();
if($course->delete()) {
return json_encode(array('success' => true));
} else {
return json_encode(array('success' => false, 'errors' => "Unable to remove course."));
}
}
Model::find();
works with primary key. 使用主键。
so you need to pass id to fetch the row or you can do. 因此,您需要传递ID来获取该行,或者可以这样做。
Model::where('column_name',$variable)->first();
For further details view : https://laravel.com/docs/5.5/eloquent 有关更多详细信息,请参见: https : //laravel.com/docs/5.5/eloquent
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.