I am using Laravel 5.2 and package "barryvdh/laravel-dompdf". I am streaming/viewing the view file like this:
$manu = Mmanufacturer::select('manName')->find($manuid);
$prds = compact('prds','images','temp_header','temp_footer');
return PDF::loadView('cp.reports.letters-pdf', $prds)->stream();
This opens the appropriate view and is working fine... see image:
http://prntscr.com/dflj4d
At the end of url as you see '43' which is id by default..what I want to show at the end is manufacturer/product or something name instead of its id..Can we achieve this?
You can set custom routing for view pdf, send parameter by post method. Like:
Route::post('/manufacturer/product', 'ReportController#viewPdf');
If you use button then use this way:
View Pdf
Form For View PDF
<form id="viewPdf" action="{{ url('/manufacturer/product') }}" method="POST" style="display: none;">
<input type="hidden" name="product_id" value="43">
{{ csrf_field() }}
</form>
In Controller:
$manuid= $request->product_id;
$manu = Mmanufacturer::select('manName')->find($manuid);
$prds = compact('prds','images','temp_header','temp_footer');
return PDF::loadView('cp.reports.letters-pdf', $prds)->stream();
Hope this trick will help. Thank You :)
Related
Hey I am working on blog project and I have created a form to add post and I want to use my css with froms.In add post form there is a image field to upload image .
forms.py
class AddPostForm(ModelForm):
class Meta:
model = Post
fields = {'title_image'}
labels ={
'title_image':'Upload Main Image'
}
addpost.html
<form action= "#" method="post" enctype="multipart/form-data">
<div class="file-upload-wrapper" data-text="Select your file!">
{{ form.title_image }}
</div>
</form>
how to use my css classes I already tried widgets and got error
'title_image' : forms.ImageField(attrs ={'class':'form-control'})
did something like this but got error
Field.init() got an unexpected keyword argument 'attrs'
Thanks and any advice will be helpful.
i'am using laravel in my project , do i want to delete an appointement , but i get this error : The DELETE method is not supported for this route. Supported methods: GET, HEAD.
This is the controller :
public function destroy($id)
{
$rdv = DB::table('rdv')->where('id',$id)->delete();
return redirect()->back()->withSuccess('success delete !' ) ;
}
}
this is the form :
#if ( $getpat->Etat_de_rdv == 'en_attente')
<td><label class="badge badge-warning"> {{$getpat->Etat_de_rdv}} </label></td>
<form method="POST" action="{{ route('delete', $getpat->id) }}">
#method('DELETE')
#csrf
<button type="submit">Supprimer rendez-vous</button>
</form>
this is the web.php
Route::get('/delete', 'rendezv#destroy')->name('delete');
It should be
Route::delete('/delete/{id}', 'rendezv#destroy')->name('delete');
You're using Route::get(), but supplying #method('delete'); those are contradictory. Modify your route as follows:
Route::delete('delete', 'rendezv#destroy')->name('delete');
Additionally, you're not passing the $id parameter, so route('delete', $getpat->id) won't work. You can do this with a form field, or a URL parameter:
Route::delete('delete/{id}', 'rendezv#destroy')->name('delete');
the correct declaration of the route is:
Route::delete('/delete', 'rendezv#destroy')->name('delete');
I want to create update method and this is the code:
Route::get("/allProducts/edit/{id}","AllproductController#edit")->name('Allproduct.edit');
Route::post("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('allProducts.update' , [ 'id'=>$allproduct->id ]) }}">
{{ csrf_field()}}
public function update(Request $request, $id)
{
$data = Allproduct::find($id);
$data->name = $request->name;
$data->save();
return redirect(route('allProducts.index'));
}
when I click on submit button it shows me :
"The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE" error!
what is the problem?
Your route names do not match.
in routes:
name('Allproduct.update');
in the form:
allProducts.update
Also, you can always check the name of the routes thanks to the console command:
php artisan route:list
if you want use method PUT:
you can change method in routes:
Route::post to Route::put
and add next in form:
<input type="hidden" name="_method" value="PUT">
OR
#method('PUT')
this is if your laravel version is 6 and if your version another, check correct way to use PUT method in forms at laravel.com/docs/6.x/routing with your version.
As said here
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
So your form should look like this:
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('Allproduct.update' , [ 'id'=>$allproduct->id ]) }}">
#csrf
#method('PUT')
You had a typo in your route name and it was missing the method field.
Change your route to this:
Route::put("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
This will work, but
i strongly recommend you read this laravel naming conventions, and then change the name of your controller and model to AppProductController, AppProduct.
I am making a simple put request to my app backend using axios.put();
This all works, I have a button that is binded to vue like #click="submitForm"
However looking around I see that some people still wrap their input fields in forms like those:
<form method="POST" #submit.prevent="onSubmit" action="{{ route('someRoute') }}" id="form-submit">
{{ method_field('PUT') }}
{{ csrf_field() }}
Even if I dont use a form like the one above I get the same result when calling my ajax put request.
Laravel allready adds csfr headers to axios by default in resources/assets/js/bootstrap.js
So is there any reason I still should wrap my inputs in a form like above?
Thanks
Your ajax request doesn't matter if you do your inputs in form tags or not, because the request still sends and receives data from a server.
I would use a form tag because everybody can read the markup much easier and it could be usefull for writing less code in javascript - one example
<form action="" method="">
<div class="form-group">
<label class="control-label">Input</label>
<input type="text" name="input" />
</div>
</form>
$(document).ready(function() {
$('#some-form').on('submit', function() {
var data = $(this).serialize();
... do whatever you want (like ajax call) ...
return false;
});
);
You're using an Ajax request, in which case a standard "form submit" would get prevented. Putting a form around it is not obligatory, especially if you use a button element, which is not a classic form element anyway.
I'm using laravel and trying to delete something. Is it possible to specify the DELETE method on laravel's route()??
e.g
route('dashboard-delete-user', ['id' => $use->id, 'method'=> 'delete'])
or something like that??
EDIT:
What I meant was could I specify that in a link or a button in my blade template. Similar to this:
href="{{ route('dashboard-delete-user') }}
Yes, you can do this:
Route::delete($uri, $callback);
https://laravel.com/docs/master/routing#basic-routing
Update
If for some reason you want to use route only (without a controller), you can use closure, something like:
Route::get('delete-user/{id}', function ($id) {
App\User::destroy($id);
return 'User '.$id.' deleted';
});
No or at least I haven't figure out how to.
The only way for this to work out of the box would be to build a form to handle it. At the very minimum, you would need...
<form action="{{ route('dashboard-delete-user') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" value="submit">Submit</button>
</form>
Or you can just create the get route which you are trying to link to and have it handle the logic. It doesn't need to be a route which only respondes to delete requests to delete a resource.
Yes you can, using a URL helper. https://laravel.com/docs/5.2/helpers#urls
There are several options to choose from.