How to use pagination on ajax data - ajax

I want to use default pagination in laravel in view fetched by ajax function. put I get an error.
ajax function
public function get_products($id){
$products = Product::where('category_id',$id)->where('is_hidden','0')->paginate(9);
$category = Category::find($id);
$returnHTML = view('products_ajax')->with('products', $products)->with('category', $category)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));
}
returned view by ajax
<h3>{{$category->name}}</h1>
<hr>
<div class="cards">
#foreach($products as $product)
<div class="card" data-item-id="{{$product->id}}">
<img style="width:50%;" src="{{asset('storages/images/products/'.$product->image)}}">
<div class="card-details">
<p class="card-brand">{{$product->brand->name}}</p>
<p class="card-name" title="Food Name Here Food Name Here Food Name Here Food Name Here">
{{$product->code}}
</p>
<p class="card-price" hidden> {{$product->price}}</p>
<p hidden class="card-full-des">
{{strip_tags(html_entity_decode($product->description))}}
</p>
<p class="card-packing">
<span>{{$product->packing}}</span>
</p>
{{-- <p class="card-packing">
<span>Packing: 12-8 oz (225g)</span>
</p> --}}
<div class="card-foot">
<button class="mbtn5" onclick="CardAddToCartOrDetails(this, true)">Add to Cart</button>
<button class="mbtn4" onclick="CardAddToCartOrDetails(this, false)">Details</button>
</div>
</div>
</div>
#endforeach
{{$products->links()}}
</div>
</div>
but I get this error:
so, can anyone help me?

The MethodNotAllowedHttpException is caused by a calling a route with a wrong method.
i.e. defining a GET route for /users and calling it with POST would result in this error.

Related

how to paginate read notifications in Laravel

I got an error related to the pagination of read notifications. Thank you for your help.
This is my controller code that gives this error when it runs
public function NotificationsRead(User $user)
{
$no = Auth::user()->readNotifications()->paginate(7)->latest()->get();
return view('user-profile.notifications-read', compact('user', 'no'));
}
And when I delete latest(), he gives an error to get()
I do not know how to paginate.
and A have two types notifications
You should do
$no = Auth::user()->readNotifications()->latest()->paginate(7);
In this way you are ordering descending before pagination.
Your code orders the result collection created by pagination.
https://laravel.com/docs/8.x/queries#latest-oldest
#jack this is my Blade
<div class="card-header text-black text-bold">
اعلان ها
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<div class="card">
<div class="card-body">
<h5 class="card-title">موضوع:</h5>
#foreach(auth()->user()->readnotifications->where('type','App\Notifications\NewReplySubmittedRoyal') as $notification)
<p class="card-text">{{$notification->data['name']}}
یک پاسخ برای پرسش شما با محتوای:
{{$notification->data['thread_title']}}
در {{$notification->data['time']}}<br>ثبت کرد.
</p>
<form>
<button type="submit" class=" btn btn-outline-success mb-2 ml-2">لینک پرسش</button>
</form>
<hr>
#endforeach
{{--*********** نوع دوم نوتیفیکیشن--}}
#foreach (auth()->user()->readNotifications->where('type','App\Notifications\FollowerNotifications') as $notification)
<p class="card-text">{{$notification->data['name']}} یک پرسش با محتوای:
{{$notification->data['thread_title']}}
ایجاد کرد.
</p>
لینک پرسش
<hr>
#endforeach
{{$no->render()}}
I have two types notifications

The POST method is not supported for this route. Supported methods: GET, HEAD."

