Laravel 5.2 - Method links does not exist - php

i'm passing my array $posts to my view and i'm tryng use pagination but i have the error:
Method links does not exist. (View:
C:\xampp\htdocs\app\resources\views\search.blade.php)
CONTROLLER
$posts = Post::where('visible', 1)
->where('expire_date', '>', $current)->where('delete', 0);
$posts->paginate(1);
$posts = $posts->get();
return view('search', compact('posts'));
VIEW
<div class="pagination-bar text-center">
{{ $posts->links() }}
</div>

Change you code to this:
$posts = Post::where('visible', 1)
->where('expire_date', '>', $current)
->where('delete', 0)
->paginate(1);
return view('search', compact('posts'));
Your code doesn't work because you do not save paginate() results to a variable, like $posts = $posts->paginate(1);. Also, you shouldn't use get() or all() after paginate().

Related

Laravel how to add pagination on categorized posts

I have an laravel-application where I want to add pagination to my "blog-posts"-section. I can make it work when I return all blog_posts, but since they are categorized, the pagination does not work anymore.
Here is my controller
public function index() {
$blog_posts = Blog::where("publishes_on", "<=", Carbon::now())
->orderBy('publishes_on', 'desc')
->where('blog_status_id', '=', '2')
->with('blog_category')
->paginate(4);
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blog_categories', 'blog_posts'));
}
and the blade view
#foreach($blog_categories as $category)
#foreach($category->blog_posts as $blog)
<div>
{{ $blog->title }}
</div>
#endforeach
#endforeach
Before I added the categories, my blade view looked like this
#foreach($blog_posts as $blog)
<div style="border-top: 2px solid red; padding: 4px;">
{{ $blog->title }}
</div>
#endforeach
<div>
{{ $blog_posts->links() }}
</div>
but now, I don't know how to add the pagination?
can someone help me out
You can use setRelation to override blog_posts relationship and have it return a paginated collection instead:
<?php
$blog_categories = BlogCategory::get()
->map(function($blogCategory) {
return $blogCategory
->setRelation( 'blog_posts', $blogCategory->blog_posts()->paginate(10) );
});
Calling paginate() returns an instance of Illuminate\Pagination\LengthAwarePaginator
To get collection of blog posts, use this:
$category->blog_posts->getCollection()
To get pagination links view:
$category->blog_posts->links()
It's all untested, so let me know if it doesn't work
You can create your custom pagination: (https://laravel.com/api/5.8/Illuminate/Pagination/LengthAwarePaginator.html)
$all_posts = Blog::where("publishes_on", "<=", Carbon::now())
->orderBy('publishes_on', 'desc')
->where('blog_status_id', '=', '2')
->with('blog_category')
->get();
$blog_categories = BlogCategory::get();
$count = count($all_posts);
$page = (request('page'))?:1;
$rpp = 4; //(request('perPage'))?:10;
$offset = $rpp * ($page - 1);
$paginator = new LengthAwarePaginator(
$all_posts->slice($offset,$rpp),$count,$rpp,$page,[
'path' => request()->url(),
'query' => request()->query(),
]);
$blog_posts = $paginator;
return view('blog.index', ['blog_posts' => $blog_posts,
'blog_categories' => $blog_categories]);

having laravel category (slug based) posts with pagination

I have this function, where i get my categories base on their slug
public function postcategoryposts($slug){
$category = PostCategory::where('slug','=',$slug)->firstOrFail();
return view('front.postcategory-posts', compact('category'));
}
Currently i'm getting my each category posts in blade like:
#foreach($category->posts as $post)
{{$post->title}}
#endforeach
until here everything is normal, the problem is how to divide my posts into pages? I am not able to use:
$category = PostCategory::where('slug','=',$slug)->paginate(10);
because I'm using
firstOrFail();
any idea?
You need to add a function to your model to get posts and then paginate.
In your controller
public function postcategoryposts($slug)
{
$category = PostCategory::where('slug','=',$slug)->firstOrFail();
$posts = $category->getPaginatedPosts(10);
return view('front.postcategory-posts', compact('posts'));
}
In your model
// PostCategory model
public function getPaginatedPosts($limit = 10)
{
// Get category posts using laravel pagination method
return Post::where('category_id', '=', $this->id)->paginate($limit);
}
In your blade view
<div class="container">
#foreach ($posts as $post)
{{ $post->title }}
#endforeach
</div>
{{ $posts->render() }}
More informations about Laravel pagination here
Hope it's helps you :)

Laravel Paginate not working

I was trying to paginate some data with Query builder with the following query.
public function productSearch(Request $request)
{
$name = $request->name;
$products = DB::table('products')
->where('name', 'LIKE', "%$name%")
->paginate(4);
return view('product',compact('products');
}
But when I tried to show the pagination in the view page with the following line
{{ $products->links() }}
It shows
Method links does not exist
What could the possible error be since the pagination does not show?
2 types to print pagination link in laravel -
Try with -
use Illuminate\Support\Facades\DB;
public function productSearch(Request $request)
{
$name = $request->name;
$products = DB::table('products')
->where('name', 'LIKE', "%$name%")
->paginate(4);
return view('product',['products' => $products]);
}
In View -
<div class="container">
#foreach($products as $product)
<h4>{{ $product['name'] }}</h5> <br>
#endforeach
</div>
1st type to defined laravel pagination link -
{{ $products->links() }}
And 2nd type to defined laravel pagination link -
{{ $products->render(); }}
Note - Also you missed in your code -> return view('product',compact('products')); in return view('product',compact('products');
Here is your solution.
Just change your return view option
return view('product')->with('products', $products);
Hope this helps you.
Change to :
return view('product',compact('products'));
In View page try :
{{$products->render()}}
In your Controller:
return view('product',compact('products');
In your View:
#foreach($products as $product)
{{ $product->name }}
#endforach
{{ $products->links() }}
try with:
{{ $products->render() }}

Dyanmic url in laravel

I am a beginner in laravel so please excuse me if this is a simple question
I am trying to pass a db variable through my url so e.g blog?=category
It displays all results but when i change the url to blog/category1 it doesn't display anything.
DB Fields:id,category, name
1 category1 test
This is in my routes:
Route::get( '/blog', 'BlogController#index' );
Route::get('/', 'HomeController#index');
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category);
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')
->with('posts', $posts);
});
BlogController
class BlogController extends BaseController {
public function index()
{
// get the posts from the database by asking the Active Record for "all"
$posts = Post::all();
// and create a view which we return - note dot syntax to go into folder
return View::make('blog.index', array('posts' => $posts));
}
}
This is my view layout:
#extends('base')
#section('content')
#foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
#endforeach
#stop
Thanks
You need to add ->get() after your Eloquent query to fetch the results.
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category)->get();
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')->with('posts', $posts);
});

Passing arguments from route through view via controller doesn't work

I'm creating a Laravel 4 webapp and got the following route:
Route::get('products/{whateverId}', 'ProductController#index');
This is my index-function in ProductController:
public function index($whateverId)
{
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products', compact('products'), $data);
}
In my view, this returns the following error:
<p>Product: {{ $data['product'] }}</p>
ErrorException
Undefined variable: data (View: /Users/myuser/webapp/app/views/products.blade.php)
return View::make('products', compact('products'), "data"=>$data);
(or compact('data'))
Try passing it as:
$data['whateverId'] = $whateverId;
$data['products'] = Product::all();;
return View::make('products', $data);
And you'll have acces to it as
{{ foreach($products as ...) }}
and
{{ $whateverId }}
Or you can
$products = Product::all();
$data['whateverId'] = $whateverId;
return View::make('products')
->with('products', $products)
->with('whateverId', $whateverId);

Categories