increment function increments by 2 not with 1 - laravel

Every time user clicks post then that post view should be incremented by 1.
But instead of 1 it is incrementing by 2.
There are many some pages where this post can be clicked.
I have tried using increment function
$view = PostAd::where('id',$id)->first();
$view->increment('viewcount',1);
full code
$view = PostAd::find($id);
$view->increment('viewcount',1);
$data['ads'] = PostAd::find($id);
$data['post']= PostAd::with('postimage')->where('id',$id)->get();
$data['postimage'] = PostAd::with('pimage')->where('id',$id)->get();
$data['details']= PostAd::with('category')->where('id',$id)->first();
$data['comments'] = Comment::where('post_id',$id)->get();
$data['favourite'] = Favourite::where('post_id',$id)->first();
$data['identify'] = PostAd::with(['category','category.children'])->get();

Answer is simple. Use this...
$view->increment('viewcount');
Increment by a custom count (COUNT)
$view = PostAd::where('id',$id)->first();
$view->increment('viewcount',COUNT);
Ex: Increment by 5
$view->increment('viewcount',5);
Read more here

You can use default 1 increment.
$query->increment('viewcount');
If you need custom increment, use
$query->increment('viewcount',increment_value);
OR
PostAd::where('id', $id)
->update('viewcount' => DB::raw('viewcount + 1'));

If you use pagination with json, for example, after it loads the page request, a new request will be created to add ?page=1.
Try to keep the pagination code only in the view that requires pagination. Create a .js file to be listed only on pages that require this functionality. Not globally.

Related

Laravel append overall total value on pagination result

I have a Laravel query using pagination.
I want to be able to return the result based on the pagination but also get the overall total of the query and append this to the return. So for example the pagination is set to 5 but the overall total might be 20.
$query = Model::paginate(5);
$queryTotal = $query->total();
$query->append($queryTotal);
return $query;
The Laravel Paginator does this already.
You can see that when serializing results to JSON there is a total key which represents all rows matching that query.
You can also see there is a total method available from the paginator:
$results->total()
Along side other methods that can be found in the Pagination Docs
$query = Model::paginate(5);
return $query;
You can access overall total using
{{ $query->total() }}
For more Info read Paginator instance
The paginate function returns a LengthAwarePaginator object. It simply not possible to add another field to this object.
Your best option is to manually create a new collection in which you merge the LengthAwarePaginator with your newly added data.
An example would be:
$query = Model::paginate(5);
$addition = collect(['totalResult' => $query->total()]);
$queryData = $addition->merge($query);
return $queryData;
Naturally, if you just return the LengthAwarePaginator object, you can simply call the total() function, if you use it in your blade files for example.
Hope this helps!

Simple pagination in datatable using ajax without sending total count from server

I'm using DataTables 1.10.5. My table uses server side processing via ajax.
$('#' + id).dataTable({
processing: true,
serverSide: true,
ajax: 'server-side-php-script-url',
"pagingType": "simple_incremental_bootstrap"
});
Everything will work properly if I send 'recordsTotal' in the server response. But I don't want to count the total entries because of performance issues. So I tried to use the pagination plugin simple_incremental_bootstrap. However it is not working as expected. The next button always return the first page itself. If I give 'recordsTotal' in server response this plugin will work properly. I found out that If we don't give 'recordsTotal', the 'start' param sent by datatable to server side script is always 0. So my server side script will always return the first page.
According to this discussion, server side processing without calculating total count is not possible because “DataTables uses the record count that is passed back to it to deal with the paging controls”. The suggested workaround is “So the display records are needed, but it would be possible to just pass back a static number (like 1'000'000 or whatever) which would make DataTables think there are a million rows. You could hide the information element if this information is totally bogus!”
I wonder if anybody have a solution for this. Basically I want to have a simple pagination in my datatable with ajax without sending total count from server.
A workaround worth to try..
If we don't send recordsTotal from server, the pagination won't work properly. If we send a high static number as recordsTotal, table will show an active Next button even if there is no data in next page.
So I ended up in a solution which utilizes two parameters received in ajax script - 'start' and 'length'.
If rows in current page is less than 'limit' there is no data in next page. So total count will be 'start' + 'current page count'. This will disable Next button in the last page.
If rows in current page is equal to or greater than 'limit' there is more data in next pages. Then I will fetch data for next page. If there is at least one row in next page, send recordsTotal something larger than 'start + limit'. This will display an active Next button.
Sample code:
$limit = require_param('length');
$offset = require_param('start');
$current_page_data = fn_to_calculate_data($limit, $offset); // in my case, mysqli result.
$data = “fetch data $current_page_data”;
$current_page_count = mysqli_num_rows($current_page_data);
if($current_page_count >= $limit) {
$next_page_data = fn_to_calculate_data($limit, $offset+$limit);
$next_page_count = mysqli_num_rows($next_page_data);
if($next_page_count >= $limit) {
// Not the exact count, just indicate that we have more pages to show.
$total_count = $offset+(2*$limit);
} else {
$total_count = $offset+$limit+$next_page_count;
}
} else {
$total_count = $offset+$current_page_count;
}
$filtered_count = $total_count;
send_json(array(
'draw' => $params['draw'],
'recordsTotal' => $total_count,
'recordsFiltered' => $filtered_count,
'data' => $data)
);
However this solution adds some load to server as it additionally calculate count of rows in next page. Anyway it is a small load as compared to the calculation total rows.
We need to hide the count information from table footer and use simple pagination.
dtOptions = {};
dtOptions.pagingType = "simple";
dtOptions.fnDrawCallback = function() {
$('#'+table_id+"_info").hide();
};
$('#' + table_id).dataTable(dtOptions);

