Laravel with multiple custom paginations - laravel

I made a custom Laravel pagination but dashboard isn't with the same styling , So I wanna make another one.

Following the Laravel documentation :
If you would like to designate a different file as the default pagination view, you may invoke the paginator's defaultView and defaultSimpleView methods within the boot method of your App\Providers\AppServiceProvider class:
public function boot()
{
Paginator::defaultView('view-name');
Paginator::defaultSimpleView('view-name');
}
If you want to use Bootstrap style :
Paginator::useBootstrap();
You have all documentation here : https://laravel.com/docs/8.x/pagination#customizing-the-pagination-view

Related

How to make main menu public in all controllers

i have this code in controller
$Headercategory=Categories::where('status','1')->orderby('order')->get();
return view('front.home',['Headercategory'=>$Headercategory]
all my controlles will include "Headercategory" that mean i have to do same code with all controllers i have.
is there any way to make this code public in all project? without no need to add it in all controllers
As I understand, you want to pass categories variable in your all blade file. You can do that easily from /app/Providers/AppServiceProvider.php on boot method :
use View;
public function boot()
{
$Headercategory = Categories::where('status','1')->orderby('order')->get();
View::share(['Headercategory' => $Headercategory);
}
Now you can access $Headercategory anywhere from your blade

Define alias for blade directory in laravel

How i can define blade directory in liravel ?
I wanna to use:
return view('panel::post.create');
// e.g : resurces/views/vendor/panel/post/create.blade.php
How i should define vendor/panel as panel:: ?
You may do this using the service provider's loadViewsFrom method. The loadViewsFrom method accepts two arguments: the path to your view templates and your package's name. For example, if your package's name is panel, you would add the following to your service provider's boot method:
public function boot()
{
$this->loadViewsFrom(__DIR__.'/path/to/views', 'panel');
}
Package views are referenced using the package::view syntax convention. So, once your view path is registered in a service provider, you may load the admin view from the panel package like so:
Route::get('admin', function () {
return view('panel::admin');
});
Read more at official Laravel documentation.

Laravel - Share data with all views when the data is available

I'm writing a web app using Laravel 5.6. I need a list of all the connections the current session user have, in all the views.
I tried something like this
View::share('connections', Connection::getList(Auth::id()))
I put this code inside the boot function of AppServiceProvider. But the problem arises when the user isn't already logged in, because at that time Auth::id() is set to null.
The connection list is not generated when the user logs in. This throws the following error:
connections variable is not defined.
This target can achieve through different method,
1. Using BaseController
The way I like to set things up, I make a BaseController class that extends Laravel’s own Controller, and set up various global things there. All other controllers then extend from BaseController rather than Laravel’s Controller.
class BaseController extends Controller
{
public function __construct()
{
//its just a dummy data object.
$user = User::all();
// Sharing is caring
View::share('user', $user);
}
}
2. Using Filter
If you know for a fact that you want something set up for views on every request throughout the entire application, you can also do it via a filter that runs before the request — this is how I deal with the User object in Laravel.
App::before(function($request)
{
// Set up global user object for views
View::share('user', User::all());
});
OR
You can define your own filter
Route::filter('user-filter', function() {
View::share('user', User::all());
});
and call it through simple filter calling.
Update According to Version 5.*
3. Using View Composer
View Composer also help to bind specific data to view in different ways. You can directly bind variable to specific view or to all views. For Example you can create your own directory to store your view composer file according to requirement. and these view composer file through Service provide interact with view.
View composer method can use different way, First example can look alike:
You could create an App\Http\ViewComposers directory.
Service Provider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer("ViewName","App\Http\ViewComposers\TestViewComposer");
}
}
After that, add this provider to config/app.php under "providers" section.
TestViewComposer
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class TestViewComposer {
public function compose(View $view) {
$view->with('ViewComposerTestVariable', "Calling with View Composer Provider");
}
}
ViewName.blade.php
Here you are... {{$ViewComposerTestVariable}}
This method could help for only specific View. But if you want trigger ViewComposer to all views, we have to apply this single change to ServiceProvider.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
public function boot() {
view()->composer('*',"App\Http\ViewComposers\TestViewComposer");
}
}
Reference

How to share object in blade template across Zizaco/Confide routes?

I'm trying to share an object across a Laravel application. I need this because I want to create a blade template which will be included everywhere and will also perform some logic/data manipulation (a dynamic menu sort of speak).
To be able to accomplish this I've created a constructor in the Base controller and used View::share facade.
While this works across all routes in the application, it's not working for Zizaco/Confide generated routes, where I get Undefined variable error for $books.
This is the constructor in the base controller:
public function __construct()
{
$books = Book::all();
View::share('books', $books);
return View::make('adminMenu')->with('books', $books);
}
What you need are View Composers!!
You can hook a view composer to a certain view name or pattern (using * wildcard). Every time before that view gets rendered the view composer will run.
You can put this anywhere. Most elegant would be a custom app/composers.php which is then required at the bottom of app/start/global.php
View::composer('adminMenu', function($view){
$books = Book::all();
$view->with('books', $books);
}

How do you change the page name for Paginator?

Current Paginator is using ?page=N, but I want to use something else. How can I change so it's ?sida=N instead?
I've looked at Illuminate\Pagination\Environment and there is a method (setPageName()) there to change it (I assume), but how do you use it?
In the Paginator class there is a method the change the base url (setBaseUrl()) in the Environment class, but there is no method for setting a page name. Do I really need to extend the Paginator class just to be able to change the page name?
Just came across this same issue for 5.1 and you can pass the page name like this:
Post::paginate(1, ['*'], 'new-page-name');
Just like you said you can use the setPageName method:
Paginator::setPageName('sida');
You can place that in app/start/global.php.
Well that didnt work for me in laravel 5 , in laravel 5 you will need to do more extra work by overriding the PaginationServiceProvider because the queryName "page" was hardcoded in there , so first create your new PaginationServiceProvider in /app/providers ,This was mine
<?php
namespace App\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider as ServiceProvider;
class PaginationServiceProvider extends ServiceProvider {
//boot
public function boot()
{
Paginator::currentPageResolver(function()
{
return $this->app['request']->input('p');
});
}//end boot
public function register()
{
//
}
}
Then in your controllers you can do this
$users = User::where("status","=",1)
->paginate(5)
->setPageName("p");

Resources