Laravel Livewire Pagination Result Disappear After Clicking Next Page/Page Number - laravel

i've been searching for a question like the one above but i can't find one, so i'm gonna make one
My problem is, when i make a very simple pagination with Laravel Livewire, the first page appear just fine, but when i click "Next Page/Page Number", the result just disappear, even though there are still more results to show, i'm absolutely got no idea, no mattter how i try.
As you can see, the simple pagination work just fine, there is next and previous button and a very big next and previous picture below
But the moment i click next page or page number, the result just disappear, the total number of result even go to 0
Here is my Component code:
namespace App\Http\Livewire;
use Livewire\Component;
use Illuminate\Http\Request;
use DB;
use Livewire\WithPagination;
class Category extends Component
{
use WithPagination;
public function render(Request $request)
{
$id = $request->id;
$product_count = DB::table('product')->where('category_id', $id)->count();
$page = $request->page ?? 1;
$product_all = DB::table('product')->where('category_id', $id)->paginate(3);
return view('livewire.category',[
'product_all' => $product_all,
'product_count' => $product_count,
'page' => $page,
]);
}
}
Here is my view code:
<div class="row">
#foreach ($product_all as $product)
<div class="col-lg-4 col-md-6 col-12 mt-4 pt-2">
#php
product($product);
#endphp
</div>
#endforeach
<!-- PAGINATION START -->
#if ($product_all instanceof \Illuminate\Pagination\LengthAwarePaginator)
{{ $product_all->links() }}
#endif
<!-- PAGINATION END -->
</div>
And a small notice, i try pagination(just normal laravel pagination) without livewire and it work perfectly fine, that's why i am really clueless of what is happening
I have already include the livewire #livewireStyles and #livewireScripts, and i can't find an answer anywhere else cause i don't see any question that match my problem, and i'm kinda new to livewire

I think your problems is related with the request parameter on the render method of the livewire component, once livewire on rerender can't access the request value, until next browser refresh.
public function render(Request $request)
instead, use livewire properties to bind the id and page with values livewire can read on rerenders
#livewire('some-component', ['category_id' => $parent_category_id, 'page' => $parent_page_value])
Like above, livewire bind this values to self properties and read from them on rerender
public $category_id, $page;
public function render()
{
$product_count = DB::table('product')->where('category_id', $this->category_id)->count();
$product_all = DB::table('product')->where('category_id', $this->category_id)->paginate(3);
return view('livewire.category',[
'product_all' => $product_all,
'product_count' => $product_count,
'page' => $this->page,
]);
}

Related

livewire on rerender can't access the request value, until next browser refresh

My problem is, when i make a very simple pagination with Laravel Livewire, the first page appear just fine, but when i click "Next Page/Page Number", the result just disappear, even though there are still more results to show, i'm absolutely got no idea, no mattter how i try.
the same problem when i try delete one category
Here is my Component code:
<?php
namespace App\Http\Livewire\Admin\Category;
use App\Models\Categories;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Http\Request;
class Index extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $category_id;
public function deleteCategory($category_id){
$this->category_id=$category_id;
}
public function render(Request $request)
{
$categories = Categories::orderBy('id','ASC')->paginate(1);
return view('livewire.admin.category.index',['categories'=>$categories]);
}
public function destroyCategory(){
$category = Categories::find($this->category_id);
$old_image_patch = 'admins/images/categories/';
if (file_exists($old_image_patch.$category->image)){
unlink($old_image_patch.$category->image);
}
$category->delete();
session()->flash('message','xoa thanh cong');
$this->dispatchBrowserEvent('close-modal');
}
}
I have already include the livewire #livewireStyles and #livewireScripts, and i can't find an answer anywhere else cause i don't see any question that match my problem, and i'm kinda new to livewire
Make sure your view file has a base div tag.
<div>
// All the other view code here
</div>

Weird Livewire + Turbolinks behavior

