Individual ID's pdf print by Laravel8 - laravel

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

Related

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

I'm using laravel 9.x
my route is
Route::middleware('verified')->group(function (){
Route::get('dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::resource('kullanicilar', UserController::class);
});
and my controller has destroy methods
public function destroy($id)
{
echo 'destroy'.$id;
//User::find($id)->delete();
//return redirect()->route('kullanicilar.index')
// ->with('success','Kullanıcı başarı ile silindi.');
}
and my user_index.blade.php
<form method="POST" aciton="{{ route('kullanicilar.destroy',$user->id) }}" style="display:inline">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></button>
</form>
even though everything seems to comply with the rules, I'm getting this error.
You have a typo in the action element causing the form to be posted back to the same route as the original page;
<form method="POST" aciton="{{ route('kullanicilar.destroy',$user->id) }}"
note action is misspelled
Also, as you are using resource controller, you should accept the model in the destroy method.
Use Route::list to check what your controller should accept
action NOT aciton in Your Form Ex :
<form method="POST" action="{{ route('kullanicilar.destroy',$user->id) }}"
style="display:inline">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></button>
</form>
I found the solution by overriding the destroy method with get on the web.php route. it's working for me for now.
such as
//this should be at the top
Route::get('kullanicilar/remove/{id}', [UserController::class,'destroy'])->name('kullanicilar.remove');
Route::resource('kullanicilar', UserController::class);
and change my user_index.blade.php
<i class="fa fa-times"></i>
it works.

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

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>

How to fix DELETE method not working in Laravel

My delete method is not deleting from the database and I can't seem to see what it is I'm missing in my code when I click on delete. I've provide my blade file, controller and wweb.php file below, any assistance will be highly appreciated.
Blade File
<div class="row max-inner">
#foreach ($tshirts as $tshirt)
#auth
<form action="{{ route('t-shirtsAdmin.destroy', $tshirt) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-lg">DELETE</button>
</form>
#endauth
<div class="columns col-6 collection-item">
<div class="row">
<a href="#">
<div class="slider">
#foreach (json_decode($tshirt->filenames) as $picture)
<div>
<img src="{{ asset('files/' . $picture) }}" alt="kids top" loading="lazy">
</div>
#endforeach
</div>
<div>
<div>
<div class="columns col"><i class="fas fa fa-child"></i> {{ $tshirt->gender }} top </div>
<div class="columns col"><i class="fas fa fa-calendar"></i> {{ $tshirt->age }} Years</div>
<div class="columns col"><i
class="fas fa fa-tag"></i>
Kshs 250</div>
</div>
<div class="row" style="display: flex; text-align: center; ">
<div class="columns col"><i class="fas fa fa-phone"></i> 0700 00000</div>
</div>
</div>
</a>
</div>
</div>
#endforeach
</div>
web.php
Route::resource('t-shirtsAdmin', 'App\Http\Controllers\TshirtController');
CONTROLLER
<?php
namespace App\Http\Controllers;
use App\Models\Tshirt;
use Illuminate\Http\Request;
class TshirtController extends Controller
{
public function destroy(Tshirt $tshirt)
{
$tshirt->delete();
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
}
ok , this problem because model binding , you can solve this by 2 way first :-
public function destroy($id)
{
Tshirt::find($id)->delete();
// or Tshirt::destroy($id);
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
second :- for route binding to work you should have type-hinted variable names match a route segment name, as the doc required : so your method will be like this :-
public function destroy(Tshirt $t-shirtsAdmin)
{
// $t-shirtsAdmin is the name of the router
$t-shirtsAdmin->delete();
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
you can find the route name var , when you run
php artisan route:list , you will find the var like this : t-shirtsAdmin/{t-shirtsAdmin} so you should use it in controller to bind it.
You cant just call the $tshirt param and expect it to delete, try something like this:
$id = $tshirt->id;
Tshirt::destroy($id);
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');

Laravel get route view without redirect

I am making a search in website.
public function search(Request $request)
{
$search_value = $request->search_txt;
$data_res = Data::Where('text', 'like', '%' . $search_value . '%')->get();
$navbar_search = Navbar::where('id','183')->first();
return redirect(route('response', $navbar_search->slug))->with('data_res',$data_res);
}
This is my controller function. I'm getting problem that i want to display data in the same page. I need to return view to this exact 'response' route and send slug. This redirect does not work, because $data_res after redirect is empty..
Routes:
Route::post('/search/search_res', 'SearchController#search')->name('search.srch');
Route::get('/{slug}', 'FrontEndPagesController#index')->name('response');
HTML:
<form class="form-horizontal" method="POST" action="{{ route('search.srch') }}">
{{ csrf_field() }}
<div class="main search_field">
<div class="input-group">
<input type="search" name="search_txt" class="form-control" value="{{ old('search_txt') }}" placeholder=" tekstas apie paieska ?" required maxlength="200" style="padding-left: 10px !important;">
<div class="input-group-append">
<button class="btn btn-secondary" type="submit">
<i class="fa fa-search"></i>
</button>
</div>
</div>
</div>
</form>
#if(!empty($data_res))
#foreach($data_res as $data)
{{ $data->id }}
#endforeach
#endif
With my little experience, I think when you redirect to another route and includes 'with', it stores the data in session.
you may want to access in the function FrontEndPagesController#index by
$data_res = session()->get('data_res');
then, to make it available for blade file, put it in return response like
return view('your response view', compact('data_res')

"Too few arguments to function App\Http\Controllers\Backend\BlogController::edit(), 0 passed and exactly 1 expected"

can anybody help me with this error?
Here my complete route
Route::resource('/blog/post', 'Backend\BlogController');
this is my edit function on BlogController
public function edit($id)
{
$post = Post::findOrFail($id);
return view("backend.blog.edit", compact('post'));
}
and this my button
<a href="{{ route('post.edit', $post->id) }}" class="btn btn-xs btn-default">
<i class="fa fa-edit"></i>
</a>
While generating a button, a correct way, according to the documentation is:
<a href="{{ route('post.edit', ['id' => $post->id]) }}" class="btn btn-xs btn-default">
<i class="fa fa-edit"></i>
</a>
Note passing an array of options as a second argument.

Resources