Laravel Livewire pagination with bootstrap links not displaying - laravel

I'm using Laravel Livewire with bootstrap 4. I'm trying to use pagination but the links aren't displaying at the bottom of the table. I don't think the issue is in my blade file or in the livewire component itself, as I can see in the response that there's no pagination links() method available by doing this:
$x = User::paginate(5);
dd($x);
Which yields this result, and contains no reference to links
In my livewire component I'm including the trait: use WithPagination;, and I'm also including the protected $paginationTheme = 'bootstrap'; line as well.
At the bottom of my div just after the table element I have included:
{{ $users->links() }}
Everything I'm doing seems to be "by the book" in terms of what the docs say, but the links just don't display. It's limiting the number of rows on the page as well, just no links.

On your Livewire component:
use Livewire\WithPagination;
Inside class use the trait:
use WithPagination;
Then set the pagination theme:
protected $paginationTheme = 'bootstrap';

Related

"next" page problem with Illuminate\Pagination\LengthAwarePaginator in the last page

I am trying to use Illuminate\Pagination\LengthAwarePaginator to paginate a Laravel Collection within a Livewire component in the following way:
public function render(): Factory|View|Application
{
$perPage = 3;
$items = $this->myCollection->forPage($this->page, $perPage);
$paginator = new Illuminate\Pagination\LengthAwarePaginator($items, $this->myCollection->count(), $perPage, $this->page);
return view('livewire.listing', [
'paginatedMyCollection' => $paginator,
]);
}
and in the listing.blade.php:
#foreach ($paginatedMyCollection as $element)
#livewire('row', ['element' => $element], key($element->id))
#endforeach
{{ $paginatedMyCollection->links() }}
It works fine(*)! for example:
except when the last page of the collection is reached. In this case, the «next page» button is erroneously constructed, resulting in the following presentation:
I have checked the Illuminate package and it seems that instead of using 'pagination.next' is taking this 'Showing.next' that does not work.
Can anyone help with this? Thanks and regards.
(*) The small buttons at the beginning and end of the page numbers could be filled with any character...
I think this is CSS issue.
Laravel Pagination is using tailwind per default.
If you are using Bootstrap, you can add Paginator::useBootstrapFive(); in app\Providers\AppServiceProvider boot() function
I am not sure how many libraries is supported, but you can always publish vendor view for pagination and edit it
php artisan vendor:publish --tag=laravel-pagination

How to disable personal teams in Laravel / Jetstream when using Inertia?

I followed an article here:
https://devlan.io/disabling-personal-teams-in-laravel-8-and-jetstream-1fd083593e08
Basically to disable personal teams in Laravel / Jetstream. Now the article gives the example of how to achieve this using Livewire but I am using Inertia. The Livewire template needs this to be changed from this:
#if (Laravel\Jetstream\Jetstream::hasTeamFeatures()
To this:
#if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->isMemberOfATeam())
How would I achieve this in Inertia? I have had a look around the web, but because I am new to this stack I am completely stumped.
Thanks in advance.
This is a conditional made in Blade templates. Since Inertia doesn't work with Blade, instead it uses some JavaScript framework (Vue, React, Svelte), what you need to do is to run this conditional somewhere in the Laravel side (Controller, Middleware) and set a variable that you'll pass your client side component to conditionally render the teams html stuff.
If you want to have this flag available to all the pages in your app, you can do this through the HadleInertiaRequests middleware.
class HandleInertiaRequests extends Middleware
{
public function share(Request $request)
{
return array_merge(parent::share($request), [
'has_teams' => Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->isMemberOfATeam()
]);
}
}
Then, your component, you can use that variable to render the teams part or not (Example using Vue):
<div v-if="$page.props.has_teams">
<!-- Teams... -->
</div>

Livewire & Laravel 8: updating global components

