I've followed this documnetation
(https://laravel.com/docs/8.x/pagination)
and made changes accordingly but still I am not able to paginate the required page.
This is my controller code:
public function showManageCourierAddress()
{
$viewdata = [];
$addressRepoObj = new \App\Repositories\Mongo\CourierAddressRepository();
$addressData = $addressRepoObj->getAddressList(0,(int)Session::get('organisation_id'));
$viewdata['addresslist'] = $addressData;
unset($addressData);
return view('frontend.admin.managecourieraddress', ['viewdata' => DB::table('viewdata')->paginate(15)]);
}
On the view laravel, I've added this :
{{ $viewdata->links() }}
Can anyone tell me where I'm going wrong? And what I should do?
DB::table('viewdata')->paginate(15)
returns LengthAwarePaginator instance, which implements these methods.
It is enought to call {{ $viewdata->links() }}, and it should return whole html with pagination buttons and links. You just write some styles for existing classes, or use methods as mentioned above to create your own html with your own structure.
If I did not understand your problem, please reply in comment.
Related
I have searched quite a bit but cannot get other solutions working for my error using pagination:
It errors on the line containing {{ $dishes->links }}
I am trying to paginate my dish.index page.
My DishController index function looks like so:
public function index()
{
$dishes = Dish::paginate(2);
return view('dishes.dish_index')->with('dishes', $dishes);
}
I have even attempted the method of following this gist however I don't think I understand enough about collections in order to remedy my issue.
I also attempted to use $dishes = Dish::all()->paginate(2); but that returns the same error.
I don't think it's directly related but I don't have a route setup with {page} either, currently I only have Route::resource('dish', DishController::class);. Seperate concern but possibly related?
Any help is appreciated!
Controller
public function index()
{
$dishes = Dish::paginate(2);
return view('dishes.dish_index', compact('dishes'));
}
View
{{ $dishes->links() }}
Within EasyAdmin for Symfony you can use the AdminUrlGenerator for easily generating URLs to for example EasyAdmin CRUD Controllers.
Documentation here: https://symfony.com/doc/current/EasyAdminBundle/crud.html#generating-admin-urls
In my case i want to generate an URL to CRUD controller which is also linked within the Dashboard. If i create a link to a CRUD controller, the link works, but the corresponding MenuItem is not highlighted.
I found out, that EasyAdmin highlights the MenuItem with an URL parameter calling menuIndex. I can unset the menuIndex during Link Generation, but then no menuItem is highlightd in the menu.
I havn't found any information on how to get the correct menuIndex for a generated CRUD URL.
So how can i generate Admin URLs with correct menuIndex?
I want to give one possible answer. But to be honest, i don't really like my solution. Maybe someone has a better solution.
So, i am iterating over the configured menuItems within the DashboardController and trying to determine the menuIndex for a given Entity.
The code lookes like this:
private function getIndexLinkForCrudController(string $controller): string
{
return $this->adminUrlGenerator
->unsetAll()
->setController($controller)
->setAction(Action::INDEX)
->set('menuIndex', $this->determineMenuIndexForEntity($controller::getEntityFqcn()))
->generateUrl();
}
private function determineMenuIndexForEntity(string $entity): ?int
{
$menuItems = $this->configureMenuItems();
foreach ($menuItems as $id => $menuItem) {
/* #var MenuItemInterface $menuItem */
$routeParameter = $menuItem->getAsDto()->getRouteParameters();
if (
is_array($routeParameter) &&
array_key_exists(EA::ENTITY_FQCN, $routeParameter) &&
$routeParameter[EA::ENTITY_FQCN] == $entity
) {
return $id;
}
}
return null;
}
This code works for me. But this code only works within the DashboardController. If i want to create Admin URLs within a CRUD controller i need to move the menu config to a static method and accessing it there.
Also i am not fetching the error case when i cannot determine the menuIndex and returning null. For my case it's fine.
Maybe this is helpfull for someone.
If somebody has better solution, i would be happy to here about it.
Goodevening,
I have an overview page where I display all my project items.
I have a model (Project.php) a controller (ProjectController) where I send the variable $project (this includes all the information for each project) to the specific view.
Note: Each project has a own row in the database. (Quit obvious I guess)
Now I also have a table 'tasks' related to a specific project. In my view I wanna display how much of the total tasks are 'done'. (This is doing with the column 'done' (true/false, boolean)).
Now because I have a foreach function in my view (to display each individuele project) I can make the function in the view. But the Laravel framework is there for reasons. And writing out a lot of php in a view isn't the right way.
But, where should I make this function? And how can I use it in my foreach (in the view). Ofcourse I can make a new foreach in my model or controller and send that variable to my view. But then I can't use that one in my view-foreach as things will get mixed up.
I don't know how/where I can set up a function like this on a clean way.
Kinds regards,
Dylan
You can create a function in app directory(forexample app/Helpers/), or wherever you want and name it yourHelperFunction.php.
After creating that file, Laravel won’t recognize the file until it is registered inside composer.json file. Add files array inside autoload section.
.
.
.
"autoload": {
"files":[
"app/Helpers/yourHelperFunction.php"
]
}
.
.
.
then do composer dump autoload and you are ready to use the function inside the blade
Use Blade engine, not Core PHP loops.
It is wise to use blade engine then core php codes.
You can see the documentation
Laravel 5 Blade Template
Thanks for the help. I've figured it out (I guess?).
I have this function in my Project Model.
public function getTaskDonePercentage($id) {
$tasks = Task::where('project_id', $id)->count();
$tasks_done = Task::where([
['project_id', $id],
['status', 'done']
])->count();
if ($tasks_done > 0) {
$calc = ($tasks_done / $tasks) * 100;
}
else {
$calc = '100';
}
return round($calc) . '%';
}
Then in my view I just call the function in the foreach like:
#foreach
{{ $project_item->getTaskDonePercentage($project_item->id) }}
#endforeach
Tbh I still wanna know if this approach is a good one. (Or, if not, why it isnt?).
Thanks!
I'm trying to return search results to a new controller than where the search action was performed from. Problem is Results is never accessible from CustomSearchResultPage.ss. I've added inline comments for what I think is happening, am I right in my thinking here?
// Customise content with results
$response = $this->customise(array(
'Results' => $results ? $results->getResults() : '',
));
if ($results) {
$response = $response->customise($results);
}
// Use CustomSearchResultPage.ss template
$templates = array('CustomSearchResultPage', 'Page');
// Create a new CustomSearchResultPage page
$page = CustomSearchResultPage::get_one('CustomSearchResultPage');
// Build a controller using the CustomSearchResultPage
$controller = CustomSearchResultPage_Controller::create($page);
// Add the custom data to the newly minted controller
$result = $controller->customise($response);
// Return the controller and tell it how to render
return $result->renderWith($templates);
The page seems to render as expected just the variable is always empty...
Your explanation is a little hard to follow I'm afraid. So I'm answering for what I can ascertain as below:
Performing a search. This requires loading a controller to do as such.
Customising the current controller with the results
RE-customising the current controller with itself.
Setting the template for the current (double customised) controller.
Disregarding all of the above.
Fetching a random page (or an empty record).
Creating a controller for the empty page.
Customising the new controller with the customised controller of the current controller customised with itself.
Returning that page, which shows no results.
You need only stop at step 4 (skip step 3), and return the customisation ($response).
If there is some reason you think you need another controller however (which seems superflous, but who knows), creating and then customising that one (only) before returning it would be better.
Being that you have only used this second controller for rendering a result, the URL will not have changed or anything. The whole thing seems beyond requirements.
A much more simple way to render a result from this action would probably be:
return $this
->customise(['Results' => $results ? $results->getResults() : null])
->renderWith(['CustomSearchResultPage', 'Page']);
(from the top of my head, may need a little refining).
Have a controller function:
public function pageedit($id)
{
$page = Pages::where('pgeID','=',121)->get();
return view('admin.pageedit')->with('page',$page);
}
And a simple view:
#extends('admin')
#section('content')
#include('/partials/adminpageheader')
Edit Page {{ $page }}
#endsection
When I run this, I get the output as follows:
Intranet Web Control Panel (Pages)
Admin Home - Admin Home Page
Edit Page
[{"pgeID":"121","pgeActive":"0","pgeContent":"1","pgeMainLevel":"6","pgeLevel":"1","pgeParent":"6","pgeOrder":"3","pgeTitle":"Employee Assistance Program","pgeHeader":"Null","pgeBody":Employee Assistance Program that all employees can access 24\/7. The website, www.assist.com has an amazing amount of information available. If you are looking for training, articles, resources and more, check them out. The use","pgeNav":null,"pgeContents":"Null","pgeCallOut":"Null","pgeEditor":"Null","pgeTitleDRAFT":"Null","pgeTemplateDRAFT":null,"pgeNavDRAFT":null,"pgeContentsDRAFT":"Null","pgeCallOutDRAFT":"Null"}]
So I know it is pulling the data correctly. But when I try to display JUST the page title (pgeTitle) I get an undefined property error if I try {{ $page->pgeTitle }} and an undefined index error if I try {{ $page['pgeTitle'] }}
I know it has to be something simple I am forgetting, but I will be danged if I can figure out what it is. Thoughts?
Instead of using ->get() use ->first(). This will return a single instance as opposed to the collection, which is the difference between having to use an array accessor and not.
$page = Pages::where('pgeID','=',121)->first();
^
first==single
get==array collection
$page is an array, where the index is numeric as far as i can tell, so you should do:
{{$page[0]->pgeTitle}}
The same answer than Taxicala, but I'd suggest you to use ->first() method instead of get, since you're looking for only 1 row.
$page = Pages::where('pgeID','=',121)->first();