Passing variable from button to controller Laravel - php

I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.
result page
<form action="generatePDFpage" method="get">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</form>
routes.php
Route::get('/dashboard/result/generatePDFpage', 'resultController#GeneratePDFc');
GeneratePDFc controller
public function GeneratePDFc(){
$id_array_implode = "HALLO";
$pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');
return $pdf->stream('invoice.pdf');
}
So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!
Could someone please help me out? :-)

When you're using get method, you can do just this:
<a href="{{ route('route.name', $parameter) }}">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</a>
For other methods you can use something like this (this one is for DELETE method):
<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>
To get variable, use something like this:
public function generatePDF(Request $request)
{
$someVariable = $request->someVariable;

I don't know Laravel but I think when in your action="" of the form you can put your route with its parameters no ?
I've found it here : https://laravel.com/docs/4.2/html#opening-a-form
And access the variable in your controller using the $request var

Related

How to change the boolean in DB calling a function in href?

In a Laravel context, I've got this messages page, with all the messages belonging to a specific user. Initially all messages are not readed, so I put a button to change the boolean in DB (from 0 to 1) and finally show the message.
I'm doing this:
The view
#if ($message->readed != 0)
<p class="card-text message text-left">{{ $message->message }}</p>
#else
<form method="POST" action="/message/read">
#csrf
#method('PATCH')
<input type="hidden" name="message" value="{{ $message->id }}"/>
<button class="btn btn-info text-white" type="submit">
Leggi
</button>
</form>
#endif
The route in web.php
Route::patch('message/read', 'MusicianController#readMessage');
The function
public function readMessage(Request $request)
{
$message = Message::where('id', $request->id)->first();
$message->readed = 1;
$message->update();
return redirect()->back()->with('message', 'message updated');
}
But it's not working, as soon as I click the button to show the message (and even change the DB value) I've got this error: The PATCH method is not supported for this route. Supported methods: GET, HEAD.
Even if I had specified a patch method in routes and even in the form with #method('PATCH')
Could someone help me understand what's wrong please??
the main answer
your route is:
Route::patch('message/read', 'MusicianController#readMessage');
replace your route with following route that use for all CRUD opration:
Route::resource('message/read', 'MusicianController');
if you use ajax for submit data then replace your type and url with following:
type: "patch",
url: "{{url('message/read')}}",
if you don't use ajax than use following:
<form method="POST" action="{{url('message/read"')}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
</form>
update: after version 5.6 you can use these syntax for above functions in any blade file:
<form method="POST" action="{{url('message/read"')}}">
#csrf
#method('PATCH')
</form>

Resource route call to wrong method

I have created route with "resource". When I try to use delete method it always going to show method.
Route list
Route call
<a class="btn btn-danger" href="{{ route('languages.destroy', ['language' => $language->id]) }}">Delete</a>
Delete method
public function destroy($language){
$lang = Language::findOrFail($language);
$lang->delete();
session()->flash('flash_message', 'The language has been
removed!');
return redirect(route('languages.index'));
}
So how to fix it?
Thank you!
Since it goes to GET method because you are not deleting using form .
route('languages.destroy',['language' => $language->id])
the above route only generate url .so if you are using
delete
then it treat as get method.So you have to use
<form method="POST" action="{{ route('languages.destroy',['language' => $language->id]) }}">
#csrf
#method("delete")
<button type="submit">Delete</button>
</form>
In your blade:
<form action="{{ route('languages.destroy',$language->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>

The POST method is not supported for this route()

I have this basic error but i cant fix it... can I have some help please ?
This is my view, and I tried with tokens #csrf and also #csrf-field and token.
I tried to write Post, post, POST.
(prat.store work well, the problem is update.)
#if(isset($ModificationMode))
<form method="post" action="{{route('prat.update', $DataPraticien ?? '')}}">
#csrf
#else
<form action="{{route('prat.store')}}" method="post">
#endif
//stuff
//stuff
/lalala
#if(isset($ModificationMode))
<button type="submit" class="btn btn-warning">Modifier Praticien</button>
#else
<button type="submit" class="btn btn-success">Ajouter Praticien</button>
#endif
my controller :
public function update(Request $request, $id)
{
$ModifPrat= Praticien::find($id);
$ModifPrat->NOM = $request->input('NOM');
$ModifPrat->ETAT_CIVIL = $request->input('ETAT_CIVIL');
$ModifPrat->NOTE = $request->input('NOTE');
$ModifPrat->NOTORIETE = $request->input('NOTORIETE');
$ModifPrat->MENBRE_ASSOCIATION = $request->input('MEMBRE_ASSOCIATION');
$ModifPrat->DIPLOME = $request->input('DIPLOME');
$ModifPrat->save();
return redirect()->route('homeAdmin', auth()->id());
}
My route is a basic resource :
Route::resource('prat', 'PratController');
NB : The variable ModificationMode is a way to use the same Page for two distinct task. I used var_dump to debug it and the variable is set well and my prat.update is detected.
Thanks ;)
If you're modifying, use this
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
you can refer to Form Method Spoofing or Resource Controllers
Check out the documentation for ResourceControllers. There is a table that explains how Laravel maps the controller methods to the request types. Basically, you need to use the #method directive (or manually add the hidden input). So, your form should look like:
<form action="{{ route('prat.update') }}" method="POST">
#method('PUT')
//...
</form>

Laravel: How to store href id in the new variable and display or use in the input field?

I want To Store the current user $emp->id from href , and use in the input value as like in my code that is written below. Is is possible? or if possible then please help me? and if this Question is not a big problem so i am sorry for that in advance.
<a href="{{'/employee'}}?id={{$emp->id}}" type="button" name="user_id" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Apply Attribute
</a>
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{store here current user}}">
</form>
Seems you just need paste your current user inside INPUT:
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{ $emp->id }}">
</form>
.....
If you want to use an id from a url you can :
Route::get('/url/{emp}', 'YourController#method');
In your controller:
public function method(Employee $emp) {
//Your code
return view('youre.view', compact('emp'))
}
Now in your view you have $emp and can acces id like $emp->id.
Offcourse name it what you want but be sure to name it the same in your route,controller and view.
Now you don't need a forEach loop, because of the binding you already have the employee from the url
p.s: Employee model is just an assumption..name it whatever your model is called.
You can use route function in blade for this.
Try this on web.php :
Route::get('/employee/{id}', 'YourController#YourMethod')->name('routename');
on your Controller your method need to have an arguments
public function YourMethod($id){
// Code here
}
And on your blade make your href with route
Link
Href Like That:
<a href="{{$emp->id}}" type="button" id="uu_id" class="btn btn-primary uu"
data-toggle="modal" data-target="#myModal"> Apply Attribute </a>
Using This Script To get Value from Href:
<script type="text/javascript">
$(document).ready(function() {
$(".uu").click(function(event) {
var u_id = $(this).attr('href');
event.preventDefault();
document.getElementById("hiddenVal").value = u_id;
});
});
</script>
And in your Form like this:
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="submit" style="margin-bottom: 10px;" class="btn btn-success
pull-right" name="apply" value="Apply"/>
<input type="hidden" name="hiddenVal" id="hiddenVal" />
</form>
And Last How to get this Value in the Controller And save to Database:
public function store(Request $request)
{
$rates = new Rating;
$user_id = $_POST['hiddenVal'];
$rates->user_id = $user_id;
$rates->save();
return redirect('/employee');
}

Laravel set route issue for post method

This is my form:
<form class="form-horizontal" method="POST" action="{{ url('/categories/new') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input class="btn btn-default" value="Cancel" type="reset">
</form>
This is my url where my form is located: /categories/new
This is my route:
Route::get('/categories/new', 'Admin\CategoriesController#newCategory');
I want to keep the new method, so i want to check if there is a post method do smth else load the view with my form. How can I achieve this in laravel 5. I'm a newbie so all of the detailed explanations are welcomed. Thank you !
If you want to use single method for both POST and GET requests, you can use match or any, for example:
Route::match(['get', 'post'], '/', 'someController#someMethod');
To detect what request is used:
$method = $request->method();
if ($request->isMethod('post')) {
https://laravel.com/docs/master/requests#request-path-and-method
Add this to your rotes file:
Route::post('/categories/new', 'Admin\CategoriesController#someOtherFunctionHere');

Categories