Return laravel query results in chunks

I have an Update model in my Laravel/Vue.js app, instead of retrieving and displaying all results at once in my component, I want to return them in chunks of fives and place a NEXT FIVE UPDATES link in my component to display the next 5 update records, much like in pagination. I tried this:
$update = Update::limit(5)->get();
This does not achieve my desired result. Is there a Laravel method i can use in my laravel backend to send the results in chunks of 5 to my Vue.Js component and then make my NEXT FIVE UPDATES link display the next 5 records.
If you're able to send some kind of page or offset value to the backend then you could use laravel's skip and take methods: https://laravel.com/docs/5.7/queries#ordering-grouping-limit-and-offset
$skip = $request->page; //assuming this variable exists...
$limit = 5;
$update = Update::skip($skip * $limit)->take($limit)->get();

Laravel Paginator getTotal returns null - Need total record count for all pages

I have implemented simple pagination using laravel. It works fine. However, I want to add total number of records and trying to use the method getTotal() but it returns null value.
$records = DB::table('tablename')
->where(condition)
....
....
->simplePaginate(10);
In the view, adding links works fine.
{{$records->links();}}
When I use,
{{$records->getTotal();}}
it returns null.
If I use,
{{$records->count();}}
it returns the count of records for a given page.
Any insights please?
That's how simplePaginate works. From the docs:
If you are only showing "Next" and "Previous" links in your pagination view, you have the option of using the simplePaginate method to perform a more efficient query. This is useful for larger datasets when you do not require the display of exact page numbers on your view.
The simple method is simple because it doesn't need to do the extra, inefficient-on-large-tables count query to obtain the total.
There is method total() in that pagination object. Just call that object. You will get total count.
ex: $records->total();
I had the same problem, what I did was
$records = DB::table('tablename')
->where(condition)
....
....
->paginate(1); //set as 1
$total = $records->getTotal(); //get total
Then returning it like this(I am using ajax that is why I return it as array):
return Response::JSON(array(
'pagination' => (string) $records->links('pagination::simple'),
'total_records' => $total
));

Changing the URL/URI of pagination in Codeigniter

I'm using the pagination class in Codeigniter to page out some info from my database.
It's all working perfectly, however, i'm trying to tidy up the URLs that people use to access the pages.
Currently, i'm pulling out the offset from the URL and have set it to 20 results per page:
http://www.mysite.com/page/20
So if you go to (in this instance) page two, it starts from db entry 20.
The trouble is that firstly, since i've set it to 20 results per page, it means I get odd URLs like http://www.mysite.com/page/60 which would be page 4.
Secondly it's a double figure which i'd like to tidy up.
Basically want I want to do is set the URLs to look like:
http://www.mysite.com/page/2
http://www.mysite.com/page/3
http://www.mysite.com/page/4
etc...
I set the use page numbers to TRUE, but the trouble is it then sets the offset to 2, 3, 4 etc when it should be 20, 40, 60.
Any ideas? I was thinking of some kind of calculation in the model to work out how many entries it should offset based on the individual page number from the URL (when use page numbers is set to TRUE). I can't seem to get it right though...
In codeigniter number in url means: row per page
So if you need to change per page number with page number change your code like this:
Controller
class news extends CI_Controller{
function index($page = 'page', $page_number = 0){
$row_per_page = 20; // define page per page
// for example if page number == 4 then offset 80
$offset = $page_number * $row_per_page
// we need to request data from database
// we need this query "SELECT * FROM `mytable` limit 80,20"
// offset = 80 AND per page = 20
$this->db->get('mytable', $offser, $row_per_page);
$this->load->library('pagination');
// you can change this url by URI Routing and other solutions.
/*
change application/config/routes.php to nice url
$route['news/page/(:num)'] = "news/index/page/$1";
*/
$config['base_url'] = 'http://example.com/index.php/news/index/page/';
// Change $count_of_rows with your query num_rows you can use SQL_CALC_FOUND_ROWS
$config['total_rows'] = $count_of_wors;
$config['per_page'] = $row_per_page;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
}
}
This is very simple. Enjoy CI

Resources