Livewire pagination links are missing the route - laravel

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

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>

how to pass one data view and others view page in laravel

i have two pages file in my view
first is my home blade then second is category blade and both of them using extends asidenav
my problem is when im redirecting to category file it gives me a error Undefined variable: categories but in my home file when im redirecting it's working fine . i will describe my code.
in my controller HomeController i pass one data categories to home.blade.php
class HomeController extends Controller
{
public function home()
{
$categories = Category::select('id','name')->get();
return view('home', compact('categories'));
}
public function category(){
return view('category');
}
}
the output of my home.blade.php
#extends('asidenav')
#section('section')
<main>
this is homepage
</main>
#endsection
you can see in my home blade there is a extends asidenav . inside of my asidenav extends the data categories i passed is inside of that asidenav to make a list of categories
the output of my extends asidenav
where the data i pass categories
<ul>
#foreach($categories as $category)
<li><span>{{ $category->name }}
#endforeach
</ul>
when i click the ahref to redirect the category blade it gives the error undefined variable categories. but when im redirecting to home blade it's working
the output of my category.blade.php
#extends('asidenav')
#section('section')
<main>
this is category
</main>
#endsection
but when i tried this code in my category controller it's working fine the error of undefined variable categories not showing
public function category()
{
$categories = Category::select('id','name')->get();
return view('category', compact('categories'));
}
my point here is i want to pass only single data categories in my home blade and others file blade like a props but i don't have a idea how to do that
You could create a service provider responsible for inyecting those variables without declaring them in your controller, in your boot method :
use Illuminate\Support\Facades\View;
View::composer(['asidenav', 'home'], function($view){
$categories = Category::all();
return $view->with('categories', $categories);
})

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

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,
]);
}

Laravel - include few views in one with controllers

Im working on Laravel and i have problem.
I created two controllers: PostController - has a view and PostController has a view.
I created next Controller called HomeController and i want to execute both Controller here PostController and MyProfileController.
I created a method in HomeController:
public function index()
{
$profile_view = app('App\Http\Controllers\MyProfileController')->index();
$post_view = app('App\Http\Controllers\PostController')->index();
return view('home',
[
'profile_view' => $profile_view,
'post_view' => $post_view
]
);
}
And im trying to show in view (home.blade.php)
#extends('layout')
#section('main-content')
Something
{!! $profile_view !!}
{!! $post_view !!}
#endsection
and it is viewing only one view from $post_view.
Anyone has a idea for this problem?
You need to create another view that would use ®include to do that and refer that view from your controller.
See https://laravel.com/docs/5.5/blade#including-sub-views

Store method not working using resource route

I am having trouble figuring out why my data is not being posted and stored in my database. I have used the resource routes for another form and it works fine, but here for some reason it won't work. Clicking submit just seems to refresh the page, no errors to work from!
So I have a form which gets the workout routines from a database, and on submission I want this to create a new Workout "session" in my database table (called "Workouts"). The form is this:
{{ Form::open(array('url' => '/')) }}
<div class="form-group">
{{ Form::text('workout_name', Input::old('workout_name'), array('class' => 'form-control', 'placeholder' => 'Session Name')) }}
</div>
<div class="form-group">
{{ Form::select('routines', $routine_names, null, array('class' => 'form-control')) }}
</div>
{{ Form::submit('Select Routine', array('class' => 'btn btn-success pull-right')) }}
{{ Form::close() }}
In my HomeController I have this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Routine;
use App\Workout;
class HomeController extends Controller
{
public function index()
{
$routines = Routine::all();
$routine_names = Routine::lists('routine_name');
return view('workout')->with(array('routines'=>$routines, 'routine_names'=>$routine_names));
}
public function store()
{
$workout = new Workout;
$workout->workout_name = Input::get('workout_name');
$workout->save();
}
}
I have a model created for the Workout, and the route for this page is the following:
Route::resource('/', 'HomeController');
I can't figure out where I'm going wrong. The index method in my controller is working, as it is returning the correct view with the data I need. The form also looks OK I think, as I'm posting to the same page, but submitting doesn't seem to carry out the code I have in the store method of the HomeController.
Any help would be appreciated!
Thanks :)
Change your route declaration from:
Route::resource('/', 'HomeController');
To something like this:
Route::resource('/workout', 'WorkoutController');
If you are using the resources controller creator command of php artisan then all the specific routes are created for you. To see all listed routes you can type , php artisan routes. This will show you RESTFUL routes even for your POST method .
And also even you did not created the resources controller and did made the routes with manual way then you can create ,
Route::POST('/workout' , SomeController#post);
I am trying to say , you have to use the different POST method for the form submission .
Hope this will solve your problem . Thanks.

Resources