Button Call to controller function / method - php

I am using Laravel 7 and am stuck on trying to initiate a Controller method via a button in my view. I am using a form with a simple a tag but am not sure if how I am going about it is the best way. Besides, I am getting an error withe the way I am attempting it. The error is:
Too few arguments to function App\Http\Controllers\TasksController::finished(), 1 passed in C:\laragon\www\taskapp\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
I am truly stumped on how to call a method from a button click. What I am trying to do is (when the button is clicked) change the status of an item in my db called $task->task_status to "Complete".
In my table in the home view, I have a form like this:
<td>
<form method="POST" action="/tasks/{{$task->id}}">
{{ csrf_field() }}
{{ method_field('POST') }}
<div class="ml-5">
<i class="fa fa-check"></i>
</div>
</td>
In my web.php, I have:
Route::get('/finished/{id}', 'TasksController#finished')->name('finished');
And in my TasksController, I have the following:
public function finished(Request $request, $id) {
$task = Task::find($id);
dd($task);
$task->task_status = $request['task_status'] = 'Completed';
$task->save();
return redirect('home')->with('success', 'Task Completed and now viewable in Completed Tasks!');
}
If anyone can show me a better way of accomplishing this (without using axaj) I would really appreciate it.
Thank you in advance.

I ended up fixing this in using the following code:
in my web.php, I used:
Route::get('/finished/{id}', 'TasksController#finished');
in my home.blade.php, I have:
<td>
<form action="finished/{{$task->id}}" method="GET" style="display: inline" class="">
#csrf
#method('POST')
<div class="ml-5">
<button type="submit" class="btn btn-sm btn-success">
<i class="fa fa-check"></i>
</button>
</div>
</form>
</td>
And in my TasksController, I pretty much left that the way it was and it now works.

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>

i'm trying to create a search method by name but it showing this error

