Laravel 8 Multiple Pagination in one page with tabs - php

I have one page which has two tabs, each one has it's pagination.
It works correctly only when I click on tab, but when I click on previous or back page, it keeps returning to the first tab.
this is my code on controller:
$entries = Entry::paginate(5,['*'],'entries')
->appends(request()->except('entries'));
$company_responses= CompanyResponse::paginate(5,['*'],'company_responses')
->appends(request()->except('company_responses'));
on view
{{$entries->appends(request()->except('entries'))->links()}}
{{$company_responses->appends(request()->except('company_responses'))->links()}}
tabs links
<div class="tab">
<button class="tablinks" onclick="openTab(event,'tab1')">الملاحظات</button>
<button class="tablinks" onclick="openTab(event,'tab2')">القيود</button>
</div>
I don't know what I need to do to make it works on previous or back.

As suggested by Crisha:
Create a separate query for each tab and paginate the results
$tab1 = Model::where('column', 'value')->paginate(10);
$tab2 = Model::where('column', 'value')->paginate(15);
I think you should be able to use this:
{{ $tab1->fragment('tab_id')->links() }}
{{ $tab2->fragment('tab_id')->links() }}
That should then add the tab # to the pagination links, you might need to add tab1 & tab2 where is has tab_id - as this is untested.

Related

How to add button for every field when retrieving data from a database

I have looked everywhere for this question, perhaps I'm just not phrasing it correctly.
I have a page displaying various products, the table is called items. There in my fields I have all the information and images to display on the page, this works fine. Now I want to be able to press a button to add the item to cart, I have the code below where I access the data.
<div class="col-md-5">
<h2 style="font-size:25px;"> <?=$items['title'];?></h2>
<img src="<?=$items['image'];?> "width='300' height='500'/>
<p style="font-size:20px;" class = "lprice">GBP <?= $items['price'];?></p>
</div>
Now that I have done that, my question is how can I add a button for every time a result is displayed and have that button linked with that item, I appreciate this may no be as straight forward as I am phrasing this, but just looking for some help.
Thanks
You can do it in two ways, either way, you need a primary key (eg: id) in your items table which will hold a unique number for each of your items.
After that, you print your id along with everything else inside a button or inside an a tag. Like
<div class="col-md-5">
<h2 style="font-size:25px;"> <?=$items['title'];?></h2>
<img src="<?=$items['image'];?> "width='300' height='500'/>
<p style="font-size:20px;" class = "lprice">GBP <?= $items['price'];?></p>
<a class="btn btn-primary" href="add_to_cart.php?id=<?=$items['id'];?>">Add to Cart</a>
</div>
Now inside your add_to_cart.php file, you can receive the id of the products like this $product_id = $_GET['id'];
Once you have the id of the product, you can process the rest on the add_to_cart.php file and redirect the user back to the previous page with a success or error message.
This way, we passed the id using a URL parameter. The second way is to pass the id using a form. I just wanted to let you that this method exists. I hope this helps. If you have any doubt, feel free to ask in the comment section.

How to specify pressed links/buttons in a foreach loop

The following is a foreach loop that will show a group of buttons coming from DB.
#foreach($acc as $accs)
<div class="col-xs-1">
{{$accs->first_name}}
</div>
#endforeach
As I click on one of the buttons, I want to show a new view with all same buttons but ONLY the selected button highlighted (e.g. class="btn btn-warning"). If I add the below code in the new view, all buttons will be highlighted, not only the selected one.
Any idea how can this be done?
#foreach($acc as $accs)
<div class="col-xs-1">
{{$accs->first_name}}
</div>
#endforeach
You should add a new property for that button like $is_highlighed in the controller send to the view and in view make an if statement for finding this button
you have two way for this goal
add a new field in DB like is_highlighed (Boolean)
or
you can add a property in the controller
for example, you get a collection from DB by the model and have some values like id, text, color and etc
and you want to make highlighted blue button so in the controller, you check the color and if button color is the blue that adds an is_highlighed = true

How to pass a VueJS variable into a Laravel blade route

I'm looping over a JSON array in VueJS and outputting each item to the screen but I need to create a link/route to a resource controller with the ID being returned for each row like so:
<tr v-for="item in searchResults.group">
<td>
<button type="button" class="btn btn-info btn-sm">Edit</button>
So I've tried putting the variable into the route like so #{{ item.id }} but get the error:
syntax error, unexpected '{' (View: /application/resources/views/admin/edit.blade.php)
The way I have done it is not the correct way obviously but I can't find anything in the documentation to achieve this.
EDIT:
Further input on this. The route function requires a second parameter, in this case, the ID of the item to edit. In pure PHP/Blade I have this and it works:
<button type="button" class="btn btn-info btn-sm">Reduce</button>
For the dynamic search page, I need to somehow pass that second parameter into blade/php from a vuejs variable but I just can't work out how to do it.
You can try this using backtick.
Its works for me. I hope it's helpful for you.
<a :href=`{{ route('admin.edit', '') }}/${item.id}`>
You are mixing two concepts that won't go together like this.
The blade template is rendered server-side, whereas the parts of your vue.js related markup will be parsed on the client side (e.g. "browser").
Because of that, referencing a property of item within a blade expression will fail (as it does).
<a href="{{ route('admin.edit', #{{ item.id }} ) }}"
route(...) refers to an expression that is related to blade, an item is supposed to be a part of your vue.js app.
What you need to do is to dynamically create the link for editing the ressource within your vue.js app once you loop your searchResult.group.
You could achieve this by injecting a "route-template" for editing the ressource into your vue.js app and bind the href property to a vue.js method, like this:
<tr v-for="item in searchResults.group">
<a v-bind:href="getEditLink(item.id)">Edit</a>
....
The according method getEditLink(id) then assembles the actual link based on the given id of item and the provided "route"-template.
You can try by appending the id to the end like so:
{{ route('admin.edit') }}/#{{ item.id }}
I'm guessing you are following a REST URI guides so that would work for you.
Use Like this
Click

