I am not sure why I'm receiving a NotFoundHTTPException when I click on my delete button in my Laravel application.
index.blade.php
<form action="/users/3" method="POST">
<input type="hidden" value="DELETE" name="_method">
<button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>
UsersController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Gate;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function destroy()
{
return 'yes';
}
}
routes.php
Route::get('users', ['as' => 'users', 'uses' => 'UsersController#index']);
Route::get('users/{user}', ['as' => 'users.show', 'uses' => 'UsersController#show']);
Route::get('users/{user}/edit', ['as' => 'users.edit', 'uses' => 'UsersController#edit']);
Route::delete('users/{user}', ['as' => 'users.delete', 'uses' => 'UsersController#destroy']);
Change the form:
<form action="/users/3" method="DELETE">
<button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>
You are using POST and you haven't defined a route for the "DELETE" verb. Either define a post instead of a delete route (Route::post(...)) or change the form to user the delete method as described above.
index.blade.php
<form action="{{url('/users/3')}}" method="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button class="btn btn-sm btn-icon btn-pure btn-default on-defaul" data-original-title="Delete" data-toggle="tooltip" type="submit">
</form>
UsersController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Gate;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function destroy($id)
{
return 'yes';
}
}
You should change:
<form action="/users/3" method="POST">
To the following:
<form action="<?= route('users.delete', [$user]) ?>" method="POST">
Related
web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::post('/products/{qty}/add', 'ProductController#addProduct')->name('addProduct');
products-list.blade.php
<form action="{{ route('addProduct', 1) }}" method="post" class="ui form">
<input type="text" name="qty" value="1" />
<button type="submit" class="btn btn-primary" type="button">Add</button>
</form>
app/Http/Controllers/ProductController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function addProduct(Request $request, $qty)
{
dd($qty);
}
}
But the controller class exists?
Namespacing won't help in this scenario, because it's just a string:
Route::post('/products/{qty}/add', 'ProductController#addProduct')
This will work:
Route::post('/products/{qty}/add', [ProductController::class, 'addProduct'])
or you can do:
Route::post('/products/{qty}/add', 'App\Http\Controllers\ProductController#addProduct')
Hello everyone I have an issue I implemented language switcher however there seems to be an issue for pages that got id parameter as the pictures below shows it's no longer recognizable
Before action Image
After Action Image : Error
Routes :
Route::redirect('/', '/fr');
Route::group(['prefix'=>'{language}'],function(){
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/projets', [App\Http\Controllers\ProjectController::class, 'index'])->name('projects.index');
Route::get('/projets/create',[App\Http\Controllers\ProjectController::class, 'create'])->name('projects.create');
Route::post('/projets',[App\Http\Controllers\ProjectController::class, 'store'])->name('projects.store');
Route::get('/projets/{id}', [App\Http\Controllers\ProjectController::class, 'edit'])->name('projects.edit');
Route::put('/projets/{id}', [App\Http\Controllers\ProjectController::class, 'update'])->name('projects.update');
Route::delete('/projets/{id}', [App\Http\Controllers\ProjectController::class, 'destroy'])->name('projects.destroy');
});
Code:
<td><a class="btn btn-success" data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit" href="{{ route('projects.edit',['language' =>app()->getLocale(),'id' => $project->id]) }}">Update</a>
</td>
<td>
<form action="{{ route('projects.destroy', ['language' => app()->getLocale(),'id' =>$project->id])}}" method="post">
#csrf
#method('DELETE')
<button class="btn btn-danger" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete" type="submit" onclick="return confirm('Etes-vous sûr de la suppression?');" >Delete</button>
</form>
I am creating a simple login function on my website with a registration feature. The registration feature is functional and users are appearing in the database. However, logging in is not working. When I enter credentials, be it correct or wrong, But It is not working.
laravel 5.5
Please look at my codes.
Middleware/Authenticates.php
Did I write this correctly?
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticates
{
public function handle($request, Closure $next, $guard = 'web')
{
if (Auth::guard($guard)->check())
{
return $next($request);
}
return redirect()->route('/');
}
}
LoginController.php
public function getLogin()
{
if (Auth::guard('web')->check())
{
return redirect()->route('dashboard');
}
return view('login');
}
routes/web.php
Route::get('/', ['as' => '/', 'uses' => 'LoginController#getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController#getLogin']);
Route::group(['middleware', ['authenticates', 'roles']], function (){
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController#getLogout']);
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController#dashboard']);
});
login.blade.php
<form class="login-form" action="{{ route('login') }}" method="post">
{!! csrf_field() !!}
<div class="login-wrap">
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
<input type="text" name="username" class="form-control" placeholder="Username" autofocus>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
<input type="password" name="password" class="form-control" placeholder="Password">
</div>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
<span class="pull-right"> Forgot Password?</span>
</label>
<button class="btn btn-primary btn-lg btn-block" type="submit">Login</button>
<button class="btn btn-info btn-lg btn-block" type="reset">Reset</button>
</div>
</form>
When i try to save data from form in laravel allways get this error:
TokenMismatchException in VerifyCsrfToken.php line 68:
But error shows when i access laravel by apache, when i run laravel server by command php artisan serve --host 0.0.0.0 it works perfect...
This is my form view:
<form class="" method="POST" action="{{ $card->path() }}/notes">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<textarea name="body" class="form-control" rows="8" cols="40"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="button">Dodaj</button>
</div>
</form>
This is my controller:
<?php
namespace App\Http\Controllers;
use App\Card;
use App\Note;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store(Request $request, Card $card)
{
$card->notes()->save(
new Note(['body' => $request->body])
);
return back();
}
}
And here is my function that will redirect user after success send form to database:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Card extends Model
{
//
public function notes()
{
return $this->hasMany(Note::class);
}
public function path()
{
return '/cards/' . $this->id;
}
}
Add this line in header
<meta name="csrf-token" content="{{ csrf_token() }}" />
add this after form tag {{ csrf_field() }}
In Laravel best practice is using Blade for form. It will create token itself.
{!! Form::open(['action'=>$card->path().'/notes','method'=>'post']) !!}
// fields and buttons
{!! Form::close() !!}
You first clear your view by
php artisan view:clear
And then write your view as this
<form class="" method="POST" action="{{ $card->path() }}/notes">
{!! csrf_field() !!}
<div class="form-group">
<textarea name="body" class="form-control" rows="8" cols="40"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="button">Dodaj</button>
</div>
</form>
This might solve your problem
I have the same problem, and fixed it by clear the view cache:php artisan view:clear and then change the storage folder to 777 not 775.
Try to do this on the page:
<form class="form-horizontal" role="form" method="DELETE" action="/user/{{ $user->id }}/delete">
<button type="submit" class="btn btn-danger">
Delete
</button>
</form>
The route:
Route::delete('user/{id}/delete', ['middleware' => ['admin'],
'uses' => 'Auth\UserController#destroy']);
The controller:
class UserController extends Controller
{
public function destroy($id)
{
DB::table('users')->where('id', $id)->delete();
return view('admin/dash');
}
}
I'm getting MethodNotAllowedHttpException in RouteCollection.php.
How do I fix it?
Solution:
Thanks to Josh. I solve it by changing form to
<form method="POST" action="/user/{{ $user->id }}/delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn btn-danger">
Delete
</button>
</form>
In some implementations of some versions of HTML, only GET and POST are allowed as methods.
You can overcome this by adding an addition attribute _method that you process yourself, or you can use JavaScript.
If you're using Chrome, check the Network tab in developer tools to verify either that this is the problem or add the request to your post for further diagnostics.
See here for more details.