I'm building an admin panel in TALL (Laravel 7) + Turbolinks. One section only includes a Livewire component that shows a paginated table of Product elements and a search field (the only "strange" thing that I'm doing before this is injecting a Market model instance to the request in a middleware).
The problem arises when I go to /products route where is the livewire component included nothing works... The records of the first page are fine, but pagination links are dead and search field does nothing, no console errors, no livewire errors, it's like javascript is not working at all, and here's the strangest thing: if I reload the page, the Market data I loaded in middleware is added to the query string and everything starts working as intended.
Middleware:
public function handle($request, Closure $next) {
$market = Market::findOrFail(session('selected_market'));
$request->request->add(['market' => $market]);
return $next($request);
}
Livewire Component:
class ProductsTable extends Component{
use WithPagination;
public $search = '';
protected $queryString = [
'search' => ['except' => ''],
'page' => ['except' => 1],
];
public function render(){
$products = Product::where('market_id', request('market')->id)
->when($this->search !== '', function($query) {
$query->where('name', 'like', "%{$this->search}%");
$query->orWhere('brand', 'like', "%{$this->search}%");
})->paginate(15);
return view('livewire.products-table', ['products' => $products]);
}
}
Livewire Component View:
<input wire:model.debounce.500ms="search" type="search" name="search" id="search">
<table>
#forelse($products as $product)
<tr onclick="Turbolinks.visit('{{ route('product', $product->id) }}')">
<td>{{ $product->name }}</td>
...
</tr>
#empty
<tr><td>Nothing to show for {{ $search }}</td></tr>
#endforelse
</table>
{{ $products->links() }}
I'm really confused and tired with this, I have no clue what is going on and similar questions haven't been answered clearly.
Thanks
The solution was as simple as adding this to your scripts:
document.addEventListener("turbolinks:load", function(event) {
window.livewire.restart();
});

Livewire pagination links are missing the route

I created a Livewire component that uses the WithPagination trait, on the render function I paginate the results and on the component's blade I print the paginator links, the paginator works however when I click any paginator link the page URL changes to the base_url/?page=x
The component class:
<?php
namespace App\Http\Livewire;
use App\Models\Order;
use Livewire\Component;
use Livewire\WithPagination;
class Table extends Component
{
use WithPagination;
public function render()
{
$orders = Order::latest()->paginate(10);
return view('livewire.table', compact(['orders']));
}
}
The component blade:
<div>
#foreach($orders as $order)
{{ $order->name }}
#endforeach
{{ $orders->links() }}
</div>
I tried the appends (like in regular Laravel paginator) and the withQueryString but when I click any pagination link the URL changes to the base URL only with the ?page=page_number, and for many reasons, I need to keep the original route.
Am I missing something?
Versions:
Laravel 8
Livewire 2

Laravel #include controller data

