Home > OS >  Method Illuminate\Database\Eloquent\Collection::paginate does not exist using relations tales
Method Illuminate\Database\Eloquent\Collection::paginate does not exist using relations tales

Time:08-24

I created a page using the pagination feature, when I opened the blog page everything went well, but when I opened the blog page based on the username from the user table there was an error Method Illuminate\Database\Eloquent\Collection::paginate does not exist. BTW it uses the same view.

blog.blade.php

@extends('layouts.main')

@section('container')
    <main >
        @foreach ($blogs as $blog)
            <article >
                {{-- judul --}}            
                <h2 ><a href="/blog/{{ $blog->slug }}">{{ $blog->title }}</a></h2>

                {{-- penulis category --}}
                <p>Author : <a href="/author/{{ $blog->user->username }}" >{{ $blog->user->name }}</a> | Category : <a href="/category/{{ $blog->category->slug }}" >{{ $blog->category->name }}</a></p>

                <p >{{ Str::limit($blog->body, 100) }}</p>
            </article>            
        @endforeach

        {{ $blogs->links() }}
    </main>
@endsection

BlogController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Blog;

class BlogController extends Controller
{
    public function index() {
        return view('blog', [
            'title' => 'All Blog',
            'blogs' => Blog::latest()->paginate(5)
        ]);
    }

    public function show(Blog $blog) {
        return view('show_blog.index', [
            'title' => $blog->title,
            'blog' => $blog
        ]);
    }
}

UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
    public function blogFromUser(User $user) {
        return view('blog', [
            'title' => $user->name,
            'blogs' => $user->blog->paginate(5)
        ]);
    }
}

Blog.php *relate to user (belongsTo)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
    use HasFactory;

    public function category() {
        return $this->belongsTo(Category::class);
    }

    public function user() {
        return $this->belongsTo(User::class);
    }
}

)*

User.php *related to Blog (hasMany)

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;


    public function Blog() {
        return $this->hasMany(Blog::class);
    }
}

)*

CodePudding user response:

When you do :

$user->blog

it will execute the query and retrieve the results, so $user->blog will be a collection.

If you want to paginate it, you need to call paginate on the query/relation, not the result, so you need to do something like :

$user->blog()->paginate(5)

then to access other pages, you will have to append ?page=xy to the page url

be aware that if you have multiple paginations within the same page you will need to change the name of the parameter to avoid conflict like so :

$user->blog()->paginate(5, '*', 'blog')
  • Related