I
I
Ivan Ivanov2018-07-09 13:18:23
Laravel
Ivan Ivanov, 2018-07-09 13:18:23

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);
    }

Here is the show.blade.php code (next/previous post)
<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-->

I will be very grateful for your help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2018-07-09
@maksim_fix

Meditate on a string

<img src="{{$post->getPrevious->getImage()}}" alt="">

Then read to dock about communications. And then either use connections as you need, or don't use them at all.
At the same time, read about pagination so as not to reinvent the wheel with square wheels.

A
Arthur, 2018-07-09
@ART_CORP

Maxim Fix
1) laravel has pagination https://laravel.com/docs/5.6/pagination
2) for non-static methods, use $this instead of self
3) and where is the getImage method? I don't see it in the model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question