Can anyone help me? I use redis cache. But I see same results on every pages when I use pagination. How can I fix it? Thanks.
You should cache your results per page, with a key that is the current page.
$currentPage = request()->get('page',1);
$category = Cache::remember('sellcategory-' . $currentPage, 10, function(){
return DB::table('elans')->orderBy('updated_at', 'desc')->where(['derc' => 1,'elaninnovu' => 'Satılır'])->paginate(10);
});
This solution to cache and clear pagination caches.
How to cache:
$page = request()->get('page', 1);
$limit = request()->get('limit', 10);
$users = Cache::remember('admin' . $page, 10, function() use ($limit){
return DB::table('users')->paginate($limit);
});
You can use a loop to check the prefix and delete them like this.
public static function forgetCaches($prefix)
{
// Increase loop if you need, the loop will stop when key not found
for ($i=1; $i < 1000; $i++) {
$key = $prefix . $i;
if (Cache::has($key)) {
Cache::forget($key);
} else {
break;
}
}
}
Clear caches:
// Clear caches:
forgetCaches('admin');
Related
I'm creating 192 html pages using this function in the web.php file, but when it creates the fifth page gives me the error.
function()
{
$homepage = Page::find(1);
$articles_show = Article::where(function ($query) {
$query->where(function ($query2) {
$query2->where('draft', 0)->whereNull('publication_date');
})->orWhere(function ($query2) {
$query2->where('draft', 0)->where('publication_date', '<=', DateHelper::currentDateTime());
});
})->orderBy('publicated_at', 'DESC')->get();
$last_page = ceil($articles_show->count() / 8);
$articles = [];
$count = 0;
for ($i = 5; $i <= $last_page; $i++) {
$filename = 'page'.$i.'.html';
$max_desc_id = $i * 8;
$min_desc_id = $max_desc_id - 7;
foreach ($articles_show as $article) {
$count++;
if ($article->desc_id >= $min_desc_id && $article->desc_id <= $max_desc_id) {
$articles[] = $article;
}
if (($count % 8) == 0) {
$count = 0;
File::put(
resource_path('views/allArticlesHtml/'.$filename),
view('allArticlesTemplate')->with([
"articles" => $articles,
"homepage" => $homepage,
"this_page" => $i,
"last_page" => $last_page
])->render()
);
continue;
}
}
$articles = [];
// if(($count % 8) == 0){
// }
}
}
The pages are created correctly but it's way too slow.
I don't really know if i am doing this in the right way or not, i'm still very new at programming and i don't know how to improve or recreate this code.
I had this issue the other day. You can do this workaround:
Every time you create 1 file under that command put this:
set_time_limit(30);
It will reset the time limit to 30 seconds and every time reset it.
Reference: https://www.php.net/manual/en/function.set-time-limit.php
I read your code. I'll try to suggest you best solution.
why you are trying to create html files with same content?
you can easily use blade engine and extend a template in different file.
to solve the error Maximum execution time of 60 seconds exceeded
you need to increase max_execution_time value in php.ini file.
or put ini_set('max_execution_time', 180) at the top of php file .
but if you want an alternative way of creating files describe your initial problem.
I want to get Laravel Redis cache key using regex or like key operator.
I have tried everything and spend lost of time but no luck with that can anyone please help on this.
$redis = Cache::getRedis();
$keys = $redis->keys("*$key_name*");
$count = 0;
$result = [];
foreach ($keys as $key) {
$result[$key] = $redis->get($key);
}
return $result;
I have tried above code but no luck. can any one help on this?
You can implement something like
$redis = Cache::getRedis();
$keys = $redis->keys("*$key_name*");
$count = 0;
$result = [];
foreach ($keys as $key) {
$result[$key] = $redis->get($key);
}
return $result;
Here is how I currently find the intersection of multiple Laravel collections:
$stuff = [
collect(['a','b','c','d']),
collect(['b','c','d','e']),
collect(['c','d','e','f']),
];
$i = 0;
foreach ($stuff as $current) {
$i++;
if ($i === 1) {
$common = $current;
} else {
$common = $common->intersect($current);
}
}
dd($common);
Is there a more efficient method? It seems a bit clumsy to have to treat the first collection differently (via the if...else...).
Laravel has so many good collection methods I suspect there is a more elegant approach to this problem.
Reduce may be a option for this:
$intersection = collect([
collect(['a','b','c','d']),
collect(['b','c','d','e']),
collect(['c','d','e','f']),
])->reduce(function ($carry, $item) {
return $carry->empty() ? $item : $carry->intersect($item);
});
I have a problem with CI sessions.
I initialized my session library:
$autoload[‘libraries’] = array(‘database’, ‘session’) (In config/autoload.php)
This is my code:
cycle
$this->load->library(‘image_moo’);
// Upload image and return unique name
$data = array(
‘image’ => $image,
);
$this->db->insert(‘category_images’, $data);
if (!$this->session->userdata(‘uploadImages’))
{
$this->session->set_userdata(‘uploadImages’, $this->db->insert_id());
}
else
{
$session = $this->session->userdata(‘uploadImages’);
$sessionData = $session.’|’.$this->db->insert_id();
$this->session->set_userdata(“uploadImages”, $sessionData);
}
echo $this->session->userdata(‘uploadImages’); // return 256; corect result - 255|256
end of cycle
This is script for image upload with jQuery File Upload (blueimp) and i need set ids of inserted in database images to session.
Can anyone help. Thank you!
To use CI Session you also need to provide the encryption key in your application/config.php:
$config['encryption_key'] = 'xxxxxx';`
Try this
cycle
$this->load->library(‘image_moo’);
// Upload image and return unique name
$data = array(
‘image’ => $image,
);
$this->db->insert(‘category_images’, $data);
if ($this->session->userdata(‘uploadImages’)=='')// or if (!isset($this->session->userdata(‘uploadImages’)))
{
$this->session->set_userdata(‘uploadImages’, $this->db->insert_id());
}
else
{
$session = $this->session->userdata(‘uploadImages’);
$sessionData = $session.’|’.$this->db->insert_id();
$this->session->set_userdata(“uploadImages”, $sessionData);
}
echo $this->session->userdata(‘uploadImages’); // return 256; corect result - 255|256
end of cycle
I have successfully created pagination on some of the pages on the application on which I am working with, but I can't make it on this one:
I have 7 records in the database, and when
page is displayed all 7 records are displayed instead of 5, as I would like to be.
Sure enough, links for the paging are not displayed.
Here is my controller code:
public function displayAllFaqCategories()
{
//initializing & configuring paging
$currentUser = $this->isLoggedIn();
$this->load->model('faqCategoriesModel');
$this->db->order_by('sorder');
$limit = 5;
$offset = 3;
$offset = $this->uri->segment(3);
$this->db->limit(5, $offset);
$data['faq_categories'] = $this->faqCategoriesModel->selectCategoriesAndParents();
$totalresults = $this->db->get('faq_categories')->num_rows();
//initializing & configuring paging
$this->load->library('pagination');
$config['base_url'] = site_url('/backOfficeUsers/faqcategories');
$config['total_rows'] = $totalresults;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$errorMessage = '';
$data['main_content'] = 'faq/faqcategories';
$data['title'] = 'FAQ Categories';
$this->load->vars($data,$errorMessage);
$this->load->vars($currentUser);
$this->load->view('backOffice/template');
} // end of function displayAllFaqCategories
And here is my model function code:
public function selectCategoriesAndParents($selectWhat = array())
{
$data = array();
$query = $this->db->query("SELECT fq . * , COALESCE( fqp.$this->parent_name, '0' ) AS parentname
FROM $this->table_name AS fq
LEFT OUTER JOIN $this->table_name AS fqp ON fqp.catid = fq.parentid");
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
} // end of function selectCategoriesAndParents
In the view, bellow of the table with the records I have the following code:
<?php echo $this->pagination->create_links();?>
Any help will be deeply appreciated.
Regards,Zoran
You've mixed two different things together I think. You're partially using the ActiveRecord class of CI, but then running the query yourself.
The simplest change would be:
// get all the rows
$data['faq_categories'] = $this->faqCategoriesModel->selectCategoriesAndParents();
// figure out the count of all of them
$totalresults = count($data['faq_categories']);
// only take some of the rows of the array, instead of keeping all of them and then showing all 7 of your records
$data['faq_categories'] = array_splice($data['faq_categories'], $offset, $limit);
Hopefully that should fix it!
To further explain what the original problem is, I think when you run this:
$totalresults = $this->db->get('faq_categories')->num_rows();
It takes the previous line $this->db->limit(5, $offset); into account, so it only returns 5 rows. Then, when you tell the pagination library that you only want to show 5 per page, the library thinks that it is actually showing all the results, so there is no need for pagination links!
Edit like this
$offset = $this->uri->segment(3) ? $this->uri->segment(3) : 0;