删除Laravel上的对象 [英] Delete object on laravel
问题描述
我正在学习laravel,现在我试图删除没有形式的对象.我想使用js来检测用户何时单击删除"按钮并将通知返回给控制器.然后,具有ID为ID的删除对象的控制器已从JS文件返回.
I'm learning laravel, and now i trying to delete object without form. i want to use js to detect when user click delete's button and return notify to controller. Then controller with delete object with id has returned from JS file.
这是刀片文件
@extends ('layouts.master')
@section ('head.title')
Blog
@stop
@section ('body.content')
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
</div>
</div>
<form class="form-show">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<h2> {{ $article->title}} </h2>
<p> {{ $article->content}} </p>
</div>
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<a href=" {{ route('article.edit', $article->id) }}" class="btn btn-info">Update</a>
<button id="delete-button" type="submit" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
</form>
</div>
<script src="{{ asset('/js/jshow.js') }}"></script>
@stop
这是控制器文件
类ArticlesController扩展了Controller{受保护的$ articleModel;
class ArticlesController extends Controller { protected $articleModel;
public function __construct(Article $article){
$this->articleModel = $article;
}
public function index(){
$articles = $this->articleModel->getListArticles();
// $articles = Article::paginate(10);
return view('articles.index',compact('articles'));
}
public function show($id){
// $article = Article::find($id);
$article = $this->articleModel->getArticleWithID($id);
return view('articles.show',compact('article'));
}
public function delete($id){
$this->articleModel->deleteArticle($id);
return redirect()->route('articles.index');
}
}
这是JS文件
var deleteButton = document.getElementById("delete-button");
var idPost = document.getElementById("")
deleteButton.onclick = function() {
alert('Click to delete');
return false;
}
推荐答案
您需要使用 jQuery
JS库中的> $.ajax .
You need to use $.ajax
from the jQuery JS library.
尝试理解并做点事情,如果遇到困难,请回来.
Try to understand and to do something, then come back if you any have difficulties.
像 Jerodev 所说,AJAX调用可以在没有jQuery的情况下完成,但是我发现jQuery方法更容易理解.在此处上阅读有关使用纯JavaScript的AJAX的更多信息.
Like Jerodev said, AJAX calls can be done without jQuery, but I find the jQuery method more understandable. Check here to read more about AJAX using plain JavaScript.
这篇关于删除Laravel上的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!