繁体   English   中英

如何在我的 Laravel 网站中添加喜欢和不喜欢的功能?

[英]How to add like and dislike functionality in my laravel website?

确实,我是 laravel 的新手,我正在按照本教程在我的网站https://mydnic.be/post/simple-like-system-with-laravel-5 中添加喜欢和不喜欢的功能。 这是我的迁移代码:-

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLikesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id');
        $table->integer('user_id');
        $table->softDeletes();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('likes');
}
}

这是我的 user.php 代码:-

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];


public function likes()
{
return $this->belongsToMany('App\Post', 'likes', 'user_id', 'post_id');
}
}

这是我的 post.php 代码

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
//
public function likes()
{
return $this->belongsToMany('App\User', 'likes');
}
}

这是我的 web.php 代码:-

<?php


Route::get('/', function () {
return view('welcome');
});

Auth::routes();






Route::get('post/{id}/islikedbyme', 'API\PostController@isLikedByMe');
Route::post('post/like', 'API\PostController@like');

?>

这是我的 PostController.php 代码:-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
//

public function isLikedByMe($id)
{
$post = Post::findOrFail($id)->first();
if (Like::whereUserId(Auth::id())->wherePostId($post->id)->exists()){
    return 'true';
}
return 'false';
}

public function like(Post $post)
{
$existing_like = Like::withTrashed()->wherePostId($post->id)-  >whereUserId(Auth::id())->first();

if (is_null($existing_like)) {
    Like::create([
        'post_id' => $post->id,
        'user_id' => Auth::id()
    ]);
} else {
    if (is_null($existing_like->deleted_at)) {
        $existing_like->delete();
    } else {
        $existing_like->restore();
    }
}
}
}

这是我的布局文件代码,名为 create.blade.php.Actully 喜欢和不喜欢按钮将在我的评论附近(顺便说一句,我的评论系统运行良好)

<html>
<head>

</head>
<body>
 <div class="row new-post">
    <div class="col-md-6 col-md-offset-3">

        <header><h3>Comments</h3></header>
        <form action="/comments" method="post">
        {{csrf_field()}}
            <div class="form-group">
                <textarea class="form-control" name="body" id="new-post" r  rows="5" placeholder="Your review on above game"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Post  Comment</button>

        </form>
    </div>
 </div>

 @foreach($comments as $comment) 
 <h1>{{$comment->body }}</h1>
 @endforeach


<div ng-app="Actions">
<span ng-controller="LikeController">
    @if ($post->user->id != Auth::id())
        <button class="btn btn-default like btn-login" ng-click="like()">
            <i class="fa fa-heart"></i>
            <span>@{{ like_btn_text }}</span>
        </button>
    @endif
</span>
</div>
<script>
var app = angular.module("Actions", []);
app.controller("LikeController", function($scope, $http) {

    checkLike();
    $scope.like = function() {
        var post = {
            id: "{{ $post->id }}",
        };
        $http.post('/api/v1/post/like', post).success(function(result) {
            checkLike();
        });
    };
    function checkLike(){
        $http.get('/api/v1/post/{{ $post->id  }}/islikedbyme').success(function(result) {
            if (result == 'true') {
                $scope.like_btn_text = "Delete Like";
            } else {
                $scope.like_btn_text = "Like";
            }
        });
    };
});
</script>
</body>

</html>

制作本教程的人也给出了布局代码,但它是我不知道的 angular 和 ajax,所以我复制了它。 这是布局代码的链接https://gist.github.com/mydnic/278e485b9e636c491ab1当我打开链接时(即http://localhost:8000/comments/create )我在 ee0676ab34142915e07250d6b7045c 中收到错误,说这个 ErrorException第 43 行:未定义变量:post。 提前致谢:-)

该错误意味着您没有将$post变量从create()方法传递给视图。 你应该在create()方法中做这样的事情:

$post = Post::find($id);
return view('comments.create', ['post' => '$post']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM