说实话,我是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>
制作本教程的人也给出了布局代码,但它是角度和ajax,我不知道所以我复制了它。这是我打开链接时布局代码https://gist.github.com/mydnic/278e485b9e636c491ab1的链接(即http://localhost:8000/comments/create)我收到错误,说ee0676ab34142915e07250d6b64599d707c58afd.php第43行中的错误异常: 未定义的变量:post。在此先感谢: - )
答案 0 :(得分:1)
错误表示您未将$post
方法中的create()
变量传递给视图。您应该在create()
方法中执行以下操作:
$post = Post::find($id);
return view('comments.create', ['post' => '$post']);