I’m fairly new to the components & livewire game so I’m getting very confusing when I need to update a component value from other sources. Let me explain:
I’m using a default Laravel 8 installation with Livewire - no JetStream.
My navigation file (the default one that comes with the installation) has 3 individual components containing: total of points achieved, total of lives and remaining lives.
Loads like:
<x-points>0</x-points>
<x-lifes>0</x-lifes>
<x-remaining-lifes>0</x-remainung-lifes>
My question: how do I update any of those components when I execute actions from different sources like:
user answer a question (file Answer.php)
User clicks on an action at the footer of my application (let’s call this Regenerate.php)
User request tips so I need to subtract (Tips.php)
I would change your three main blade components for livewire componentes:
<livewire:points></livewire:points>
<livewire:lifes></livewire:lifes>
<livewire:remaining-lifes></livewire:remaining-lifes>
So for example, let's create the points and answer components.
// Points.php livewire component
public int points = 0;
public $listeners = ['loadUserPoints'];
public function render() { ... }
public function loadUserPoints()
{
$this->points = user()->points()->sum('total');
}
// points.blade.php livewire component
<div wire:init="loadUserPoints">{{ $points }}</div>
Now let's abstract the answer component.
// answer livewire component (very abstracted)
public function save()
{
user()->answers()->create($this->data);
// this will be listened by the points component
// that will run its query and update your frontend
$this->emit('loadUserPoints');
}
So livewire works mainly for events, you have to use it to pass data cross multiple components. To update your frontend as an SPA you're not gonna use blade components, you have to use livewire component or a lot of javascript to handle the DOM.

Passing View Variable to Included Layout Template in Laravel 8

This is probably something simple, but it's doing my head in.
So, my layout blade template has this:
#include('layouts.partials.sidebar')
{{ $slot }}
#include('layouts.partials.footer')
#include('layouts.partials.scripts')
I create a view which loads a template. This I assume gets parsed in $slot.
return view('request', [
'boo' => 'Hoo'
]);
No problems, the page loads and the variable 'boo' is accessible as {{ $boo }} in the 'requests' template.
But my question is, how can I pass the 'boo' variable to an included file in the layout file? In this case the following:
#include('layouts.partials.scripts')
So, in 'layouts.partials.scripts' how can I access {{ $boo }}? At the moment I just get an undefined index error.
Thank you very much for the help.
#include('layouts.partials.scripts', ['boo' => 'Hoo'])
Laravel Docs
https://laravel.com/docs/8.x/blade#including-subviews
If you have a partial like a nav, header, or sidebar, part of the master layout from which you are composing other views. It requires data that doesn't change from one view to another, like navigation links. Then, instead of passing the data from each controller method, you can define a view composer in a service provider's boot() method:
Service Provider's boot method
public function boot()
{
View::composer('layouts.partials.sidebar', function ($view) {
//$links = get the data for links
return $view->with('links', $links);
});
}
Laravel Docs
https://laravel.com/docs/master/views#view-composers

Laravel 5.7 Pagination Customization

$users = Student::paginate(2);
I was trying to paginate some data with paginate function() but am want to show only data no links
You can customize the way pagination is displayed, first step:
php artisan vendor:publish --tag=laravel-pagination
This command publishes the pagination views under the resources/views/vendor directory.
Here you can modify files as you want, the bootstrap-4.blade.php file is the default view for pagination.
You can find more info in the laravel official documentation
Using the $students->links() is optional if yo have a custom blade file showing pagination. However, if you do not need detailed pagination, you can use simplePaginate
$students = Student::simplePaginate(2);
And then in blade file just loop through it :
#foreach($students as $student)
Show student details
#endforeach
If you want to show just next or prev page links then you can use $students->links(), otherwise do not use links at all.
it's simple you must create a function to display your students in your template, add this code in your controller :
public function index()
{
$students = Student::paginate(10);
return view('student.index', ['students ' => $students ]);
}
And on the bottom of your template for example views\student\index.blade.php add :
<div class="row">
<div class="col-md-12">
{{ $students->links() }}
</div>
</div>
if you want display additional pagination information use the following methods: :
Paginator Instance Methods

Resources