Я новичок в Laravel и пытаюсь создать базовый блог, в котором пользователям может понравиться публикация.Однако я получаю следующую ошибку:
BadMethodCallException в строке Builder.php 2405: вызов неопределенного метода Illuminate \ Database \ Query \ Builder :: likes ()
Ниже приведен код, который используется:
PostController.php
class PostController extends Controller
{
public function getIndex()
{
$posts = Post::orderBy('created_at', 'desc')->paginate(2);
return view('blog.index', ['posts' => $posts]);
}
public function getAdminIndex()
{
$posts = Post::all();
return view('admin.index', ['posts' => $posts]);
}
public function getPost($id)
{
$post = Post::find($id);
return view('blog.post', ['post' => $post]);
}
public function getLikePost($id)
{
$post = Post::find($id);
$like = new Like();
$post->likes()->save($like);
return redirect()->back();
}
}
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = ['title', 'content'];
public function likes()
{
return $this->hasMany('App\Like');
}
}
Like.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
public function post()
{
return $this->belongsTo('App\Post');
}
}
post.blade.php
@extends('layouts.master')
@section('content')
<div class="row">
<div class="col-md-12">
<p class="quote">{{ $post-> title}}</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{{ count($post->likes) }} Likes | <a href="{{ route('blog.post.like', [ 'id' => $post->id ]) }}">Like</a></p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{{ $post->content }}</p>
</div>
</div>
@endsection
rout.php
Route::get('/', [
'uses' => 'PostController@getIndex',
'as' => 'blog.index'
]);
Route::get('post/{id}', [
'uses' => 'PostController@getPost',
'as' => 'blog.post'
]);
Route::get('post/{id}/like', [
'uses' => 'PostController@getLikePost',
'as' => 'blog.post.like'
]);
Route::get('about', function () {
return view('other.about');
})->name('other.about');
Route::get('Register', function () {
return view('auth.register');
})->name('auth.register');
Route::get('Login', function () {
return view('auth.login');
})->name('auth.login');
Я не могу понять, где проблема заключается в методе лайков или в контроллере.Я ограничен Laravel 5.2 из-за ограничений IDE Cloud 9.
Вывод dd ($ post):
Post {#182 ▼
#fillable: array:2 [▼
0 => "title"
1 => "content"
]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:5 [▼
"id" => 1
"created_at" => "2018-05-31 20:19:50"
"updated_at" => "2018-05-31 20:19:50"
"title" => "Another test"
"content" => "adsasjioh919789172y"
]
#original: array:5 [▼
"id" => 1
"created_at" => "2018-05-31 20:19:50"
"updated_at" => "2018-05-31 20:19:50"
"title" => "Another test"
"content" => "adsasjioh919789172y"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▼
0 => "*"
]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}