route is unable to run Controller - laravel-5

I'm new to laravel... created a route and given its a controller which has a method to update a database.. but once it reads the route the app is unable to get to the controller
Route::post('/workorder/store/third/{$id}',
[
'uses'=>'WorkOrdersController#storeThird',
'as'=>'workorder.store.third'
]);
//method in WorkOrderController
public function storeThird(Request $request,$id)
{
$this->validate($request,[
'address_region'=>'required|string',
'address_no'=>'required|string',
]);
$workorder = WorkOrder::find($id);
$workorder->address_region = $request->address_region;
$workorder->address_no = $request->address_no;
$workorder->save();
return view('third-workorder',compact('workorder'));
}
results in browser ...
in the address bar.. "http://localhost:8000/workorder/store/third/9"
and in the browser .."Sorry, the page you are looking for could not be found."
this.. view.blade looks like
<div class="modal" id="createThirdWorkshopModal">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST" action="{{ route('workorder.store.third',['id'=>$workorder->id]) }}" >
{{ csrf_field() }}

Navigating directly to http://localhost:8000/workorder/store/third/9 in your browser is a GET request, but you have the route defined as a route that handles a POST request. You need to submit something for that route to be "found":
<form method="POST" action="{{ url("/workorder/store/third/".$id) }}" ...>
...
</form>
or define the route as
Route::any("/workorder/store/third/{$id}", ...);
to handle this.
Note: ::any() handles all HTTP verbs (GET, POST, PUT, DELETE, etc.)

Router::post('/workorder/store/third/{$id}', WorkOrdersController#storeThird)->name('workorder.store.third');
and use:
<a href="{{route('workorder.store.third', $id)}}">
...
</a>

Related

laravel form is not submitted

I am new to laravel. This is my view (bids.new):
<form method="post" action="{{ route('bids.storeBid', $project->id) }}" enctype="multipart/form-data" >
#csrf
*content*
<div class="row pt-4">
<button class="btn btn-primary">Place Bid</button>
</div>
</form>
This is my route:
Route::get('/bids/new/{id}', 'BidController#create')->name('bids.new');
Route::post('/bids/{id}', 'BidController#storeBid')->name('bids.storeBid');
BidController:
public function create($id)
{
$project = Projects::findOrFail($id);
return view('bids.new',compact('project'));
}
public function storeBid(Request $request, $id)
{
*content*
}
When I clicked on Place Bid button in my view, the page is not responding and URL is still showing /bids/new/1 which means the storeBid route was not loaded. I tried using dd($id) at controller but it is not displaying as well so I assumed I have a problem with the route or form in the view.
<div class="row pt-4">
<button type="submit" class="btn btn-primary">Place Bid</button>
</div>
try this one. I hope it will help you. Let me know.
Thank you guys for the comments, I figured out the problem was not because of the form or route but due to some errors on my eloquent in the controllers.

Laravel verfication.resend - The GET method is not supported for this route. Supported methods: POST

Odd question here. Im using the default Auth::routes(['verify' => true]); In Laravel 6. So I register ( Custom registration form ) and all works fine ( added to database etc ) then I am taken to the verification page where it has an email link to resend. When I click this I get:
The GET method is not supported for this route. Supported methods: POST.
The View has this named routed in the link route('verification.resend')
As you can see here. Verify resend is a POST route. So GET method is not allowed. So it should be a form Post instead.
If you are using blade something like this will get you there.
<form method="POST" action="{{ route('verification.resend')) }}">
</form>
Because in laravel 6+ they added this route as a post so you can do it by below code
<a onclick="event.preventDefault(); document.getElementById('email-form').submit();">{{ __('click here to request another') }}
</a>.
<form id="email-form" action="{{ route('verification.resend') }}" method="POST" style="display: none;">
#csrf
</form>

I can't solve "Method Not Allowed Exceptions" in Laravel

The method not allowed exceptions is shown as follow, The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header card-header-success">
<h4>Account Registration Form</h4>
</div>
<form method="POST" action="{{ route('accounts.store')}}" enctype="multipart/form-data" id="commentForm">
#csrf
<!-- Codes -->
</form>
</div>
</div>
</div>
</div>
</div>
```
```Route::get('/', function () {
return view('home');
});
Route::resource("accounts", "AccountController");
Route::get('/accounts', 'AccountController#create');
Route::post('/accounts', 'AccountController#create');
```
Define routes only once in routes/web.php.
Remove the following lines:
Route::get('/accounts', 'AccountController#create');
Route::post('/accounts', 'AccountController#create');
The resource route definition provides in the accounts.store route if you matched the controller methods to the laravel docs.
Make sure that your AccountController also contains a function called store
Rewrite this route
Route::get('/accounts', 'AccountController#create')->name('accounts.create');
Route::post('/accounts', 'AccountController#store')->name('accounts.store');
not post
This because you overwrote your routes if you need to run routes below resources you need to give them same name of route like this
Route::post('/accounts', 'AccountController#create')->name('accounts.store');
Or if you want to use resources routes you need to put it below of your routes to avoid overwrite its name and urls

How to pass variable to view without using url in Laravel?

guys. I'm a newbie of Laravel.
I'm just wondering that if I need to pass some sensitive info, which is been wrote on a form, to controller, then to another view.
How could I pass the info to the view without using URL?
I have made some small test, but the result is all the same (url would include the data).
Here is the code.
Html(a):
<form action="/data" method="GET" class="form-horizontal">
<div class="form-group">
<input type="text" class="form-control" name="Test">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">test</button>
</div>
web.php:
Route::get('/data', 'DataController#show');
DataController.php:
public function show(Request $request) {
return view('testShow');
}
Html(b):
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->Test }}
#endif
Simply change your form to use POST instead of GET.
On the form...
<form action="/data" method="POST" class="form-horizontal">
In your route ...
Route::post('/data', 'DataController#show');
This will send all of the form fields to the server in a way that is less visable (not in the URL) but to make the form secure, you'll want to ensure it's served using HTTPS as well.
In Laravel when you use Route::post('/data', 'DataController#show') the Laravel expect a GET, because of the show function.
So you can use Route::post('/data', 'DataController#someFunction') and in your laravel controller get with:
public function someFunction(Request $request)
{
$array = $request->all(); //your fields
}
In this function you can get the attributes with $array = $request->all();

Undefined variable in view laravel

I have this code here which is listing all items from one user, user and item are two differents model and they have relationship with their primary keys.
#foreach($user->items as $item)
<p class="heading">
{{ $item->item_id }}
</p>
#endforeach
My question is how to send that item_id with post request to controller, because when I write like this for example <form class="form-horizontal" method="post" action="{{ route('preview', $item->item_id) }}" role="form" enctype="multipart/form-data"> it gives me that $item variable is not defined.
My controller:
public function preview($item_id){
return view('itempreview')->with('item_id', $item_id);
}
Any ideas how to send that item_id?
You should use array as 2nd param to route() method!
You are doing it wrong, you should send it like this:
route('preview', ['item_id' => $item->item_id]);
Make sure, your route should be defined as this:
Route::post('preview/{item_id}', 'ControllerName#preview')->name('preview');
Hope this helps!
You need to send $item_id as part of the POST:
#foreach($items as $item)
<form method="post" action="{{ route('preview') }}">
<input type="hidden" name="item_id" value="{{ $item->item_id }}">
</form>
#endforeach
Then in the method that receives the POST:
public function index() {
$item_id = request()->input('item_id');
return view('preview');
}

Resources