I am having this laravel 8 error Illuminate\Routing\Exceptions\UrlGenerationException - laravel

I have this button
<a class="btn btn-primary" href="{{route('generate-pdf',$data->id)}}">Export to PDF</a>
this is my route
Route::get('generate-pdf/{id}', [App\Http\Controllers\PDFController::class, 'generatePDF'])->name('generate-pdf');
this is my controller but when I var dump it does get the data and store it in $data variable. so my problem is why do i still get this error.
public function generatePDF($id)
{
$data = PersonalRecord::find($id);
var_dump($data);
$pdf = PDF::loadView('generate_pdf', $data)->setPaper('a4', 'landscape');
return $pdf->download('softcopy.pdf');
// return $pdf->stream();
}
and this is my pdf page (still sample not my final pdf design)
#extends('layouts.app')
#section('content')
Generate PDF
<h1>{{$data->name}}</h1>
<div>
<p>{{ $data->first_name }}}</p>
</div>
#endsection

Change your button link from:
<a class="btn btn-primary" href="{{route('generate-pdf',$data->id)}}">Export to PDF</a>
To
<a class="btn btn-primary" href="{{route('generate-pdf',['id' => $data->id])}}">Export to PDF</a>

Related

Individual ID's pdf print by Laravel8

I'm studying laravel using this gentleman's awesome code.
https://github.com/savanihd/Laravel-8-CRUD/blob/master/app/Http/Controllers/ProductController.php
I use Laravel Framework 8.48.2
I would like to add PDF pritabile view to each ID's "show" page
I could success PDF export using this simple test page.
public function generate_pdf() {
$pdf = \PDF::loadView('products.generate_pdf');
return $pdf->stream('yokoya_test.pdf');
}
so I had been trying to add PDF export to "show" function .
Here is my current code
Controller
public function each_print(Product $product)
{
$pdf = \PDF::loadView('products.each_print');
return $pdf->stream('each_print.pdf');
}
original index.blade.php code is this
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
I changed this as below
<a class="btn btn-info" href="{{ route('products.each_print',$product->id) }}">Print</a>
Here is my current WEB.php
Route::get('products/each_print', [ProductController::class, 'each_print']);
Route::resource('products', ProductController::class);
but I got this error
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [products.each_print] not defined.
C:\xampp0827\htdocs\lara\ic0630-simple\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php:427
Could you teach me right code please?
UPDATE
index.blade.php
https://jsfiddle.net/diessses/m72bs54r/1/
each_print.blade.php
https://jsfiddle.net/diessses/8Lg5wot3/
you need define the name to the route like:
Route::get('products/each_print/{product}', [ProductController::class, 'each_print'])->name('products.each_print');
UPDATED
first refactor your code in the index blade table like:
<td>
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">detail</a>
<a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">edit</a>
<a class="btn btn-info" href="{{ route('products.each_print',$product->id) }}">Print</a>
<form action="{{ route('products.destroy',$product->id) }}" method="POST">
#csrf
#method('DELETE')
<i class="fas fa-trash-alt"></i>
<button type="submit" class="btn btn-danger">×</button>
</form>
</td>
and in the each_print function pass the product instance to the view like:
public function each_print(Product $product)
{
$pdf = \PDF::loadView('products.each_print', ['product' => $product]);
return $pdf->stream('each_print.pdf');
}

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');
}

How to use pagination on ajax data

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.

How to limit my laravel posts just on home page

So I was wondering if I could limit the number of posts just for my homepage
Homepage
Here I check there are any posts and if so I display the latest one full width
#if(count($posts) > 0)
{{-- Get first post --}}
<div class="col-sm-12" style="padding: 0px;">
<a href="posts/{{$posts->first()->id}}">
<div class="post-icon">
<img style="width: 100%;" src="storage/cover_images/{{$posts->first()->cover_image }}">
</div>
<div class="bottom-left">
<button class="btn bg-dark text-light" type="submit">
<h5>
{{$posts->first()->title}};
</h5>
</button>
</div>
<div class="top-right-categorie">
<div class="categorie" style="background-color: {{$posts->first()->categorie['hex']}};">
{{$posts->first()->categorie['title']}}
</div>
</div>
</a>
</div>
<div class="w-100">
<br>
</div>
#endif
PostController
public function index()
{
$posts = Post::all();
return view('posts.index')->with('posts', $posts);
}
Page
You can easily find this in the Laravel's model documentation:
You can try:
$posts = Post::->take(2)->get();
return view('posts.index')->with('posts', $posts);
This will return the first 2 posts, replace "2" with the number of posts that you need
You can use:
$post = Post::all()->limit(5);
return view('posts.index')->with('posts', $posts);
Or use pagination:
$post = Post::all()->paginate(5);
return view('posts.index')->with('posts', $posts);

Default pagination fails in Laravel

I am creating an basic pagination with Laravel5.1 , I receive the following PHP code.
public function postFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
return view('users.findUsers')->with('users',$findUserByNombre);
}
This code returns the list of users correctly but in the view I don't know how to solve this error , I have the following code.
<div class="hotel-list listing-style3 hotel">
#foreach($users as $usuario)
<article class="box">
<figure class="col-sm-5 col-md-4">
<img width="270" height="160" alt="" src="{{$usuario->foto}}">
</figure>
<div class="details col-sm-7 col-md-8">
<a href="{{ URL::asset('detalle') }}">
<div>
<div>
<h4 class="box-title">{{$usuario->nombre}} {{$usuario->primer_apellido}} {{$usuario->segundo_apellido}}<small><i class="soap-icon-departure yellow-color"></i> {{$usuario->fecha_nacimiento}}</small></h4>
</div>
</div>
<div>
<p>{{$usuario->descripcion}}</p>
<div>
<a class="button btn-small full-width text-center" title="" href="detalle">CONSULTAR</a>
</div>
</div>
</a>
</div>
</article>
#endforeach
{{$users->render()}}
</div>
However , when I put this url in the page public/search?page=2 , this url return a typical error MethodNotAllowedHttpException in RouteCollection.php line 219:
Could anyone help to me ?
/UPDATE/
Route::get('/search-users', 'UserController#getUsers');
Route::post('/search', 'UserController#postFindUsers');
Get request
routes
Route::get('/search', 'UserController#getFindUsers');
controller
public function getFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
return view('users.findUsers')->with('users',$findUserByNombre);
}
HTML
{!! Form::input ('search' , 's' , null , ['class' => 'form-control'] , ['placeholder' => 'Search...']) !!}
You can return both $findUserByNombre and $links in the function postFindUsers()
public function postFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
$links = $findUserByNombre->render();
return view('users.findUsers', compact('findUserByNombre', 'links'));
}
And in the view findUsers.blade.php, just add
<div>{!! $links !!}</div>
And change the request method from post to get.
Route::get('/search', 'UserController#postFindUsers');
To get the 2nd page, just call the following url
public/search?page=2

Resources