sort and filter product with query builder spatie - laravel

I have filter product that have sort and filter
sorting by price, created at , available product
But when I filter the product, I can no longer sort the same product filter
for example
http://127.0.0.1:8000/product?&filter[brand]=1
and I want sort product in brand based on created at but
When I click on the latest, the link does not continue the filter and only does the sort
http://127.0.0.1:8000/product?sort=created_at
I know there is a problem with my JavaScript, but I do not know how to solve it
<form method="GET" id="jobfilter-form" enctype="multipart/form-data" content="{{ csrf_token() }}">
<div class="col-sm-12 d-flex justify-content-center">
<div class="product-short">
<ul class="products-archive-tabs watch-page">
<p>order by ::</p>
<li>
<a id="createdAt" href="{{ URL::current()."?sort=created_at" }}">
The oldest
</a>
</li>
<li>
<a id="createdAt" href="{{ URL::current()."?sort=-created_at" }}">
the newest
</a>
</li>
</ul>
</div>
</div>
<div class="shop-box-area">
<div class="sidebar-categores-box shop-sidebar mb-10">
<div class="expander">
<div class="inner-bit">
<ul class="barnd-item">
#foreach ($brand as $brands)
<li>
<label class="m-checkbox">
<input
name="brand" type="checkbox" value="{{ $brands->id }}"
#if (in_array($brands->id, explode(',', request()-
>input('filter.brand'))))
checked
#endif
>
{{ $brands->name }}
<span class="count-brand">({{count($brands->product)}})
number
</span>
<img src="/photo/brandsmall/{{$brands->smallbarnd}}">
</label>
</li>
#endforeach
</ul>
</div>
</div>
</div>
</div>
java script
<script>
function getIds(checkboxName) {
let checkBoxes = document.getElementsByName(checkboxName);
let ids = Array.prototype.slice.call(checkBoxes)
.filter(ch => ch.checked == true).map(ch => ch.value);
return ids;
}
function filterResults() {
let brandIds = getIds("brand");
let createdAt = getIds("createdAt");
let href = 'product?';
if (createdAt.length) {
href += '&sort[created_at]=' + createdAt;
}
if (brandIds.length) {
href += '&filter[brand]=' + brandIds;
}
document.location.href = href;
}
$("#jobfilter-form #filter").on("click", e => {
filterResults();
});
</script>
$products = QueryBuilder::for(Product::class)
->allowedFilters([
AllowedFilter::exact('brand', 'brand_id'),
])
->allowedSorts('created_at')
->defaultSort('available_id')
->with('category')->paginate('5')
->appends(request()->query());
How can I keep the latest value in the link?

At First, you should not have duplicated ID value in your elements.
id="createdAt" is duplicated.
Second, you are using href for the sort function, whenever you click on it, the page will redirect to the URL of the href. so other things on the page will reset.
You should use a checkbox for sort items without the <a> tag, then when filter results are called, your sort items will be added to the URL without any page redirection.

Related

After Click Add to Cart Button then Button Change to Go to Cart in Laravel 8

Please help me, i'm right now try to change Add to Cart Button to Go to Cart after click, how possible I can do that?, please help me, I have trying but didn't work, here my condition code in Controller:
if (Temporary::where('course_id','=', $request->IDCourse)->exists()) {
Session::flash('failed-session','Go to card');
}
else
{
Session::flash('failed-session','Add to cart');
}
and below my blade for looping product on cart box
#foreach($carts as $cart)
<ul class="list-group list-group-flush">
<li class="list-group-item bg-light">
<div class="row">
<div class="col">
<input type="text" name="IDCourse" value="{{ $cart->course_id }}">
<a href="{{ route('course.detail', $cart->course->id) }}" class="text-body">
<div class="d-flex">
#if($cart->course->image == '')
<img src="{{ asset('storage/courses/Default_Photo.png') }}" alt="" class="avatar-md square" />
#else
<img src="{{ asset('storage/courses/'.$cart->course->image) }}" alt="" class="avatar-md square" />
#endif
<div class="ms-3">
<h5 class="fw-bold mb-1">
{{ $cart->course->title }}
</h5>
<span class="fs-6 text-muted">
<span>{{ __('Oleh ') }}{{ $cart->course->user->name }}</span>
</span>
</div>
</div>
</a>
</div>
</div>
</li>
</ul>
<hr class="my-0">
#endforeach
And the last code to display the result from condition in controller
#if(Session::has('failed-session'))
<div class="alert alert-danger">
{{Session::get('failed-session')}}
</div>
#endif
I try dd($request->course_id) and displaying null. My planning is to check if customer have add to cart that item then the button change to Go to Cart. Please help me.Thank u so much for any help.
simply just check this item in your controller then initialize the variable and check if variable is empty or not , then you can change the action of the form or change the src of the button ,
for example : if user->cart->count() > 0
redirect to the page
you can add another button for continue shopping as well too .

Laravel8 Property [Eng2] does not exist on this collection instance

