Reputation: 13
I'm using laravel and I was trying to make an Ajax live search, I managed to make it work but there is one problem with @csrf and @method('DELETE') not working inside my code
foreach ($data as $row) {
$output .= '<div class="col s4 m4">
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<span class="card-title center">'. $row->first_name.' '.$row->last_name.'</span>
<p><u>id</u>: '. $row->id.'</p>
<p><u>email</u>: '. $row->email.'</p>
<p><u>Phone</u>: '. $row->phone.'</p><br>
</div>
<div class="card-action">
<a href="/users/'. $row->id.'" class="waves-effect waves-light btn">Infos</a>
<a href="/users/'. $row->id.'/edit" class="waves-effect waves-light btn">Edit</a>
<a class="waves-effect waves-light btn modal-trigger center-align" href="#modal1">Delete<i class="material-icons right">delete</i></a>
</div>
</div>
</div>
<div id="modal1" class="modal">
<div class="modal-content">
<h4>Delete User</h4>
<p>Are you sure to delete this user?</p>
</div>
<div class="modal-footer">
<a href="#" class="modal-action modal-close waves-effect waves-red btn-flat ">Cancel<i class="material-icons right">close</i></a>
<form action="/users/'. $row->id.'" method="POST">
@csrf {{-- Doesn't work --}}
@method("DELETE") {{-- Doesn't work --}}
<button class="modal-action modal-close waves-effect waves-green btn-flat" type="submit" name="action" value="delete">Confirm
<i class="material-icons right">check</i></a>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#modal1").modal();
});
</script>';
}
it shows me this error:
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, DELETE.
and @csrf / @method('DELETE') appears in my page as if it was a span. Sorry about my english I'm trying to be as clear as possible.
Upvotes: 1
Views: 281
Reputation: 2951
You are not using blade templating, therefore you can't use @csrf or @methos there. 2 Options for you:
<form action="/users/'. $row->id.'" method="POST">
<input type="hidden" name="_token" value=".' csrf_token() '.">
<input type="hidden" name="_method" value="DELETE">
<button class="modal-action modal-close waves-effect waves-green btn-flat" type="submit" name="action" value="delete">Confirm
<i class="material-icons right">check</i></a>
</form>
Upvotes: 0
Reputation: 2972
It's not working because you are using it in php
file. It will only work in blade.php
files.
Try one of the following solutions as alternative.
You can declare a hidden field
<input type="hidden" name="_method" value="delete" />
Or Laravel 5.1 or higher you can use in blade file
<form action="/users/'. $row->id.'" method="POST">
{{method_field('DELETE')}}
{{csrf_token}}
<button class="modal-action modal-close waves-effect waves-green btn-flat" type="submit" name="action" value="delete">Confirm
<i class="material-icons right">check</i>
</button>
</form>
Upvotes: 0
Reputation: 70
First of all @csrf and @method are Laravel-blade directive so it will have to define inside the blade file. Otherwise it will not work.
To use you can pass data from controller to view and define all html code in blade file.
Upvotes: 2
Reputation: 1807
It's better to work with routes name. Means instead of using:
<form action="/users/'. $row->id.'" method="POST">
@csrf {{-- Doesn't work --}}
@method("DELETE") {{-- Doesn't work --}}
<button class="modal-action modal-close waves-effect waves-green btn-flat" type="submit" name="action" value="delete">Confirm
<i class="material-icons right">check</i>
</button>
</form>
use:
<form action="{{ route('users.delete') }}" method="POST">
@csrf
@method("DELETE")
<button class="modal-action modal-close waves-effect waves-green btn-flat" type="submit" name="action" value="delete">Confirm
<i class="material-icons right">check</i>
</button>
</form>
maybe {{ route('users.delete') }}
not exists for you so you can see all route with command php artisan route:list
and use correct route name for delete method
Upvotes: 0