I'm very confused, I need your help, this is the error:
Missing required parameter for [Route: single.temp] [URI: singlepost/{name}] [Missing parameter: name]. (View: C:\Users\Toshiba\Desktop\working\mouhawla\resources\views\index.blade.php)
The search field:
<div class="search">
<form role="form" action="{{route('single.temp')}}">
<i class="fa fa-search"></i>
<div class="field-toggle">
<input type="text" name="name" class="search-form" autocomplete="off" placeholder="Search">
</div>
</form>
</div>
The method:
public function getPostByName($name) {
$products = DB::table('templates')
->where('name', $name)
->first();
return view('singlepost', compact('products'));
}
The route:
Route::get('/singlepost/{name}', 'App\http\controllers\TemplatesController#getPostByName')->name('single.temp');
The final view:
<h1 style="text-align: center;">ACH-template</h1>
<table>
<tr>
<td><img src="/storage/{{$products->image_path}}"></td>
<td><img src="/storage/{{$products->image_path2}}"></td>
<td><img src="/storage/{{$products->image_path3}}"></td>
<td>
<p>
<h2 style="text-align:center;">{{$products->name}}</h2>
</br>
<p>{{$products->description}}</p>
</p>
</td>
</tr>
</table>
<a href="/storage/{{$products->file_path}}" class="btn btn-primary">
<button class="btn" style="width:100%">
<i class="fa fa-download"></i> Download
</button>
</a>
The error is very explanatory. You are trying to use /singlepost/{name} route, but on your blade file, you are doing route('single.temp'), it is telling you that it needs the parameter name, else it cannot create the URL as it is a missing parameter.
You should have something like:
<form role="form" action="{{route('single.temp', ['name' => VALUE'])}}">
But that will not solve your problem, as you are trying to do a search, so you want something like /singlepost/John, and John is going to be input by the user on the input field. So you have to do an AJAX call because {{ route('single.temp') }} is going to be rendered by PHP and served to the user, so it is always going to miss the needed parameter.
What you can also do is get that value from the Request instead of a URL parameter.
You have defined a route which requires a parameter: {$name}. You have also used the route helper to generate a URL which takes the name of a route as the first argument and an array of parameters as an optional second argument.
When you have used route('single.temp') in your form action, you have not specified any parameters and so Laravel is throwing the error you're seeing. To resolve this error, you would need to specify a $name parameter as the second argument (i.e. route('single.temp', ['name' => 'something'])). This is not ideal though as if you're using $name as a search term, you don't know the value when the page is first rendered and so can't provide that value.
There are a few ways you could achieve your goal of searching records, a basic example of how you could do this follows.
web.php
Define two routes, the first to return a view with a form and another to process the form submission and show the results.
Route::get('/templates', [TemplateController::class, 'index'])
->name('templates.index');
Route::get('/templates/search', [TemplateController::class, 'search')
->name('templates.search');
TemplateController.php
Define the two functions which will be used when one of the routes defined above is requested.
class TemplateController extends Controller
{
// return a view
public function index()
{
return view('templates.search', ['templates' => []]);
}
// process the form submission
// perform a search for the $request search term
// return a view with the results
public function search(Request $request)
{
$request->validate([
'term' => ['required', 'string']
]);
$templates = Template::where('name', $request->term)->get();
return view('templates.search', ['templates' => $templates]);
}
}
templates/search.blade.php
{{-- create a form which will submit to the search route --}}
{{-- note I use GET rather than POST here, explained later --}}
<form action="{{ route('templates.search') }}" method="GET">
#csrf
<input type="text" id="term" name="term" />
<button type="submit">
{{ __('Search') }}
</button>
{{-- loop over and show results if there are any --}}
#forelse ($templates as $template)
{{ $template->name }}
#empty
{{ __('Empty') }}
#endforelse
</form>
The above should be self explanatory. My reason for using GET rather than POST in the search is because the value will be added to the URL as a query string parameter meaning it can be bookmarked or shared with ease.

Laravel missing required parameters on destroy route

I've just created a new project something like todo list in Laravel. When I try to do simple deleting I get this error:
Missing required parameters for [Route: destroy] [URI: {}]. (View: C:...\resources\views\index.blade.php)
Here is part of code from index.blade.php:
#if($todos)
<ol>
#foreach($todos as $todo)
<li>{{ $todo->todo }}</li>
<form action="" method="post">
#csrf
#method('Delete')
x
</form>
#endforeach
</ol>
#endif
so I am just checking if there is anything, if not, then don't show list.
Part of code from controller:
public function index()
{
$todos = Todo::all();
return view('index', ['todos' => $todos]);
}
public function destroy($id)
{
Todo::findOrFail($id)->delete();
}
and line of code from web.php:
Route::resource('/', 'TodosController');
This is so basic and it is making me crazy because I can't figure out what is causing this error. Seems like everything is good.
Im not 100% certain, but try updating your resource route declaration to:
Route::resource('todos', 'TodosController');
and changing your link to
x
In the docs here, https://laravel.com/docs/5.7/controllers#restful-naming-resource-route-parameters
They describe the routes as being created like:
Actions Handled By Resource Controller
DELETE /photos/{photo} destroy photos.destroy
You don't actually submit your form. You just click on a link, so you in essence make a GET request, and not a POST request.
You need to send it through a form.
#foreach($todos as $todo)
<li>{{ $todo->todo }}</li>
<form action="{{ route('destroy', $todo->id) }}" method="POST">
#csrf
#method('DELETE')
<input type="submit" class="btn btn-danger" value="x" />
</form>
#endforeach
Having an inline form like that can appear a bit weird, so you can instead put the button outside, and submit your form on click.
#foreach($todos as $todo)
<li>
{{ $todo->todo }}
<a href="#" class="btn btn-danger"
onclick="event.preventDefault();
document.getElementById('todo-destroy-{{ $todo->id }}').submit();">x</a>
</li>
<form action="{{ route('destroy', $todo->id) }}" method="POST" id="todo-destroy-{{ $todo->id }}">
#csrf
#method('DELETE')
</form>
#endforeach

Laravel Delete Method: MethodNotAllowedHttpException in RouteCollection.php line 233:

I tried to delete the cart from a list. When I tried to delete it, it shows an Error. Below here is my code:
Web.php
Route::post('cart/delete/{id}','ProductController#deleteCart');
blade.php
<a href="{{ url('/cart/delete',$row->id) }}" class="remove_item">
<i class="fa fa-times"></i>
</a>
<form action="{{ url('/cart/delete',$row->id)}}" method="POST" style="display: none;">
{!! Form::hidden('id',$row->id) !!}
</form>
Controller.php
public function deleteCart($id){
$cart = Cart::find($id);
$cart->destroy();
return Redirect::to('/shop-cart');
}
Simply change the following line of code:
Route::post('cart/delete/{id}','ProductController#deleteCart');
into:
Route::get('cart/delete/{id}','ProductController#deleteCart');
Reason for this error is sending a GET request to a POST route. In your code you are sending a GET request by calling a URL.
<a href="{{ url('/cart/delete',$row->id) }}" class="remove_item">
<i class="fa fa-times"></i>
</a>
Or otherwise if you want to keep the route as it is (as a POST route) just use the following code and make some adjustments accordingly:
<form action="{{ url('/cart/delete') }}" method="POST" style="display: none;">
{!! Form::hidden('id', $row->id) !!}
<input type="submit" value="Submit">
</form>
And it is better to modify the route as follows as the '/{id}' part is not needed as we are sending the id along with the POST request:
Route::post('cart/delete','ProductController#deleteCart');
Import Http\Request into your controller using:
use Illuminate\Http\Request;
And update your controller function as follows:
public function deleteCart(Request $request){
$cart = Cart::find($request['id']);
$cart->destroy();
return Redirect::to('/shop-cart');
}
But for this scenario GET route seems a good choice to avoid complexity.
Remove a href link, because it will send GET request, instead of POST. Add submit button to the form:
<form action="{{ url('/cart/delete/'.$row->id) }}" method="POST" style="display: none;">
{!! Form::hidden('id', $row->id) !!}
{!! Form::submit('delete') !!}
</form>
The error say that there is no route like the one you try to call. you have a route like this in your route file : /cart/delete/{id} where expects an id, and you call this from your form /cart/delete?id=theid
Fix
Change this from your submit url : /cart/delete',$row->id)
To this: /cart/delete/{{$row->id}}

Passing variable from button to controller Laravel

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

Categories