Laravel: 类似系统实现错误
Laravel: Error with like system implementation
我是 laravel 的新手,所以这可能是个愚蠢的错误,但我就是不明白为什么会出现这个错误。是的,所以在我的网站上,用户可以创建 posts,其他用户可以喜欢那些 posts。然而,我的类似系统的实现抛出以下错误:
ErrorException (E_ERROR)
Method Illuminate\Database\Eloquent\Collection::likes does not exist. (View: C:\xampp\htdocs\eventcw\resources\views\eventspage.blade.php)
这是我的post控制点赞的控制器方法:
public function postLikePost($post_id){
$loggedin_user = Auth::user()->id;
$like_user = Like::where(['user_id' => $loggedin_user, 'post_id' => $post_id])->first();
if(empty($like_user->user_id)){
$user_id = Auth::user()->id;
$post_id = $post_id;
$like = new Like;
$like->user_id = $user_id;
$like->post_id = $post_id;
$like->save();
return redirect()->route('events');
}else{
return redirect()->route('events');
}
}
我的数据库关系似乎很好,
这是我的赞模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function post(){
return $this->belongsTo('App\Post');
}
}
这是我喜欢的table迁移:
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->integer('user_id');
$table->timestamps();
});
这是我的 post 观点:
<section class="row posts">
@foreach($posts as $post)
<div class="col-md-2 col-md-offset-3">
<article class="post">
<p>{{ $post->body }}</p>
<div class="info">Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</div>
<p>This post has {{ $posts->likes()->count() }} likes </p>
<a href="{{ route('like') }}" class="post-item">Like</a>|
</article>
</div>
@endforeach
</section>
该错误表明您正在直接在 Collection 上调用 likes()
。
$posts
是您在 blade 模板中迭代的 collection。
将{{ $posts->likes()->count() }}
更改为{{ $post->likes()->count() }}
我是 laravel 的新手,所以这可能是个愚蠢的错误,但我就是不明白为什么会出现这个错误。是的,所以在我的网站上,用户可以创建 posts,其他用户可以喜欢那些 posts。然而,我的类似系统的实现抛出以下错误:
ErrorException (E_ERROR)
Method Illuminate\Database\Eloquent\Collection::likes does not exist. (View: C:\xampp\htdocs\eventcw\resources\views\eventspage.blade.php)
这是我的post控制点赞的控制器方法:
public function postLikePost($post_id){
$loggedin_user = Auth::user()->id;
$like_user = Like::where(['user_id' => $loggedin_user, 'post_id' => $post_id])->first();
if(empty($like_user->user_id)){
$user_id = Auth::user()->id;
$post_id = $post_id;
$like = new Like;
$like->user_id = $user_id;
$like->post_id = $post_id;
$like->save();
return redirect()->route('events');
}else{
return redirect()->route('events');
}
}
我的数据库关系似乎很好,
这是我的赞模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
public function post(){
return $this->belongsTo('App\Post');
}
}
这是我喜欢的table迁移:
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->integer('user_id');
$table->timestamps();
});
这是我的 post 观点:
<section class="row posts">
@foreach($posts as $post)
<div class="col-md-2 col-md-offset-3">
<article class="post">
<p>{{ $post->body }}</p>
<div class="info">Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</div>
<p>This post has {{ $posts->likes()->count() }} likes </p>
<a href="{{ route('like') }}" class="post-item">Like</a>|
</article>
</div>
@endforeach
</section>
该错误表明您正在直接在 Collection 上调用 likes()
。
$posts
是您在 blade 模板中迭代的 collection。
将{{ $posts->likes()->count() }}
更改为{{ $post->likes()->count() }}