Pagination in Laravel 4 works for one page. but not working for another

I downloaded the starter kit for laravel using sentry from GitHub.
Everything is fine so far. Blog page has pagination. I am able to change the number of items per page as I like by changing the same pagination(x) value.
Now problem is, i created a new page manually by myself called "Search" which displays results for Keywords searched.
Here I added the same pagination, it shows the number of items per page as mentioned, page numbers, arrows. Everything is perfect, but when I click page 2 it shows a BLANK PAGE. The page source of blank page is also empty. The URL is perfect for page two. It generates correctly as
sitename.dev/search?page=2
Please guide me where I am going wrong. The blog page pagination still works fine. Problem occurs only in newly created SEARCH page pagination.
This is my Controller
public function postSearch()
{
$searchString = Input::get('searchInput');
$posts = Post::where('title', 'LIKE', '%' . $searchString . '%')->orderBy('created_at', 'DESC')->paginate(4);
if($searchString){
return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);
}
else{
return Redirect::route('home')->with('error', 'Please enter Search Term');
}
}
This is my Route
Route::any('/search', 'BlogController#postSearch');
Search form better using GET method, because it's make easy to bookmark/save the URL for later use. In Laravel 4's Pagination, there's a method to appends the query string. So, we can keep the search querystring in other pages.
Docs:
http://laravel.com/docs/pagination#appending-to-pagination-links
My code:
Search Form
<form method="get" action="{{{ URL::to('lib/search') }}}">
<input class="input-xxlarge" name="q" type="text" placeholder="Search...">
<div class="control-group">
<div class="controls">
<input type="submit" class="btn" id="submit" value="Submit" />
</div>
</div>
Routing
Route::get('lib/search', 'LibraryController#getSearch');
Controller
public function getSearch()
{
$search = Input::get('q');
$posts = $this->post->where('title', 'like', '%'.$search.'%')->paginate(10);
return View::make('site/libraries/list', compact('posts', 'search'));
}
View / Blade
At method to display pagination:
{{ $posts->appends(array('q' => $search))->links() }}
So we can keep the q=? querystring in other pages (ex: /lib/search?page=2&q=apple)
First of all
return View::make('frontend/search', compact('posts'))->with('success', 'Account successfully updated')->with('posts', $posts);
When doing compact you're already including the $posts var to be used as $posts in the view, exactly the same you'd do using with('posts', $posts). So this last can be removed. If you want to load more vars using compact just separe the variable names by commas.
You're paginating the item in the POST action of Search, when you click any of the pagination links or call page=2 you're not running the postSearch method, you're probably calling getSearch as you're not doing now a POST request.
Have a look to RESTful controllers
A few things to check.
The new route exists in the app/routes.php file.
You are returning a view file for the new route.
That view file has the pagination output. For example: <?php echo $articles->links(); ?>.
Ultimately, there appears to be some kind of error occurring, which is why the page is rendering blank. Trying turning php errors on in your dev env and see what happens after that.
appends solved my issue. Also usage of appends will enable our users to save/bookmark the URL for future use.
But one problem is, it is not appending First page of the result.

jQuery EasyUI tabs Ajax

I created some tabs using jQuery EasyUI tabs. The content of the tab is loaded by Ajax because I pass the href attribute to my tab like:
<div id="mytabs" class="easyui-tabs" style="width:500px;height:250px;">
<div title="Tab1" href="/app/controller/tab_content" style="padding:20px;">
tab1
</div>
<div title="Tab2" href="/app/controller/tab_content" style="padding:20px;">
tab2
</div>
<div title="Tab3" href="/app/controller/tab_content" style="padding:20px;">
tab3
</div>
</div>
As you can see above, every time a tab is selected, the action tab_content which contains the content to be displayed in the tab panel (/app/controller/tab_content is just for illustration) is called. All of this work fine, but what I'm trying to do is to make a variable available to the view tab_content.ctp when the url is called (WITHOUT passing it as a parameter in the url) like:
/app/controller/tab_content/my_variable:test
Is there any way that I can pass a variable to my view when the tab is clicked without passing it to the url of the tab content? In order words, I want my_variable to be available to the view tab_content.ctp when the tab is clicked.
Note: This is a cakephp application
Thank you
You might have solved the problem already, but I ran into a similar issue with EasyUI tabs and remote content and thought I might share it. What I ended up doing was
for the variable to be passed: declare it as a window.variable, i.e. by setting window.my_variable = my_variable
for the external tab content: call the variable via javascript in the external file - since a window.variable has global scope, it is also available in dynamically loaded content (see this answer)
Hope this is of help.
Edit jquery.easyui.min.js and search for:
$.fn.panel.defaults
Change method: "get" to method "post"

Categories