I have an SQL search where I use Eloquent whereRaw, and than paginatate results.
Proplem: the results are paginated and if I navigate to the next page the results will be gone and it will just "SELECT All " bacause laravel's pagination sends only ?page= GET request.
code:
if (Input::has('city')) {
$query .= ' and city = '.Input::get('city');
}
if (Input::has('area')) {
$query .= ' and area = '.Input::get('area');
}
if (Input::has('sub_location')) {
$query .= ' and sub_location = '.Input::get('sub_location');
}
if (Input::has('rent')) {
$query .= ' and rent = '.Input::get('rent');
}
if (Input::has('price_min') && Input::has('price_max')) {
$query .= ' and price < '.Input::get('price_max').' and price >'.Input::get('price_min');
}
$listings = ListingDB::whereRaw($query)->paginate(5);
return View::make('page.index')->with('title','Home')->with('listings',$listings);
so when I navigate to the next page all the Input is gone because laravel generates pagination only with ?page= GET variable, how can I solve it?
thank you.
you can append you query string to the pagination link
{!! $listings->appends(Input::all())->render() !!}
"Appending To Pagination Links
You may add to the query string of pagination links using the appends method. For example, to append &sort=votes to each pagination link, you should make the following call to appends:
{!! $listings->appends(['sort' => 'value'])->links() !!}
So in your case you need to add to pagination links like this if you have city and rent search applied then
{!! $listings->appends(['city' => 'london', 'rent' => 5000])->links() !!}
This will sort out your problem.
Related
I am working on the pagination of the results from the research page. The code has two pages.
index page that direct to the page with the search form.
the second page is the result page where the output should be displayed. I want the results to be displayed in pages. my code is:
in the search function the output this is the code
$final_list = $this->paginate($final_list,5);
return view('results',['list' => $final_list]);
The paginate function is:
public function paginate($items, $perPage = 5, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, page,
$options);
}
in the result page I display the data and add the {!! $list->links() !!} to get pages.
The code displays the output for the first page. The issue I am facing is when I click to the second or any other page it direct me to the index page where I have to fill in the search form again before displaying the result of that page.
I don't understand what is wrong in this code. Could you please help me. thank you
you need to append your search filter data to your main data array.
$detail_data->appends(['keyword' => $keyword]);
I share sample code of controller
$keyword = $request->keyword;
$deals = Deal::join('category','category.id','=','deals.deal_category_id')
->join('users','users.id','=','deals.user_id')
->select('deals.*', 'category.cat_name', 'users.name AS username', 'users.profile_photo_path')
->where('deal_isApproved', '=', 'Approved');
$deals->where(function($query) use ($keyword) {
$query->where('deal_title', 'like', '%'.$keyword.'%')
->orWhere('category.cat_name', 'like', '%'.$keyword.'%');
});
$detail_data = $deals->paginate(20);
$detail_data->appends(['keyword' => $keyword]);
I blade template file
{{ $detail_data->links() }}
I'm using laravel and I need to convert my array into a string so that I can add it to the phpSpreadsheet validation. So that when I download an excel spreadsheet
I can select a list of products. The problem I'm having is that I can't get it to be the format that is needed.
Here is the documentation PhpSpreadsheet
The correct format is
$validation->setFormula1('"Item A,Item B,Item C"');
Here is my code
public function generate()
{
$this->spreadsheet = new Spreadsheet;
$validation = $this->spreadsheet->getActiveSheet()->getCell('B5')->getDataValidation();
$validation->setType( DataValidation::TYPE_LIST );
$validation->setErrorStyle( DataValidation::STYLE_INFORMATION );
$validation->setAllowBlank(false);
$validation->setShowInputMessage(true);
$validation->setShowErrorMessage(true);
$validation->setShowDropDown(true);
$validation->setErrorTitle('Input error');
$validation->setError('Value is not in list.');
$validation->setPromptTitle('Pick from list');
$validation->setPrompt('Please pick a value from the drop-down list.');
$products = Product::all();
$product_list = [];
foreach($products as $product)
{
$product_name = $product->name;
array_push($product_list, $product_name);
}
$validation->setFormula1($product_list);
}
I tried a json_encode(), but it seems that excel doesn't like that.
Based on the documentation $product_list should be a comma delimited string not an array.
$products = Product::all();
$product_list = '';
foreach($products as $product) {
$product_name = $product->name;
$product_list .= '"' . $product_name . '",';
}
$validation->setFormula1(rtrim($product_list,','));
$validation4->setFormula1(implode(','), $product_list);
must be string right or formula range cell like =$A;
some value can find when you try make data validation list type on excel, you use that on your program.
I am using laravel pagination to paginate my products By default It gives me url like this
http://example.test/search?page=3
But I want some like
http://example.test/search/page/3
OR If Possible
http://example.test/search
On every page
This Is How I am doing it now but it is not giving me any result
{{ $books->appends(Request::except('page'))->links() }}
And I have also tried like this
$books = Book::paginate(10);
$books->withPath('/search');
But still no result
You can use this to generate custom link where you use link() in your blade.php.
#php
$links = $books->links();
$pattern = $replacement = array();
$pattern[] = '/'.$books->getCurrentPage().'\?page=/';
$replacement[] = '';
$customLinks = preg_replace($pattern, $replacement, $links);
echo $customLinks;
#endphp
Also you can use a package named laravel-paginateroute from spatie in github.
I am using karavan autosearch extention for magento1.7. and I want to modify the searching technique. In that search technique if we provide a full or partial name, the search engine works perfectly. but I want it works for reverse name. I mean If the exact name is 'test product', then if I use 'product test', that result will show same product in dropdown product list, what is now became empty. I have debug it and found that this search engine also using magento default search technique.
Any kind of Idea is acceptable.Please help me..
Thanks in advance..
Karavan extension uses default Magento search Model as backbone
app\code\local\Mage\CatalogSearch\Model\Resource\Search\collection.php
Find method _getSearchEntityIdsSql() and change there as u required.
$words = array();
if(str_word_count($this->_searchQuery)>1){
$words = explode(" ",$this->_searchQuery);
}
$ifValueId = $this->getConnection()->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
foreach ($tables as $table => $attributeIds) {
foreach($words as $word){
$selects[] = $this->getConnection()->select()
->from(array('t1' => $table), 'entity_id')
->joinLeft(
array('t2' => $table),
$this->getConnection()->quoteInto(
't1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id = ?',
$this->getStoreId()),
array()
)
->where('t1.attribute_id IN (?)', $attributeIds)
->where('t1.store_id = ?', 0)
->where($resHelper->getCILike($ifValueId, $word, $likeOptions));
}
if ($selects) {
$likeCond = '(' . join(' and ', $selects) . ')';
}
}
Some what like this.
Note: DOnt overwrite BAse Class
first be gentle im a begginer.
I have a problem with codeogniter pagination and im totally clules.
I watched a lot of tuts on the net and never got any aswer.
My problem is, im building a real-estate site, it has 2 types of estate, for sale and for rent.
My query looks like this
function for_sale()
{
$query = $this->db->query(" SELECT city, rand_id, price, image FROM estate WHERE type = 'for_sale' ");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $f)
{
$for_sale[] = $f;
}
return $for_sale;
}
}
my controller
function index()
{
$this->load->view("header");
$this->load->model('estate_model');
$for_sale['result'] = $this->estate_model->for_sale();
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/kpi/for_sale/index';
$config['total_rows'] = 22;
$config['per_page'] = 9;
$config['num_links'] = 19;
$this->pagination->initialize($config);
// $data['records'] = $this->db->get('ingatlan', $config['per_page'], $this->uri->segment(3));
$this->load->view("for_sale_view", $for_sale);
$this->load->view("footer");
}
and my view
foreach($results as $r) {
echo '<div class="grid_4">';
echo '<p class="title">Estate for sale</p>';
echo '<div class="thumbnail">
<div class="info">
<p class="bar">'.$r->city.'</p>
<p></p>
</div>
'.$r->image.'</div>';
echo '<div class="more">View details</div>';
echo '</div>';
}
So my problem is i cant figure this out how to use it with my query, i watched lots of tuts, that i need to load in the table library, and pass a data array like this
$data['records'] = $this->db->get('estate', $config['per_page'], $this->uri->segment(3));
anf give the per page this result `$this->db->get('estate')->num_rows(); but i would like this with my query, and not with a table.
So can someone hive me a hint?
Thank you
... i need to load in the table library, and pass a data array ...
how you display your data is totally up to you. the html-table class can help you with that, but it is not necessary to use it. your view should work just fine except for the fact that you want to iterate over $results which will be empty because you put your data into $for_sale['result'] (results != result).
in order for the pagination library to work with your query, you have to pass your query 2 parameters:
how many rows/records should be loaded from your db (= $config['per_page'])
how many rows/records should be skipped at the beginning (also known as 'offset', this value will be provided by the pagination library = $this->uri->segment(3))
So instead of:
$for_sale['result'] = $this->estate_model->for_sale();
Model:
function for_sale()
{
$query = $this->db->query(" SELECT city, rand_id, price, image FROM estate WHERE type = 'for_sale' ");
...
}
you should try:
$for_sale['result'] = $this->estate_model->for_sale($config['per_page'],$this->uri->segment(3));
Model:
function for_sale($per_page, $offset) {
$query = $this->db->query(" SELECT city, rand_id, price, image FROM estate WHERE type = 'for_sale' LIMIT $offset, $per_page");
...
return $query->result();
}
One last thing: the pagination library has to be initialized BEFORE you query the database.
And another last thing: $config['total_rows'] = 22; you will probably want to replace that with something like $this->estate_model->get_for_sale_total_rows()
Hope this helps.