【问题标题】:Updating content Laravel更新内容 Laravel
【发布时间】:2016-07-14 14:13:30
【问题描述】:

我想在 laravel 中为我的网络应用程序创建一个功能(我是新手),每个帖子/评论/主题(在我的情况下)用户都可以进行 upVote 和 downVote。现在我正在使用 upVote,但它不起作用。

在视图中(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>

其中 $Theme->name 是用户想要投票/喜欢的名称(随便)。

路线:

Route::put('/', [
'uses'=>'Vote@VotePlus',
'as' =>'Voteplus' //Name of route
    ]);     

还有控制器:

<?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]);
}
    };

我正在尝试一切,但它不起作用。有人可以帮帮我吗?

【问题讨论】:

  • -&gt;where('title', $title) 应该是-&gt;where('title', $name)
  • 我改了还是不行
  • 可能put路由错了,我用错了?

标签: php mysql database laravel


【解决方案1】:

使用锚标记,您只需发送一个获取请求。如果你想放它,你必须创建一个表单,然后添加:

<input type="hidden" name="_method" value="PUT">

【讨论】:

  • 感谢您的见解,我终于解决了问题:)
【解决方案2】:

解决此问题的另一种方法是使用 Ajax。现在,每当用户想要对主题进行投票时,页面都会刷新,这可能会非常令人沮丧。

我认为您应该使用 Ajax 发布到后端,并在成功回调时使用 javascript 更新视图。我建议在前端使用Angular。它拥有你所需要的一切,而且制作 Ajax 请求非常简单。

所以,这里有一个简单的例子,你可以如何使用 Angular + Laravel 让你的 Web 应用程序正常工作。

前端

<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>

后端

你的路线

Route::get('api/themes', 'ThemeController@getFive');
Route::post('api/themes/voteUp, 'ThemeController@voteUp');

你的主题控制器

function getFive() {
    return Theme::paginate(5);
}

function voteUp(Request $request) {
    $theme = Theme::whereId($request->id);
    $theme->votes += 1;
    $theme->save();

    return $theme;
}

此代码未经测试。但我想你明白了!

【讨论】:

  • 我稍微改了一下,就可以了!非常感谢!
  • 太棒了!很高兴我能帮忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 2019-04-25
  • 2019-01-24
  • 1970-01-01
  • 2023-03-15
  • 2013-03-29
相关资源
最近更新 更多