Answer the question
In order to leave comments, you need to log in
Relationship method must return an object of type?
Hello, I am writing a blog on Laravel, but I got an error:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation (View: /home/ssciiike/blog.glezer.ru/resources/views/pages/show .blade.php)
I wanted to make pagination (previous and next post)
Here is the Post.php (model) code that I wrote after I got an error:
public function hasPrevious()
{
return self::where('id', '<', $this->id)->max('id');
}
public function getPrevious()
{
$postID = $this->hasPrevious(); //ID
return self::find($postID);
}
public function hasNext()
{
return self::where('id', '>', $this->id)->min('id');
}
public function getNext()
{
$postID = $this->hasNext();
return self::find($postID);
}
<div class="row"><!--blog next previous-->
<div class="col-md-6">
@if($post->hasPrevious())
<div class="single-blog-box">
<a href="{{route('post.show', $post->getPrevious()->slug)}}">
<img src="{{$post->getPrevious->getImage()}}" alt="">
<div class="overlay">
<div class="promo-text">
<p><i class=" pull-left fa fa-angle-left"></i></p>
<h5>{{$post->getPrevious()->title}}</h5>
</div>
</div>
</a>
</div>
@endif
</div>
<div class="col-md-6">
@if($post->hasNext())
<div class="single-blog-box">
<a href="{{route('post.show', $post->getNext()->slug)}}">
<img src="{{$post->getNext()->getImage()}}" alt="">
<div class="overlay">
<div class="promo-text">
<p><i class=" pull-right fa fa-angle-right"></i></p>
<h5>{{$post->getNext()->title}}</h5>
</div>
</div>
</a>
</div>
@endif
</div>
</div><!--blog next previous end-->
Answer the question
In order to leave comments, you need to log in
Meditate on a string
<img src="{{$post->getPrevious->getImage()}}" alt="">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question