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



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

<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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 IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser 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('AppPost', 'likes', 'user_id', 'post_id');
}
}

这是我的post.php代码

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
//
public function likes()
{
return $this->belongsToMany('AppUser', 'likes');
}
}

这是我的web.php代码:-

<?php

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



Route::get('post/{id}/islikedbyme', 'APIPostController@isLikedByMe');
Route::post('post/like', 'APIPostController@like');
?>

这是我的PostController.php代码:-

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
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。实际上喜欢和不喜欢的按钮都会在我的评论附近(顺便说一句,我的评论系统运行得很好)

<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)我在ee0676ab34142915e07250d6b64599d707c58afd.php第43行中遇到错误,表示此ErrorException:未定义的变量:post。提前感谢:-)

该错误意味着您没有将$post变量从create()方法传递到视图。您应该在create()方法中执行以下操作:

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

最新更新