And how does your controller code looks like?
also try links() instead of Links().
I have tried to use a pagination but its says "Call to undefined Method"
And how does your controller code looks like?
also try links() instead of Links().
Sorry my bad, i did use 'links()'
Here is my controller code....
{
$posts = Post::orderBy('title', 'desc')->paginate(10);
return view('posts.index')->with('posts', $posts);
}
@captainburah how sure are you that the error comes from using $posts->links()?
Can you remove that from the page, and try to reload to ensure that the page loads without using that?
Without the code {{$posts->links()}} the page loads fine as called by the controller.
When i insert the pagination, it prompts that error "Call to Method Undefined".
But i did change my Bootstrap 3 to Bootstrap 4 recently.
Bootstrap has nothing to do with it.
I just doubt that on the page where you use $posts->links() you are not using a paginated results.
Try dumping the results of $posts in your controller and make sure that the object is an instance of a Paginator.
You can do that using dd($posts);. If it doesn't stops, that means that the posts are coming in from somewhere else. In case you use a View Composer and you share the posts from within the composer, than this values will be overwritten.
Paginated results? Im sorry, im learning laravel, began this week.
Where do i place the dd($posts); in the controller?
Here is the code of my 'Views'...
@extends('layouts.app')
@section('content')
<h1>POSTS</h1>
@if(count($posts)>0)
@foreach($posts as $posts)
<div class="card card-body mt-3">
<h3><a href="/posts/{{$posts->id}}">{{$posts->title}}</h3>
<small>Written on {{$posts->created_at}}</small>
</div>
@endforeach
{{$posts->links()}}
@else
<p>No Posts Found</p>
@endif
@endsection
Here is the code of my 'Controller'...
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
public function index()
{
$posts = Post::orderBy('title', 'desc')->paginate(10);
return view('posts.index')->with('posts', $posts);
}
Are you copy pasting the full files or just partials? Because your controller does not look good if that's the full content.
you place it below the initialization in the controller:
$posts = Post::orderBy('title', 'desc')->paginate(10);
dd($posts);
No i just copy pasted the partials. Omitted the Comments. Here's the full 'controller'
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
//use DB;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//$posts = Post::all();
//return Post::where('title', 'Post Two')->get();
//$posts = DB::select('SELECT * FROM posts');
//$posts = Post::orderBy('title', 'desc')->take(1)->get();
$posts = Post::orderBy('title', 'desc')->paginate(10);
return view('posts.index')->with('posts', $posts);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$post = Post::find($id);
return view('posts.show')->with('post', $post);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
@captainburah the comment by @wingly should solve your issue:
@foreach($posts as $post)
<div class="card card-body mt-3">
<h3><a href="/posts/{{$post->id}}">{{$post->title}}</h3>
<small>Written on {{$post->created_at}}</small>
</div>
@endforeach
{{$posts->links()}}
@captainburah How @wingly mentions, just change it to this
@extends('layouts.app')
@section('content')
<h1>POSTS</h1>
@if ($posts->isNotEmpty())
@foreach ($posts as $post)
<div class="card card-body mt-3">
<h3><a href="/posts/{{ $post->id }}">{{ $post->title }}</h3>
<small>Written on {{ $post->created_at }}</small>
</div>
@endforeach
{{ $posts->links() }}
@else
<p>No Posts Found</p>
@endif
@endsection
Make it as i said like that
@extends('layouts.app')
@section('content')
<h1>POSTS</h1>
@if(count($posts)>0)
@foreach($posts as $post)
<div class="card card-body mt-3">
<h3><a href="/posts/{{$post->id}}">{{$post->title}}</h3>
<small>Written on {{$post->created_at}}</small>
</div>
@endforeach
{{$posts->links()}}
@else
<p>No Posts Found</p>
@endif
@endsection
You are overwriting the value of $posts in your foreach loop. since $posts represents a collection of Post, you should use $post as a name for one of them.
@section('content')
<h1>POSTS</h1>
@if(count($posts)>0)
@foreach($posts as $post)
<div class="card card-body mt-3">
<h3><a href="/posts/{{$post->id}}">{{$post->title}}</h3>
<small>Written on {{$post->created_at}}</small>
</div>
@endforeach
{{$posts->links()}}
@else
<p>No Posts Found</p>
@endif
@endsection
@wingly Thanks mate. That was the issue...
Now i get the pagination on the page, except there is no styling, only numbers & arrows... <<12345678910>> Is this bcz i changed my Bootstrap version to v4?
You can customize view, so read this part of documentation
https://laravel.com/docs/7.x/pagination#customizing-the-pagination-view
Thank You @michaloravec . Ill check on that right away..
Please or to participate in this conversation.