Please help me, im newbie in laravel and get an error
when I will update data field from button, like this
MethodNotAllowedHttpException in RouteCollection.php line 218:
and im using Routes:
Route::post('/verify', 'DataPelamarController#verify');
and I write my Controller like bellow:
public function verify(Request $request, $id){
DB::table('databeasiswas')->update([
'status_pendaftaran'=>'1']);
return redirect(url('pendaftar'));
}
and this is my view
<a href ="{{ url('verify') }}" name="_method" type="hidden" value="PATCH" >
<button type="submit" class="btn btn-vk btn-sm"><i class="fa fa-check"></i> Verify Data</button>
</a>
thanks before..
Just change this line. Patch is for updating, Post for new db entries. This is just a convention. You can use post for both.
Route::patch('/verify', 'DataPelamarController#verify');
Related
I have a project of an online forum with Laravel 8, and in this project, I have a "Like" button at Blade like this:
<form action="{{ route('questions.likes', $show->id) }}" method="POST">
#csrf
<button class="btn">
<i class="fas fa-thumbs-up"></i>
</button>
</form>
$show->id is the question id
And here is the route questions.likes:
Route::post('questions/{id}/thumbsUp' , [LikeController::class, 'store'])->name('questions.likes');
And then I called the Controller LikeController with the store method which goes like this:
public function store(Request $request, Question $id)
{
dd('output')
}
But now the problem is it does not send data to this method, I mean when you click the button nothing happens at all!
So what is going wrong here?
If you would like to take a look at all the routes in the web.php file, here it is.
You will have to change store(Request $request, Question $id) to store(Request $request, Question $question). Laravel will automatically resolve the provide $show->id argument to the Question model if you use the following route:
Route::post('questions/{question}/thumbsUp' , [LikeController::class, 'store'])->name('questions.likes');
<button type="submit" value="Submit">Submit</button>
Set a type for your button, check if it works or not
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.
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}}
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
Code in my Route file look like this :
Route::delete('/subtask1/delete/{{subtask}}', 'TaskController#subtaskdestroy');
Route::get('/home', 'HomeController#index');
Route::get('/redirect/{provider}', 'SocialAuthController#redirect');
Route::get('/callback/{provider}', 'SocialAuthController#callback');
});
Code in view file:
<form action="/subtask1/delete/{{1}}" method="POST" style="display: inline-block;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" id="delete-task-{{$subtask->id }}" class="btn btn-danger btn-xs">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
And code on the Controller:
public function subtaskdestroy(Request $request, Subtask $subtask)
{
$this->authorize('checkTaskOwner', $subtask);
$subtask->delete();
return redirect('/tasks');
}
With this code, I am getting an error like this:
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 161:
You mistake when defined route for delete. It should be like this:
Route::delete('/subtask1/delete/{subtask}', 'TaskController#subtaskdestroy');
But you given:
Route::delete('/subtask1/delete/{{subtask}}', 'TaskController#subtaskdestroy');
More about Route Parameters:
Laravel Route Parameters
You used return redirect('/tasks'); in your controller. With this line the page will redirecte to route /tasks after successfully delete the data. Make sure you have /tasks route in your route file. Example:
Route::get('/tasks','YourController#method');