I am facing a strange problem: I have 2 pages with similar structure relating to same DB, the first one displaying a grid of products working perfectly, the second the item detail giving me this error. I can get the all JSON string {{$wine}} on the page but no way to extract elements with {{$wine->Eng2}} or {{$wine['Eng2']}}
#extends('layouts.layout')
#include('partials.sidebar')
<div class="detail-container">
<img src="/img/9122705276958.png" alt="" />
<ul>
<!--<li>{{$wine}}</li>-->
<li>{{$wine->Eng2}}</li>
<li></li>
</ul>
<!--<div class="addCart">
<i class="fas fa-shopping-cart"></i>
</div>
<ul class="side-icons">
<span><i class="fas fa-search"></i></span>
<span><i class="far fa-heart"></i></span>
<span><i class="fas fa-sliders-h"></i></span>
</ul>
</div>
<div class="bottom">
<ul>
<li></li>
<li></li>
</ul>
<ul class="price">
<li>Retail price: Ntd </li>
<li>Member price: Ntd </li>
<li>VIP price down to: Ntd </li>
</ul>
</div> -->
</div>
public function detail($id) {
$wine =Wine::where('ART','=',$id)->get();
return view('detail', [
'wine' => $wine
]);
}
Route::get('wines', [WineController::class,'index']);
Route::get('/wines/{id}',[WineController::class,'query']);
Route::get('detail/{id}', [WineController::class,'detail']);
Try to use
$wine = Wine::where('ART','=',$id)->first();
instead of
$wine = Wine::where('ART','=',$id)->get();
first() will get you the first element in the collection but get() will get you the whole collection, so you can use first() if the collection has one element, or you have to use foreach in the view.

i want to hide html code when product is_featured column status all equal to zero using laravel

i am trying to hide html code by if condition when all product featured is zero, html code should not be shown in front page please help me how can i do that ?
controller
public function index()
{
$data = [
'products' => Product::with('productColorGallary')->where('is_featured', 1)->first(),
];
return view('home', $data);
}
html view
#if($products->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$products->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#endif
You are getting your products in your database with is_featured = 1. It means that your if condition in your blade will be always false.
Plus, are you trying to get all products or just one ?
If it's many, then :
Your controller
public function index()
{
$products = Product::with('productColorGallary')->get();
return view('home', compact('products');
}
and your blade
#foreach($products as $product)
#if($product->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$product->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#else
SOMETHING HERE IF PRODUCT IS FEATURED
#endif
SOMETHING HERE COMMONS TO BOTH FEATURED AND NOT FEATURED
#endforeach
Products is an array key, so you can use like $data['products'] etc..
But if you create a collection/object like the following should be work:
public function index()
{
// first will return the first matched item from db
// and you will get only is_featured = 1
$products = Product::with('productColorGallary')->where('is_featured', 1)->first();
return view('home', compact('products');
}

making pagination in laravel for image

Hello for everyone I have a problem in laravel that about the showing images in one view, the problem is: I have a page that just show the images but it show all the images stored in table with specific id but I want to show some of that and have a button (show more) and when click on button it show the other images I don't know hove can I do this if someone can help me please!
This is my controller code:
public function listHostel()
{
$hostels = Hostel::all();
return view('front/khabgah_list',compact('hostels'));
}
And this is my blade code:
#foreach($hostels as $hostel)
#foreach($hostel->attachments as $photo)
<div class=" col-12 col-sm-6 col-md-6 col-lg-3 px-1 mt-3">
<div class="card card-shadow custom-height-1 " style="border-radius: 0%">
<a href="">
<img src="/assets-/app/media/img/blog/hostels-img/{{$photo->file_name}}"
class="card-img-top card-img custom-card-img-height" alt="">
</a>
<div class="car-body">
<div class="card-footer">
<div class="custom-circle">
<p class="custom-circle-text card-text">
<b>
#if($hostel->type == 1)
{{ 'ذکور'}}
#else
{{ 'اناث' }}
#endif
</b>
</p>
</div>
<div class="custom-prices card-text text-left"> کرایه فی ماه
: {{$hostel->remark }}
</div>
</div>
</div>
</div>
</div>
#endforeach
#endforeach
Pagination is likely what you're looking for. Change your controller to:
public function listHostel()
{
$hostels = Hostel::all()->paginate(5);
return view('front/khabgah_list',compact('hostels'));
}
Then you can get the links in your Blade template:
{{ $hostels->links() }}
You might need to change it a bit to work for your specific situation. The documentation gives some more info: https://laravel.com/docs/5.8/pagination

Show a collection in laravel from controller on different columns in view

got this problem:
I want to show the info from a collection where a publication has a "featured" column in true.
$pub_destacadas = Publicaciones::take(3)->where('destacado', '=', 1)->get();
The problem is that my layout has 3 elements to fill with this information that have different sizes so i wasn't able to solve this using the chunk() method in the foreach in my blade template, so I tried the following:
$individual_destacada = $pub_destacadas->take(1);
$grupo_destacada = $pub_destacadas->take(2);
and show them in the view as follows:
<div class="publicaciones-destacadas">
<div class="container">
<h2>Publicaciones Destacadas</h2>
<div class="row">
#foreach($individual_destacada as $destacada)
<div class="col-md-6">
<div class="item-destacado size-2">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
#endforeach
<div class="col-md-6">
#foreach($grupo_destacada as $destacada)
<div class="row doble-publicacion">
<div class="col-md-12">
<div class="item-destacado size-1">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
So... now it shows it correctly in the layout but the first item that the query gets is shown 2 times in this layout. How can I exclude this first element in the second variable where I get the featured publications?
Hope I explained the problem correctly.
Here's the layout:
You can use splice.
$individual_destacada = $pub_destacadas->splice(1);
$grupo_destacada = $pub_destacadas->splice(2);

Resources