I'm trying to receive data on a sidebar that is included in the blade template but i'm not getting any data delivered. I've tried adding #include('admin.sidebar',['message_counter' => $message_counter]) and in the sidebar view show as {{$message_counter}}. I'm getting a Undefined variable: message_counter.
My router:
Route::get('/admin/sidebar', [
'uses' => 'MessagesController#counter',
'as' => 'admin.sidebar'
]);
My controller
use App\Message;
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar')->with('message_counter', $message_counter);
}
My View
<span class="menu-collapsed">Messages <span class="badge badge-pill badge-primary ml-2"> {{$message_counter}} </span></span>
What i ultimately intend to do is to show the amount of unread messages in the sidebar of the administrator backend, which is #includein every page.
It may be because i'm accessing two different controllers everytime I enter any page on the admin backend.
I've looked into Including Sub-Views but i'm probably missing something silly or not understanding some key concept, help is appreciated!
Thank you!
Note: I think this is inconvenient and unrecommendable. This is just to answer the question, you can scroll down to see other answers or approach.
Controller
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar');
}
View
#php
$message_counter = App\Message::where('status', 0)->get();
#endphp
Messages <span class="badge badge-pill badge-primary ml-2"> {{$message_counter}} </span></span>
You can try like this way
In Route:
Route::get('/admin/sidebar', 'MessagesController#counter');
In Controller
use App\Message;
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar', compact('message_counter));
}
And your view is ok.. Try this and if it is not working please let me know....
With a View Composer: add this to App\Providers\AppServiceProvider#boot()
View::composer('admin.sidebar', function ($view) {
$message_counter = Message::where('status', 0)->get();
$view->with([''message_counter' => $message_counter]);
});

New to Laravel - How to pass model data to a blade view?

Ok, I am totally re-writing this question, now that I am a bit more familiar with larval.
Here is my situation: I have a guitar lessons site based on larval 5.2.36, where each lesson belongs to a category, and within a lesson are several exercises. An exercise table does not have a category id as it is linked to a lesson which has a category.
Goal What I am trying to figure out is how to pass the category of the currently displayed lesson or exercise to a menu sidebar view that displays the categories, so that the category of the lesson or exercise is highlighted. For this, I need to understand how to do such a task in laravel.
From what I gathered, this is often done via controllers. However, there is no menu controller, but rather a menu composer. It contains a function
class MenuComposer
{
public function compose(View $view)
{
$minutes = 6 * 60;
$value = Cache::remember('menu-categories', $minutes, function() {
return \App\Category::with('parent')->with('children')->get();
});
$view->with('categories', $value);
}
}
Then in the menu blade file we have
#foreach ($categories as $category)
<?php $category = $category->present(); ?>
#if ($category->parent == null)
<li>{{ $category->title }}</li>
#foreach ($category->children as $child)
<?php $child = $child->present() ?>
<li class="level1">{{ $child->title }}</li>
<?php
/*
#foreach ($child->children as $grandChild)
<?php $grandChild = $grandChild->present() ?>
<li class="level2">{{ $grandChild->title }}</li>
#endforeach
*/
?>
#endforeach
#endif
#endforeach
So this is clear. I see that I can use the menu composer to pass additional data with a $view->with() call.
The question is how do I get the current category? For exercises and lessons, the routes don't have category data. They are of form
lesson/lessonid/lessontitle
or
exercise/exid/extitle
So I know I could do some sort of query of the model. But seems that wouldn't make sense, since I know there are other places in the process flow where the current cat is being passed. For instance, on an exercise page, the view is retrieving category as
$exercise->lesson->category->title
It is being passed this in exercise controller as
public function index($id, $name = null)
{
//$this->hit($id);
$exercise = $this->apiController->get($id);
$authorized = $this->isUserAuthorized();
return view('exercise/index', [
'exercise' => $exercise->present(),
'authorized' => $authorized,
]);
}
Similarly, a lesson controller passes $lesson object to lesson view as
public function index($id, $name = null)
{
//$this->hit($id);
$lesson = $this->apiController->get($id);
$subscribed = $this->request->user() && $this->request->user()->subscribed('premium');
return view('lesson/index', [
'lesson' => $lesson->present(),
'subscribed' => $subscribed,
]);
}
Based on above, seems I could modify the return statements in the lesson and exercise controller to pass the category to the menu view, but I don't see in the documentation how to do that, and I suspect the menu view is rendered before the lesson and exercise controller are called...
Also read about using service providers. middleware, etc, here: How to pass data to all views in Laravel 5?
But all these approaches seem overkill. I don't need every view to have the data. Seems to me, I need to do this somehow in the menu composer. But I don't know what method to use from the menu composer to retrieve the current lesson or exercise category. In the menu composer after debugging in phpstorm I see that the $view object for a lesson has $view->$data->$lesson->$entity.
So what I did was edited the menu composer to pass category to view:
$d=$view->getdata();
$s=array_key_exists ('lesson' , $d );
if ($s ==1) $attr = collect($d)->get('lesson');
$cat=$attr->cat();
This works since in the LessonPresenter I added function
public function cat()
{
$cat = $this->entity->category['attributes']['title'];
return $cat;
}
This works, but I feel like it is a hack. And I will have to do this for the Exercise Presenter as well. Being new to larval I suspect there has to be a more elegant way to do this. So can someone please explain how this should be done?
thanks,
Brian
You can use Facades of Laravel directly in blade templates.
Just use {! !} syntax to try and echo it. e.g: {!! Route::current() !!}
There are also similar functions of Route facade you can use.
Then, you can check your category with #if() ... #endif blocks and add something like class name within it.
Note: Don't put lots of logic in your blade files. Do it in your controller file (even in your other service classes) and pass simplest variables (e.g $isCurrentCategory) as an array to your template files using View::make() function's 2nd parameter.
Maybe this can help you
<a href="#" class="{{ (\Request::route()->getName() == 'routename') ? 'active' : '' }}">
You can also get the route prefix for example, you can check this out here:
Laravel API Docs Routing

Resources