I'm successfully adopted this guide: https://laraget.com/blog/how-to-create-an-ajax-pagination-using-laravel to my needs, it works, but pagination won't work if i apply my custom filters.
Let's start with routes/web.php:
// products routes
Route::get('products', 'ProductController#index')->name('CatalogIndex');
// sorting products with custom filters
Route::post('products', 'ProductController#Filters');
Then we're going to ProductController.php:
public function index(Request $request)
{
$products = Product::orderBy('created_at', 'desc')->paginate(12);
if ($request->ajax()) {
$products_view = view('includes.products', compact('products'))->render();
return response()->json(compact('products_view'));
}
return view('catalog.index', compact('products', 'power', 'area', 'source'));
}
// filtering products
public function Filters(Request $request, Product $product)
{
if ($request->ajax()) {
// using query builder here
$prods = $product->newQuery();
// sorting by price
if ($request->has('order')) {
if ($request->input('order') == 'default') {
$prods->orderBy('created_at', 'desc');
} else {
$prods->orderBy('price', $request->input('order'));
}
}
// more filters omitted for readability
$products = $prods->paginate(12);
$products_view = view('includes.products', compact('products'))->render();
return response()->json(compact('products_view'));
}
return abort(403, 'Unauthorized action.');
}
Some javascript, that makes these filters work:
$('body').on('click', '.pagination a', function(e) {
e.preventDefault();
var url = $(this).attr('href');
getProducts(url);
window.history.pushState("", "", url);
});
function getProducts(url) {
$.ajax({
method: 'get',
url: url,
success: function(data) {
$('#products').html(data.products_view);
console.log(data);
},
error: function(jqxhr, status, exception) {
console.log('Error' + exception);
}
});
}
Now is the question, normal pagination with ajax works just fine, if i don't apply any filter, but if apply the filter (select all products where power source equal to something, etc), then when i click on a pagination link, it just leads to my index method of ProductController on 2nd page ignoring my filters, also url changes to "products?page=2", i expect the following behavior -> when you apply source filter == somevalue, then clicking on 2nd page, it would display 2nd page of all products with applied source filter, now it shows just index method with simple sorting by created_at column.
You need:
on the blade partial appends the adicional parameters on the paginator like this:
{!! $products->appends(['param1' => 'val1', 'param2'=>'val2'])->links() !!}
And on your method index() do that:
if ($request->ajax()) {
return $this->filters($request, $product);
}
Good luck.
Your http methods are mismatched. Try changing your route to a "get" instead of "post" and see if that fixes it
Ended using query strings and switched to GET method.
Related
I want to filter the data in my table. I want if I select the first box it will show the data accordingly and if I select the second box it will show the data accordingly but the problem is that if I select the first box here the data does not show then the second box has to be selected.
Here is my controller code
public function UpazilaWiseReportShow(Request $request){
$data = [];
$data['report_data'] = Report::distric()->status(1)
->where('upazila_id',$request->upazila_id)->where('fiscal_year', $request->fiscal_year)
->get();
return view('adc.reports.upazilla-wise-data', $data);
}
here is my view code
<script>
$(document).ready(function() {
$('#upazila_id').on('change', function() {
getFilterData();
});
$('#fiscal_year').on('change', function (e) {
getFilterData();
});
});
function getFilterData() {
$.ajax({
type: "GET",
data: {
upazila_id: $("[name=upazila_id]").val(),
fiscal_year: $("[name=fiscal_year]").val()
},
url: "{{url('adc/upazila-wise-report')}}",
success:function(data) {
$("#report_data_table").html(data);
}
});
}
</script>
You should check if your $request has your needed fields. So you have to check if your request has these fields. You can use when method for it.
With the code below, you can either select fiscal year or upazila_id or both.
$data['report_data'] = Report::distric()
->status(1)
->when(
isset($request->upazila_id),
function ($query) use ($request) {
$query->where('upazila_id', $request->upazila_id);
}
)
->when(
isset($request->fiscal_year),
function ($query) use ($request) {
$query->where('fiscal_year', $request->fiscal_year);
}
)
->get();
I'm Trying to Save The Product into The Database By Clicking On Add To Cart
But It's Not Adding I Also Use Ajax `
I Want To Add The Cart To DataBase And It's Not Adding.
This is The Error That I cant Add Any Product To The Cart Because Of It
message: "Call to undefined method App\User\Cart::where()", exception: "Error",…
enter image description here
Model Page.
class Cart extends Model
{
use HasFactory; I
protected $table = 'carts';
protected $fillable = [
'user_id',
'prod_id',
'prod_qty',
];
}
Controller page.
public function addToCart(Request $request)
{
$product_qty = $request->input('product_qty');
$product_id = $request->input ('product_id');
if(Auth::check())
{
$prod_check = Product::where('id',$product_id)->first();
if($prod_check)
{
if(Cart::where('prod_id',$product_id)->where('user_id',Auth::id())->exists())
{
return response()->json(['status' => $prod_check->pname." Already Added to cart"]);
}
else
{
$cartItem - new Cart();
$cartItem->user_id = Auth::id();
$cartItem->prod_qty = $product_qty;
$cartItem->save();
return response()->json(['status' => $prod_check->pname." Added to cart"]);
}
}
}
else{
return response()->json(['status' => "Login to Continue"]);
}
}
javascript page.
This Is MY First Time To Use Ajax And Sure That Every Thing Is Correct I Want Know Why Its Not Add
$('.addToCartBtn').click(function (e) {
e.preventDefault();
var product_id = $(this).closest('.product_data').find('.prod_id').val();
var product_qty = $(this).closest('.product_data').find('.qty-input').val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
method: "POST",
url: "/add-to-cart",
data: {
'product_id': product_id,
'product_qty': product_qty,
},
success: function (response) {
alert(response.status);
}
});
// alert(product_id);
// alert(product_qty);
// // alert ("test ") ;
});
Route:
Route::middleware(['auth'])->group(function () {
Route::post('/add-to-cart', [App\Http\Controllers\User\indexController::class, 'addToCart' ]);});
So why this error occurs, how can I fix it?`
This look like an error in importation like App\Models\Cart not like this?
verify if you had added use App\Models\Cart;
Using Laraver Inertia Vue
I use a vue with a paginated list of posts. For each post I only load a few column from the database such as title and author. Then I visit url to load the details of a chosen post in the list. I do so using visit url with the lazy loading functionality. After that I am ready to edit the post without reloading the full page. Once the post is updated I submit it and correctly save it into the database. After that I can return back to the page. Everything happens without any reloading on the list.
In order to be able to load the details on a specific post lazily, my on controller is like this.
class PostController extends Controller
{
public function Index($id = null)
{
$this->id = $id;
return Inertia::render('Posts/Index', [
'posts' => Post::select('id', 'title', 'created_at')
->addSelect([
'userfirstname' => User::select('firstname')->whereColumn('id', 'posts.user_id'),
'userlastname' => User::select('familyname')->whereColumn('id', 'posts.user_id')
])
->orderBy('created_at', 'DESC')
->paginate(10),
//lazily evaluated
'details' => function () {
if ($this->id) {
$post = Post::find($this->id);
} else {
$post = null;
}
return $post;
},
]);
}
public function Update(Request $request)
{
$request->validate([
'id'=>'required',
'abstract'=>'required',
//TODO :to be completed
]);
$post=Post::find($request->input('id'));
$post->abstract=$request->input('abstract');
$post->title=$request->input('title');
//TODO to be completed
$post->save();
return Redirect::back();
}
}
and the method I use to load page and details are these:
//visit this url to get the lazzy evaluation of post details
if (to_visit) {
this.$inertia
.visit(`/posts/${to_visit}`, {
method: "get",
data: {},
replace: false,
preserveState: true,
preserveScroll: true,
only: ["details"],
headers: {}
})
.then(to_visit => {
console.log("fetched " + this.details.title);
});
}
},
updatePost(form) {
console.log("form submitted");
this.$inertia.visit(`/post`, {
method: "put",
data: form,
replace: false,
preserveState: true,
preserveScroll: true,
only: [],
headers: {}
});
},
This works fine as long as the particular post I update is on the first page, but when it is on the any other paginated page on the list, post saving is ok but I don't return on the paginated page but always on the first one.
Would be happy to ear about a solution!
How can i render component that is sent from controller via an ajax request? For example i want to dynamically filter product using this method:
Load the index URL
Fetch the products based on the filter category or return all the products using ajax
My ajax Code:
$(document).ready(function () {
filterData();
// Filter data function
function filterData() {
// Initializing loader
$('#product-listing-row').html('<div id="loading" style="" ></div>');
var action = 'fetchData';
var subCategory = getFilter('sub-category');
/* LARAVEL META CSRF REQUIREMENT */
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
// Ajax Call
$.ajax({
url: "/shop/filter",
method: "POST",
data: {action: action, subCategory: subCategory},
success: function (data) {
$('#product-listing-row').html(data);
}
});
}
// Get Filter by class name function
function getFilter(className) {
var filter = [];
$('.' + className + ':checked').each(function () {
filter.push($(this).val());
});
//console.log(filter);
return filter;
}
$('.common-selector').click(function () {
filterData();
});
});
I am getting all the filters from ProductController
Instead of manually writing html in controller I want to return the specific component from the controller
ProductController:
public function productFilter() {
if (!request()->action) abort('500');
// Starting the query for products which are active
$products = Product::where('is_active', '1');
//dump(request()->subCategory);
/* Checking the filters */
// sub category exists
if (request()->subCategory) $products = $products->where('sub_category_id', request()->subCategory);
// Completing the query
$products = $products->orderBy('created_at', 'DESC')->paginate(15);
// Adding reviews and total review
$products = Product::setProductReviewTotalReviewsAttr($products);
foreach ($products as $product)
//view('components.shop-product', ['product' => $product])->render();
echo '<x-shop-product :product="$product"></x-shop-product>';
}
Instead of getting the components rendered, I am receiving the whole string echoed out. Is there any way i can just get the components rendered?
Thanks in advance
Actually now I found a way to do it myself
I applied the following to the ProductController
return View::make("components.shop-product")
->with("product", $product)
->render();
Updated Code:
public function productFilter() {
if (!request()->action) abort('500');
// Starting the query for products which are active
$products = Product::where('is_active', '1');
//dump(request()->subCategory);
/* Checking the filters */
// sub category exists
if (request()->subCategory) $products = $products->where('sub_category_id', request()->subCategory);
// Completing the query
$products = $products->orderBy('created_at', 'DESC')->paginate(15);
// Adding reviews and total review
$products = Product::setProductReviewTotalReviewsAttr($products);
$output = '';
foreach ($products as $product) {
$output .= View::make("components.shop-product")
->with("product", $product)
->render();
}
if (count($products) > 0)
echo $output;
else
echo '<div class="col">No Data</div>';
}
with laravel > 8 you can use \Blade::render directly in your controller to render even anonymouse components, here I'm rendering a table component with a lot of properties:
class componentController extends Controller {
public function index(){
$table = [
:tableid => "table"
:thead => ["id","name","job"]
:data => [
["1","marcoh","captain"],
["2","sanji","cook"]
],
tfoot => false
];
// Renders component table.blade.php
return \Blade::render('
<x-table
:tableid="$tableid"
:thead="$thead"
:data="$data"
tfoot
/>
', $table);
...
http://rimi-classified.com/ad-list/west-bengal/kolkata/electronics-and-technology
The above link has a filter in the left. I am trying to use ajax to get city from state. but as the ajax is triggered the entire query is changing.
SELECT * FROM (`ri_ad_post`)
WHERE `state_slug` = 'west-bengal'
AND `city_slug` = 'kolkata'
AND `cat_slug` = 'pages'
AND `expiry_date` > '2014-03-21'
ORDER BY `id` DESC
It is taking the controller name in the query (controller name is pages).
The actual query is:
SELECT *
FROM (`ri_ad_post`)
WHERE `state_slug` = 'west-bengal'
AND `city_slug` = 'kolkata'
AND `cat_slug` = 'electronics-and-technology'
AND `expiry_date` > '2014-03-21'
ORDER BY `id` DESC
// Controller
public function ad_list($state,$city,$category,$sub_cat=FALSE)
{
if($state===NULL || $city===NULL || $category===NULL)
{
redirect(base_url());
}
$data['ad_list'] = $this->home_model->get_adlist($state,$city,$category,$sub_cat);
$this->load->view('templates/header1', $data);
$this->load->view('templates/search', $data);
$this->load->view('ad-list', $data);
$this->load->view('templates/footer', $data);
}
public function get_cities()
{
$state_id = $this->input->post('state');
echo $this->city_model->get_cities($state_id);
}
// Model
public function get_adlist($state,$city,$category,$sub_cat=FALSE)
{
if ($sub_cat === FALSE)
{
$this->db->where('state_slug', $state);
$this->db->where('city_slug', $city);
$this->db->where('cat_slug', $category);
$this->db->where('expiry_date >', date("Y-m-d"));
$this->db->order_by('id', 'DESC');
$query = $this->db->get('ad_post');
}
$this->db->where('state_slug', $state);
$this->db->where('city_slug', $city);
$this->db->where('cat_slug', $category);
$this->db->where('sub_cat_slug', $sub_cat);
$this->db->where('expiry_date >', date("Y-m-d"));
$this->db->order_by('id', 'DESC');
$query = $this->db->get('ad_post');
return $query->result_array();
//echo $this->db->last_query();
}
//ajax
<script type="text/javascript">
$(document).ready(function () {
$('#state_id').change(function () {
var selState = $(this).val();
alert(selState);
console.log(selState);
$.ajax({
url: "pages/get_cities",
async: false,
type: "POST",
data : "state="+selState,
dataType: "html",
success: function(data) {
$('#city').html(data);
$("#location_id").html("<option value=''>--Select location--</option>");
}
})
});
});
</script>
Please help me how to solve this issue. Please check the url I have provided and try to select a state from the filter section the problem will be more clear.
In .js what is the value of selState ?
In your model, you should if() else() instead of just a if, because your query will get override.
Where is the get_cities function ? Can we see it ?
On your url, the problem is that your ajax url doesn't return a real ajax call but an entire HTML page which is "harder" to work with. Try to change it into json (for dataType's ajax()) You should only do in your php something like this :
in your controller :
public function get_cities()
{
$state = $this->input->post('state');
//Do the same for $cat
if (!$state) {
echo json_encode(array('error' => 'no state selected'));
return 0;
}
$get_cities = $this->model_something->getCitiesByStateName($state);
echo json_encode($get_cities);
}
You should definitely send with ajax the $cat info