mlazuardy's avatar

Pagination In Category Page Error Call to undefined method

I want to make the pagination inside the sorting list of my category page in localhost:8000/category/slug-name of my category slug name

i use method

public function show($slug)
    {
        //
      
        $categories = Category::where('slug',$slug)->first();
        if(!$categories){
          abort(404);}
        return view('categories.show')->with('categories',$categories);
        }

i input the page view

  @foreach ($categories->posts as $post)
        <div class="col-md-4">

              <a href="{{url('blog',$post->slug)}}"><div class="card">
                  <img class="card-image-top" src="/storage/{{  $post->image}}" alt"card image">

                    <span class="category">{{$categories->name}}</span>

                  </br>
                    <h2 class="card-title">{{ $post->title}}</h2>
                    <div class="commit-button-default">
                            <a href="{{url('blog',$post->slug)}}" id="commit-button">Read More</a>
                    </div>

                  </div></a>

        </div>
      @endforeach 

everything still okay here, but when i use the paginator

  <nav class="page-navigation">
  {{$categories->links()}}
  </nav>

and it show Call to undefined method TCG\Voyager\Models\Category::links() i use Voyager Model for my admin page please help me

0 likes
16 replies
tisuchi's avatar

Its because you are calling pagination on first() method that is not possible.

You need to change a bit-

Use-

        $categories = Category::where('slug',$slug)->paginate(10);

instead of-

        $categories = Category::where('slug',$slug)->first();

Ref: https://laravel.com/docs/5.4/pagination

2 likes
mlazuardy's avatar

now i got new error ''' "Undefined property: Illuminate\Pagination\LengthAwarePaginator::$name ''' here

<?php $__env->startSection('title',$category->name); ?>

i use $category->name to echo the $category-> name of my category

tisuchi's avatar

In your controller, do dd() and show the result-

dd($categories);
2 likes
mlazuardy's avatar

what exactly i must write in my CategoryController sir? i copy your code and show

"Undefined variable: categories"

really sorry i'm new, still learn php and laravel at the same time .

tisuchi's avatar

Show your full code of public function show($slug) method.

mlazuardy's avatar

here sir

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use TCG\Voyager\Models\Post;
use TCG\Voyager\Models\Category;
class CategoryController extends Controller
{


    public function show($slug)
    {
        //
        dd($categories);
        $categories = Category::where('slug',$slug)->paginate(12);
        if(!$categories){
          abort(404);}
        return view('categories.show')->with('categories',$categories);
        }

}

and it show error as i said before, because i dont know how to use that dd($categories);

rabib's avatar

Use dd()before return view('categories.show')->with('categories',$categories); and after checking dd($categories) remove dd($categories) from controller function and use {{$post->name}} in your view file

mlazuardy's avatar

can you write for me the full code of my public function show mr. @rabib

tisuchi's avatar

Can you run this code and show what displays in browser.

public function show($slug)
    {
        //
        
        $categories = Category::where('slug',$slug)->paginate(12);

    dd($categories);

        if(!$categories){
          abort(404);}
        return view('categories.show')->with('categories',$categories);
        }
2 likes
mlazuardy's avatar

it just show like this

LengthAwarePaginator {#665 ▼
  #total: 1
  #lastPage: 1
  #items: Collection {#672 ▶}
  #perPage: 12
  #currentPage: 1
  #path: "http://localhost:8000/category/info"
  #query: []
  #fragment: null
  #pageName: "page"
}
tisuchi's avatar

Can you show full code of Category.php and Post.php model?

mlazuardy's avatar

Post.php

<?php

namespace TCG\Voyager\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Traits\Translatable;
use TCG\Voyager\Models\Category;
use App\User;
use App\Comment;
class Post extends Model
{
    use Translatable;

    protected $translatable = ['title', 'seo_title', 'excerpt', 'body', 'slug', 'meta_description', 'meta_keywords'];

    const PUBLISHED = 'PUBLISHED';

    protected $guarded = [];

