[英]Updating content Laravel
I want to make a feature for my web app in laravel(i'm new at it) that every Post/Comment/Theme(in my case) the user has the ability to upVote and downVote. 我想在laravel(我的新功能)中为我的Web应用程序创建一个功能,使用户的每个帖子/评论/主题(在我的情况下)都具有upVote和downVote的功能。 Now i am woking with upVote, but it is not working whatsoever.
现在,我开始使用upVote,但是无论如何它都无法正常工作。
In the view (welcome.blade.php) I have: 在视图(welcome.blade.php)中,我有:
<a href="{{ route('Voteplus', ['name' => $Theme->name]) }}"> <img class="media-object" style="height:40px; width:40px;" src="images/upVote.svg" alt="..."></a>
Where $Theme->name is the one that the user wants to upVote/like(whatsoever). 其中$ Theme-> name是用户想要进行投票/点赞(无论如何)的那个。
The route: 路线:
Route::put('/', [
'uses'=>'Vote@VotePlus',
'as' =>'Voteplus' //Name of route
]);
And the controller: 和控制器:
<?php
namespace App\Http\Controllers;
use App\NewTheme;
use DB;
class Vote extends Controller {
public function VotePlus($name){
DB::table('New_Themes')
->where('name', $name)
->increment('upVotes', 1);
$Themes = NewTheme::paginate(5);
return redirect()->route('welcome', ['Themes'=>$Themes]);
}
};
I am trying everything, but it isn't working. 我正在尝试一切,但没有用。 Can someone help me please?
有人能帮助我吗?
With an anchor tag, you only send a get request. 使用锚标记,您仅发送获取请求。 If you want it to be put, you must create a form, and then add :
如果要放置它,则必须创建一个表单,然后添加:
<input type="hidden" name="_method" value="PUT">
Another way to solve this issue is to use Ajax. 解决此问题的另一种方法是使用Ajax。 Right now the page will get refreshed each time a user wants to up-vote a theme and that can be quite frustrating.
现在,每次用户想要投票主题时页面都会刷新,这可能会令人沮丧。
I think you should post to the back end using Ajax and on the success callback update the view with javascript. 我认为您应该使用Ajax发布到后端,并在成功回调中使用javascript更新视图。 I recommend using Angular for the front end.
我建议在前端使用Angular 。 It has all what you need and it is super simple to make an Ajax-request.
它具有您所需要的一切,并且进行Ajax请求非常简单。
So, here's a quick example how you could use Angular + Laravel to make your web application work. 因此,这是一个简单的示例,说明如何使用Angular + Laravel使您的Web应用程序正常工作。
Front End 前端
<html ng-app="exampleApp">
<head>
<title>MyTitle</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
<div ng-controller="ThemeController">
<ul>
<li ng-repeat="theme in themes">
<span><% theme.name %></span>
<p><% theme.description %></p>
<p><% theme.votes %></p>
<a href="#" ng-click="voteUp(theme)">Vote Up</a>
</li>
</ul>
</div>
<script type="text/javascript">
var app = angular.module("exampleApp", [], function($interpolateProvider) {
// This we need to not collide with Laravel's {{ }}
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
app.controller('ThemeController', function($scope, $http) {
$scope.themes = [];
$scope.voteUp = function(theme) {
$http({
url: '/api/themes/voteUp',
method: 'POST',
data: {
id: theme.id
}
}).success(function(response) {
theme.votes += 1;
});
}
// On init we need to get the themes
$http({
url: '/api/themes',
method: 'GET'
}).success(function(themes) {
$scope.themes = themes;
});
});
</script>
</body>
</html>
Back End 后端
Your routes 您的路线
Route::get('api/themes', 'ThemeController@getFive');
Route::post('api/themes/voteUp, 'ThemeController@voteUp');
Your ThemeController 您的ThemeController
function getFive() {
return Theme::paginate(5);
}
function voteUp(Request $request) {
$theme = Theme::whereId($request->id);
$theme->votes += 1;
$theme->save();
return $theme;
}
This code is not tested. 此代码未经测试。 But I'll think you get the point!
但我认为您明白了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.