How to pass the route in ajax in laravel without form?
I have a link or route without form.
<div class="row" id="markets">
#foreach($markets as $market)
<div class="col-lg-3">
<div class="card">
<img src="{{ asset('images/markets/'.$market->image) }}" class="card-img-top">
<div class="card-body">
<h5 class="card-title">{{ $market->title }}</h5>
<p class="card-text">{{ Str::words($market->body, 10) }}</p>
<p class="card-text">{{ $market->price }} $</p>
Add to cart
Continue
</div>
</div>
</div>
#endforeach
</div>
I want to do it with ajax.
<a role="button" class="card-text add-to-cart">Add to cart</a>
<script>
$(document).ready(function(){
$('.add-to-cart').on('click', function(event){
event.preventDefault();
var Route = $(this).attr('href');
$.ajax({
url: '{{ route("cart", $market->id) }}',
});
});
});
</script>
I see this error
My goal is to make this page.
try changing in web.php
Route::get('/cart/{market}',"MarketController#cart")->name('cart');
to
Route::match(['get', 'post'],'/cart/{market}',"MarketController#cart")->name('cart');
it seems you are using both get and post on same route if so then this may be it
EDIT:
try this
$('#markets').load('{{ route('cart') }} #markets')
What the above function does is it will load the cart route's market id and will replace the current route markets id
also i think it will be better to add
<div id="markets">
<div class="row">
#foreach($markets as $market)
<div class="col-lg-3">
<div class="card">
<img src="{{ asset('images/markets/'.$market->image) }}" class="card-img-top">
<div class="card-body">
<h5 class="card-title">{{ $market->title }}</h5>
<p class="card-text">{{ Str::words($market->body, 10) }}</p>
<p class="card-text">{{ $market->price }} $</p>
Add to cart
Continue
</div>
</div>
</div>
#endforeach
</div>
</div>
ie to create a div outside of the one you want to replace so the classes wont get mixed as for the for loop it will be only server sided so what you will load is loaded for-looped data

laravel error - Trying to get property of non-object

I'm receiving the Non object error (Trying to get property of non-object) when posting data to my summary view (blade). running dd($data); on the controller returns the payload data correctly (ie flyingFrom, flyingTo)
Form Payload
FlyingFrom
FlyingTo
Controller
$locations = MyLocations::all()->toJson();
$data = $request->all();
return view('summary', compact('data','locations'));
View
#foreach ($data as $locationData)
{{$locationData->flyingFrom}}
{{$locationData->flyingTo}}
#endforeach
Error
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
resources/views/planner_newsummary.blade.php:28
<div class="container">
<div class="col-12">
<h1 class="ml-2">Book this trip</h1>
<div class="row">
<div class="col-sm-6 my-4">
<div class="card inputcard h-100 progress-step is-complete">
<div class="card-header">
<i class="fas fa-plane-departure"></i> Flying from
</div>
<div style="position:relative">
<div id="flyingFrom" style="position:relative">{{$locationData->flyingFrom}}</div>
</div>
</div>
</div>
just access your data directly
$data = $request->all(); returns array
in your blade {{$data[flyingFrom]}}

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

How to include a sidebar in Laravel to show on all pages

I want to show a sidebar that displays data from the database across all my pages in Laravel, but I keep getting this error:
Undefined variable: products (View:
C:\xampp\htdocs\shop\resources\views\pages\sidebar.blade.php) (View:
Sidebar
#extends('layouts.app')
<nav class=" d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<form>
<div class=" input-group">
<input class="form-control" type="text" placeholder="Search...">
<button type="submit" class="btn btn-primary" value="search">Search</button>
</div>
</form>
#foreach($products as $product)
<span id="catName">{{ $product->name }}</span>
<h2>No category to show</h2>
#endforeach
</div>
</nav>
Controller
public function index()
{
$product = Product::get();
return view('pages.sidebar', ['products' => $product]);
}
Route
Route::resource('sidebar','SidebarController');
app.blade.php
<div class="col-md-4 col-lg-4 col-sm-12">
#include('pages.sidebar')
</div>
You can either do #include() or your can return it via a controller, you cannot do both. I think you might be a little mixed up with the structure of your project.
If you are using #include it isn't going to hit a controller. So in theory, you would need to include ['products' => $products] on every controller method.
Here is how I would do it:
sidebar.blade.php:
<nav class=" d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<form>
<div class=" input-group">
<input class="form-control" type="text" placeholder="Search...">
<button type="submit" class="btn btn-primary" value="search">Search</button>
</div>
</form>
#foreach($products as $product)
<span id="catName">{{ $product->name }}</span>
<h2>No category to show</h2>
#endforeach
</div>
</nav>
app.blade.php:
<div class="col-md-4 col-lg-4 col-sm-12">
#include('pages.sidebar')
</div>
Create a new file (or use an existing one) for a home page inside the pages folder, we will call it home.blade.php
home.blade.php:
#extends('layouts.app')
Change the view you are returning in the controller to new home.blade.php file.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
$product = Product::get();
return view('pages.home', ['products' => $product]);
}
}
I finally have to use view composer to solve this problem. Thank you

Resources