Laravel - Paginating Eloquent Retrieved results - pagination

This what i have in my controller
$projects=Project::where_sup_id($user_id)->get();
$total=count($projects);
$per_page=3;
$projects = Paginator::make($projects, $total, $per_page);
return View::make('project.index')->with('projects',$projects);
And this in my View
#foreach ($projects->results as $project)
{{$project->title}}
#endforeach
{{$projects->links();}}
but when i view it in the browser it is displaying all rows in all pages...the links are displaying perfectly! what do u think is the problem? please help! thank u in advance!

You are counting all rows, but not using limit at all, Laravel fluent query builder has skip() and take() method for that.
Btw, you don't need to manually paginate it. Laravel does pagination automatically with paginate() method.
Do it that way:
$projects = Project::where_sup_id($user_id)->paginate(3); // 3 means records per page
return View::make('project.index')->with('projects', $projects);
You are doing view work correctly.

Related

Set parameter for URL in html and use parameter in Node.js Express for routing

I very much new to Node JS and Express Framework & mongodb. I am working on a website which has multiple categories and when we click on category it goes to a view, where based on the category clicked, data gets loaded in the view.
please check the link for website here.
I am looking help for navbar links eg. Montessori Premium->Toddler Material
As these are static links I can not figure how to pass any parameter or url format for this.
I have done the routing for individual products as they have ids coming from database so I am using that id in url.
Thanks in advance.
It seems to me like you're just missing a bit of glue in between the front-end and the back-end database calls. Here's a rough idea of the sequence you might be after and an explanation of what I think you're missing.
If I'm wrong, please add a comment and update your question to include the new details and I'll see about editing my answer.
In this example I'm creating a website which has a list of "items" in a shop and we're looking to display them all on a single page. Each item should then have an a href clicking through to that specific item where there might be more detail such as a "full description" of the item and maybe some images or something. We're going to look at the flow to build that first page with the list of items.
The user makes a HTTP GET call to the root of your website such as "http://www.mystore.com/".
This request comes through in Express to your controller which takes the req, res, next parameters.
Your controller makes a call to the database to get a list of all items including their names and ID's.
You then use something called templating to inject in the list of items.
In this template you iterate through the list doing something like this.
.
<ul>
{{#each items}}
<li><a href="/items/{{id}}>{{name}}</a></li>
{{/each}}
</ul>
In this template (which happens to be Handlebars), we're going through the list of items and using the id and name of each to inject them into the HTML to get something a bit like this back.
<ul>
<li><a href="/items/1>Shoes</a></li>
<li><a href="/items/2>Red spotted dress</a></li>
<li><a href="/items/3>Jeans</a></li>
</ul>
This is then served up to the user once the template has been processed.
In essence I think you're after some sort of templating engine, I'd highly recommend having a look at Handlebars with something like the express-hbs.
Thank you #Elliot for your time to answer this. Yes I am using handlebars for templating. The solution I found is, because I am using static links, I am passing query string in anchor tag link.
eg. http://localhost:8000/product?category=Toddler_Material
and then using req.query for capturing the values (Toddler_Material) in router
var category = req.query.category;

What's currently the best way to pass data from a given content item to a variable Form in Orchard

I have a product package with several options in my Orchard site and I need users to be able to send an enquire based on a single option:
I've seen out there many articles about Tokens, Dynamic Forms (the examples using Dynamic Forms seem to be just for a single form type), I can think of many ways to achieve this but I don't know what is really the cleanest and fastest way to implement this functionality.
I would need a hint on what to keep researching to speed this up.
Thank you in advance
In your view, I'd wrap the button around an <a> tag something like this:
<a href="#(String.Format("/{0}/{1}", "x", y))" target="_blank">
//put button code or whatever here
</a>
Where 'x' is the extended url and y could be the query string such as: www.google.com/game/q=2
When you do this and get the redirect correct, in your driver for the page, you can get the page number from the url using something like this:
private readonly IHttpContextAccessor _httpContextAccessor;
var httpContext = _httpContextAccessor.Current();
var query = httpContext.Request.QueryString["q"];

Orchard query content items in my custom controller

The title pretty much sums it up. I created my own module in Orchard. I can access its actions via http requests like an ordinary controller in any MVC application.
I generated the controller using the command line interface and it came with IOrchardServices property that is populated in the constructor.
On my Orchard site I have a blog which I filled up with about 40 blog posts.
How can I query these blog posts from within my controller?
First I would like to start by saying: "read the source luke". You may find Orchard lacking in documentation and examples, but because it is open source, pretty much everything you want to know can be found there.
You should use the BlogPostService, inject that into your controller to get the blog posts you want.
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogPostService.cs
You can see it being used in several controllers within Orchard.Blogs:
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogPostController.cs
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogAdminController.cs
Check out the code in the BlogPostService to see how it works, it is a little confusing because blogs are content items with blog posts underneath them. If you want to learn about simpler querying of content items I would check out how the BlogService works, it is a little easier to get to grips with:
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogService.cs
Here is an example of what I was looking for:
var query = Services.ContentManager.Query();
var list = query.ForType(new []{"BlogPost"}).List();
var result = new List<dynamic>();
foreach (var contentItem in list) {
result.Add(new
{
title = contentItem.As<TitlePart>().Title, // dynamically typed: ((dynamic)contentItem).TitlePart.Title
text = contentItem.As<BodyPart>().Text
});
}

Can I set default page to last page when paginating in Laravel 4?

In laravel 4, it's really convenient to do pagination. When the page parameter is missing, the default page is the first page. In my project, it's better to be the last page as default value.
Currently I'm using following UGLY way to hack the pagination. Is there any better way to do this?
if(!Input::get('page') && $comments->getLastPage() > 1)
//Redirect with page=lastPage
And the question can be also asked as that can I set the current page of paginate() function manually? If no, what can I do if I don't want the page parameter to be named page.
If you can live with setting the pagination manually:
// check if a page is set
if(!Input::get('page'))
{
// work out the last page
$lastPage = ceil($totalItems/$perPage);
// set the page - maybe merge would be better than replace???
Input::replace(array('page' => $lastPage));
}
// manually create the paginator
$paginator = Paginator::make($items, $totalItems, $perPage);
I don't know what you're displaying, but would reversing the sorting not work?
Paginator docs

Mongoid Pagination problem

I'm struggling to paginate using mongoid. I'm able to call paginate on my collection object like
#results = #collections.paginate :page => 1, :per_page => 10
but how it needs to be referred in view?
In my view I have rendered the collection in partial as
<%= render :collection => #collections, :partial => collect.xml %>
<%= will_paginate #results %>
In the above line I get the error undefined method total_pages for array.
But there is no error if I remove the will_paginate call in view and all the collections are shown in the view without pagination. Pls help.
I think I'm going wrong in calling the pagination helper in view. I have searched for quite a long time and did not find an example that included the pagination call in view for mongoid.
Forgive me if it is a dumb question. I'm new to mongoid.
This method call
#results = #collections.paginate :page => 1, :per_page => 10
will populate #results with an array of 10 items from #collections. You don't need to do use will_paginate at all, just use the #results object as is.
If you get problems with the view giving more than 10 results, debug the controller and the view to ensure there isn't something in between messing with your #results array.
EDIT:
Aha, so the problem is in displaying pagination links on the view. Yes, for that you would need the view to know things like the current page and the total number of pages available, so that you can compute how many links are necessary.
In return, your links would need to let the controller know what page is being requested, which you can achieve with GET request parameters. Helpers are probably the best way to keep the view clean.

Resources