    public function save(array $options = [])
    {
        // If no author has been assigned, assign the current user's id as the author of the post
        if (!$this->author_id && Auth::user()) {
            $this->author_id = Auth::user()->id;
        }

        parent::save();
    }
    public function user()
    {
      return $this->belongsTo(User::class,'author_id','id');
    }

    public function authorId()
    {
        return $this->belongsTo(Voyager::modelClass('User'), 'author_id', 'id');
    }

    /**
     * Scope a query to only published scopes.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePublished(Builder $query)
    {
        return $query->where('status', '=', static::PUBLISHED);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function category()
    {
        return $this->hasOne(Voyager::modelClass('Category'), 'id', 'category_id');
    }

    /**
     *   Method for returning specific thumbnail for post.
     */
    public function thumbnail($type)
    {
        // We take image from posts field
        $image = $this->attributes['image'];
        // We need to get extension type ( .jpeg , .png ...)
        $ext = pathinfo($image, PATHINFO_EXTENSION);
        // We remove extension from file name so we can append thumbnail type
        $name = rtrim($image, '.'.$ext);
        // We merge original name + type + extension
        return $name.'-'.$type.'.'.$ext;
    }
    public function comments()
    {
      return $this->hasMany(Comment::class);
    }

    public function getRouteKeyName()
    {
      return 'slug';
    }
}

and the Category.php

<?php

namespace TCG\Voyager\Models;

use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Traits\Translatable;
use TCG\Voyager\Models\Post;
class Category extends Model
{
    use Translatable;

    protected $translatable = ['slug', 'name'];

    protected $table = 'categories';

    protected $fillable = ['slug', 'name'];

    public function posts()
    {
        return $this->hasMany(Voyager::modelClass('Post'))
            ->published()
            ->orderBy('created_at', 'DESC');
    }
    public function tutorials()
    {
      return $this->hasMany(Tutorial::class);
    }
    


    public function parentId()
    {
        return $this->belongsTo(self::class);
    }
}

tisuchi's avatar

Can you try this-

Controller.php

public function show($slug)
{

    $categories = Category::where('slug',$slug)->paginate(12);

    if(!$categories){
        abort(404);
    }
    return view('categories.show')->with('categories',$categories);
}

In your View-

@foreach ($categories->posts as $post)
<div class="col-md-4">
    <a href="{{url('blog',$post->slug)}}"><div class="card">
      <img class="card-image-top" src="/storage/{{  $post->image}}" alt"card image">

        <span class="category">{{$categories->name}}</span>

      </br>
        <h2 class="card-title">{{ $post->title}}</h2>
        <div class="commit-button-default">
                <a href="{{url('blog',$post->slug)}}" id="commit-button">Read More</a>
        </div>

      </div>
    </a>
</div>
@endforeach 



<nav class="page-navigation">
  {{ $categories->links() }}
</nav>

It should works.

Recommendation for you: https://laracasts.com/series/laravel-from-scratch-2017

2 likes
mlazuardy's avatar

and it come

"Undefined property: Illuminate\Pagination\LengthAwarePaginator::$name (View: D:\XAMPP\htdocs\commit\resources\views\categories\show.blade.php)"

I really sorry sir, i have a trouble too with my Mobile Hotspot Provider and cant open Laracast for now, im in Village for now to complete my study for 1 month. didnt see wi-fi here

tisuchi's avatar

Change your view with this code-

@foreach ($categories->posts as $post)
<div class="col-md-4">
    <a href="{{url('blog',$post->slug)}}"><div class="card">
      <img class="card-image-top" src="/storage/{{  $post->image}}" alt"card image">

        <span class="category">{{ $post->category->name}}</span>

      </br>
        <h2 class="card-title">{{ $post->title}}</h2>
        <div class="commit-button-default">
                <a href="{{url('blog',$post->slug)}}" id="commit-button">Read More</a>
        </div>

      </div>
    </a>
</div>
@endforeach 



<nav class="page-navigation">
  {{ $categories->links() }}
</nav>
2 likes
mlazuardy's avatar
Call to undefined method TCG\Voyager\Models\Category::links() (View: D:\XAMPP\htdocs\commit\resources\views\categories\show.blade.php)"

Help me again sir

Please or to participate in this conversation.