本当に私はlaravelの新人です。私はこのチュートリアルに続き、私のウェブサイトのような機能と嫌いな機能を追加していますhttps://mydnic.be/post/simple-like-system-with-laravel-5。 は、ここに私の移行コードである: -私のlaravelウェブサイトに好き嫌いの機能を追加するには?
<?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');
}
}
ここに私のウェブです。 PHPコード: -
<?php
Route::get('/', function() {
return view('welcome');
});
Auth::routes();
Route::get('post/{id}/islikedbyme', 'API\[email protected]');
Route::post('post/like', 'API\[email protected]');
?>
ここに私の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>
このチュートリアルを作った人にもレイアウトコードが与えられていますが、それは私が知っていない角度とアヤックスであり、コピーしました。リンクを開くときに(http://localhost:8000/comments/create)、レイアウトコードhttps://gist.github.com/mydnic/278e485b9e636c491ab1へのリンクがあります。このErrorExceptionをee0676ab34142915e07250d6b64599d707c58afd.php 43行目に表示しています。未定義変数:post。ありがとうございます:-)
卿私はPostController.phpで作成する機能を持っていないが、私はこのパブリック関数は($ ID)を作成するように関数を作成することにより、isLikedByMe機能を交換しようとした { を:あなたは
create()
方法でこのような何かを行う必要があります$ post = Post :: find($ id);しかし、私は同じエラーを受けていました。 リターンビュー( 'comments.create'、['post' => '$ post']); } – Pawanどこでも慣習を使用していないので、別の方法で呼び出すことができます。私は '/ comments/create'ページに行くときに実行されるメソッドについて話しています。 –
申し訳ありません私はあなたのために問題を作成していますが、私に固執してください。あなたが何を言っているか把握することはできませんよりpresizeにすることができます。 – Pawan