I've encountered a problem submitting my form in laravel. My form structure looks this.
<form class="form-group" action="{{ route('writepoem') }}"
method="post" name="publish" enctype="multipart/form-data"
onsubmit="return validateForm();">
<input type="hidden" name="_token" value="{{ Session::token() }}">
<input type="text" name="user">
<textarea name="poem"></textarea>
<input type="submit" value="save">
</form>
My web.php file has this route.
Route::post('/writepoem', ['uses'=>'PoemController#postCreatePoem','as'=>'writepoem']);
My PoemController.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Poem;
class PoemController extends Controller
{
public function postCreatePoem(Request $request)
{
//validation
$poem=new Poem();
$poem->poem=$request['poem'];
$poem->user=$request['user'];
//save poem
$request->user()->poems()->save($poem);
return redirect()->route('feed');
}
}
On submitting my form I get this Exception.
(1/1) NotFoundHttpException
in RouteCollection.php line 179.
What could be the issue with routing?
If your route points site.com/writepoem and this route returns notfoundexception; this means 2 possible things;
1) Your route has a group and this group has a parent namespace for url
Route::group(['prefix' => 'poems'], function() {
Route::post('/writepoem', ['uses'=>'PoemController#postCreatePoem','as'=>'writepoem']);
});
2) Or your route file is cached. please try; php artisan route:clear & php artisan route:cache
I found that my form was linked to the main page via
`require resource_path().'/sub-folder/write.php'`
and that was causing the error.
I moved the form to the main document and my issue was fixed.
I had the same problem but now I fixed my problem.
You need to follow these steps. If you have written the right code and then you get this error, then you need to follow steps.
Clear cache php artisan route:clear & php artisan route:cache run command one by one. If your problem not fixed yet then you need to follow the second step also
Run this command to reload composer composer dump-autoload
Related
This is the error that I get rolled out
(View: Route [user.profile] not defined. (View: D:\Programi\XAMPP\htdocs\crushapp\resources\views\layouts\base.blade.php)
But my route is clearly defined here:
use App\Http\Livewire\User\ProfileComponent;
Route::middleware(['auth:sanctum','verified'])->group(function()
{
Route::get('/user/profile',ProfileComponent::class)->name('user.profile');
});
The error comes from the layout, namely:
<span><i class="fa fa-user-circle-o" aria-hidden="true"></i> {{ Auth::user()->name }} </span>
Any ideas what could it be?
UPDATE: Any new route I try defining under user won't work...
first you are missing a function name in your route
also run php artisan route:clear
Route::get('/user/profile',ProfileComponent::class, 'index')->name('user.profile')
i have a problem in delete an order:
here the form where press on cancel to delete the order:
<form action="{{route('user.orders.delete', $order->id)}}" method="POST"
onsubmit="return confirm('Are you sure?');">
#method('DELETE')
#csrf
<button
type="submit" class="mt-8 mb-4 py-2 px-14 rounded-full
bg-red-900 text-white tracking-widest hover:bg-blue-800
transition duration-200">
Cancel
</button>
</form>
this is my router:
Route::delete('user/orders/delete/{order}', [OrderController::class, 'delete'])->name('user.orders.delete');
OrderController : delete function:
public function delete(Order $order)
{
try {
unlink(public_path().'/storage/orders/files/'.$order->file);
$order->delete();
}catch(\Exception $e) {
return redirect()->back()-with('status', 'you cannot delete this item');
}
return redirect()->back()->with('status', 'product deleted succefully');
}
so when click on the button (cancel) just go to the right url : when 21 is the order id
http://127.0.0.1:8000/user/orders/delete/21
and it is just not found page!!! and nothing deleted!!!
i think your app routes is cached
try to run the fallowing command (in cmd or terminal) to see all the routes, this will show registered routes and errors if exist:
php artisan route:list
if the route doesnt exist in route list, the route list may be cached. try the fallowing command to clear route cache:
php artisan route:clear
and then run app and test the route.
If you do not have another problem in coding or naming variables, the program will be fixed properly
*if all o the above is ok, it seems your apache config is not configure to delete request(for route delete method ) and you must change .htaccess requests LIMIT like the fallowing sample to Grant delete request :
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
As you can see the DELETE method(request) has been added to .htaccess
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.
My CountryController is in admin folder which has store function.
Web.php is:-
Route::prefix('admin')->group(function () {
Route::resource('country', 'admin\CountryController');});
HTML Code is:-
<form method="post" action="{{ route('admin/CountryController.store') }}">
It's showing error:- Route [admin/CountryController.store] not defined. (View: C:\xampp\htdocs\happyvivah\resources\views\admin\country.blade.php)
use 'php artisan route:list' command in CMD. it will show the name of all route. it will show route name 'country.store'.
so after replacing to route('admin/CountryController.store') with route('country.store') it will work.
I don't know what I'm doing wrong, but my request URL for my Ajax Patch-request through Vue-resource is getting:
400 Bad Request
But this is only on the local environment!!! When I push it up to the server, it works!
Does anyone know why?
Here is my request:
var id = thing.id;
this.$http.patch('/api/things/' + id, thing).then(function(data){
console.log('edited to: '+data);
});
The request parameters should all be correct when looking at the Network tab in dev tools.
Here is my Route:
Route::resource('/api/things','ThingsPanelController');
Here is my Controller:
public function update(Request $request, $id)
{
Thing::findOrFail($id)->update([
'body'=>$request->body,
]);
return Response::json($request->all());
}
Could it be because the ID I added was 'api/things' + id and it sees it not as a wildcard in the route, but as a string! ?
This is my form I use. I prevent default and have the ajax request, so the form action shouldn't even be relevant...
<form action="update" #submit.prevent="doneEdit(thing)">
<input type="hidden" name="_method" value="PATCH">
<input type="text"
v-model="thing.body"
v-thing-focus="thing == editedThing"
#blur="doneEdit(thing)"
#keyup.esc="cancelEdit(thing)"
>
</form>
Check if the problem is persistent on your local server AS WELL AS your live server.
In this case the problem was only on the local server. Then just do this:
Stop artisan's serve, and execute: php artisan cache:clear
Then try again: php artisan serve and the problem should be solved.
The problem might be caused because of a program like CodeKit. Even though your php artisan serve would serve on ip 8000, CodeKit changes this for auto reload reasons. This messes up the ajax call uri's.