So my simple destroy don't work, what am I missing? - php

I'm trying to do the simpliest destroy with Laravel with a little modal, but it always appear a 404 Error.
I'm just doing in 2 parts, the index and the PostController, nothing else:
index (Button)
<button data-toggle="modal" data-target="#deleteModal" data-id="{{ $post->id }}" class="btn btn-danger"> Delete</button>
index (Modal DIV and Script)
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Are you sure to delete this?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<form method="POST" action="{{ route('post.destroy', 0) }}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
</div>
</div>
</div>
<script>
window.onload = function() {
$('#deleteModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var id = button.data('id')
var modal = $(this)
modal.find('.modal-title').text('You're going to delete the POST: ' + id)
})
}
</script>
PostController
public function destroy($id)
{
$post = Post::findOrFail($id);
$post->delete();
return back()->with('status', '¡Post deleted!');
}

I believe The second parameter of the helper method route should be an array while you have an integer 0.
Perhaps you mean to do something like this:
<form method="POST" action="{{ route('post.destroy', ['id' => $post->id]) }}">
(my apologies if the syntax is wrong, I'm more experienced with symfony/twig than laravel/blade)

Related

Laravel-6: Deleting Logged in user : The GET method is not supported for this route. Supported methods: POST

EDIT! LATEST CODE UPDATED, NEW ERROR^
I am currently designing a website which has a feature for users to create an account.
I am encountering problems trying to get the user to be deleted first of all, then for the user to be deleted whilst logged in.
My Users controller looks like so:
public function destroy(Request $request)
{
$user = Auth::user();
Auth::logout();
if ($user->delete())
{
return Redirect::route('\home')->with('global', 'Your account has been deleted!');
}
}
My Modal bootstrap window that opens from a form looks like this;
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Are you sure?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<form action="{{route('users.delete', ['user' => Auth::id()])}}" method="Post">
<span aria-hidden="true">×</span>
</button>
</div>
#csrf
#method('delete')
<div class="modal-body">
Are you sure you want to permanetly delete your account?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">No, cancel</button>
<button type="submit" class="btn btn-danger">Yes, delete my account</button>
</div>
</div>
</div>
</div>
The form the modal opens from:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<img src="{{ asset('/uploads/avatars/' . $user->avatar ) }}" style="width:100px; height:100px; float:left;
margin-right:25px ">
<strong>Delete {{$user->name}}'s account?</strong></div>
<div class="card-body">
<form action="delete" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="name">Account Email:</label>
<input type="text" name ="email" value="{{$user -> email}}" class="form-control" readonly>
<div class="form-group">
<div class="text-centre">
<p></p>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">
Delete
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
And finally my route looks like this;
Route::post('/users/delete', 'Admin\UsersController#destroy')->name("delete-account");
Any ideas on how to firstly get this working and secondly implement it are welcome. thank you
Please tell us the error you are getting.
The first thing I recognize seeing your code is, that you return on the second line of your method. But after the return, you are still expecting code to run. Unfortunately, this will not work. Code after a return is ignored.
Try to split the methods:
Route::delete('/users/destroy', ['uses' =>'Admin\UsersController#destroy', 'as' => 'users.destroy']);
Route::get('/users/delete', ['uses' =>'Admin\UsersController#delete', 'as' => 'users.delete']);
Form method and request should be like this:
<form action="{{route(users.destroy)}}" method="delete">
Actual problem was the mismatch of route types. You have defined a route of GET type while the form is submitted to a route of DELETE type.
Please replace your code with this one and check:
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Are you sure?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<form action="{{route('users.delete', ['user' => Auth::id()])}}" method="Post">
<span aria-hidden="true">×</span>
</button>
</div>
#csrf
#method('delete')
<div class="modal-body">
Are you sure you want to permanetly delete your account?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">No, cancel</button>
<button type="submit" class="btn btn-danger">Yes, delete my account</button>
</div>
</div>
</div>
</div>
And change your route to this
Route::delete('/users/{user}', 'Admin\UsersController#destroy')->name("users.delete");
Your controller lacks an IF-THEN statement
public function destroy(User $user)
{
if($user->id !== Auth::id()) return view('admin.users.delete')->with('user', Auth::user());
Auth::logout();
if ($user->delete())
{
return Redirect::route('\home')->with('global', 'Your account has been deleted!');
}
}
Please replace your code with this one and check:
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Are you sure?</h5>
<form action="{{route('delete-account'}}" method="Post">
#csrf
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</form>
</div>
<div class="modal-body">
Are you sure you want to permanetly delete your account?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">No, cancel</button>
<button type="submit" class="btn btn-danger">Yes, delete my account</button>
</div>
</div>
</div>
</div>
And change your route to this
Route::post('/users/delete', 'Admin\UsersController#destroy')->name("delete-account");
Your controller lacks an IF-THEN statement
public function destroy(Request $request)
{
$user = Auth::user();
Auth::logout();
if ($user->delete())
{
return Redirect::route('\home')->with('global', 'Your account has been deleted!');
}
}

How to pass #foreach value into Bootstrap Modal? Laravel 7.3

I have a list of categories, from where I want to pass {{category.name}} to data into Bootstrap modal.
I want to achieve that when I click on the specific button, to show the name of the category in the modal as a message?
Categories
<div class="card mt-2">#foreach($categories as $category)
<ul class="list-group list-group-flush">
<li class="list-group-item">{{ $category->name}}
<a href="" class="btn btn-danger btn-sm ml-2 float-right"
onclick="handleDelete({{$category->id}})">Delete</a>
Edit
</li>
</ul>
#endforeach
</div>
From where I've tried to pass {{$category->name}} into div element, but It showing the last element from the loop.
Modal
<form action="" method="POST" id="deleteCategoryForm">
#csrf
#method('DELETE')
<div class="modal fade" id="deleteModalCenter" tabindex="-1" role="dialog" aria-labelledby="deleteModalCenterTitle"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Delete category</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete "{{$category->name}}" ?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Go Back</button>
<button type="submit" class="btn btn-danger">Yes, Delete</button>
</div>
</div>
</div>
</div>
</form>
JS Script
<script>
function handleDelete(id){
event.preventDefault();
var form = document.getElementById('deleteCategoryForm');
console.log('Delete', form);
form.action = '/categories/' + id;
$('#deleteModalCenter').modal('show');
}
</script>
Thanks in advance

Pass data from laravel controller to Modal

I want to edit in Modal, but the data does not come through to opgaveRet.blade.php containing Modal :-( Here is my code:
Can anyone help me to see what I am doing wrong?
// route.php
Route::resource('/admin/opgave', 'Admin\OpgaveController');
// OpgaveController.php
public function edit($id)
{
$tasks = Tasks::findOrFail($id);
return view('admin.opgaver.opgaveRet', ['tasks' => $tasks ]);
// also tried :-(:
//return view('admin.opgave', compact('tasks'));
}
// opgave.blade.php
#foreach ($opgaver as $opgave)
// here is a table, and then comes the Action
<a href="{{ route('opgave.edit', $opgave->id) }}"
data-toggle="modal"
data-target="#RetOpgave"
class="btn btn-primary btn-xs">Edit</a>
#endforeach
// in bottom of opgave.blade.php
// #include('admin.opgaver.opgaveRet')
<div class="modal fade" id="RetOpgave" tabindex="-1" role="dialog" aria-labelledby="RetOpgave">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="RetOpgave">Ret opgave</h4>
</div>
<div class="modal-body" >
#if(!empty($tasks))
// Here i want to build a FORM::
// But there is nothing in $tasks ???????
{{ dd($tasks) }}
#endif
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Luk</button>
</div>
</div>
</div>
</div>
You have to call the edit function in AJAX.
this one may help you:
https://tutorials.kode-blog.com/laravel-5-ajax-tutorial

How to pass value from view to modal?

I have this link where i open modal:
<li><i class="fa fa-times"></i></li>
And i have modal in seperate page modals.blade.php
<div class="modal fade modal_form" id="deleteProperty{{$property->id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel5" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="exampleModalLabel5">Are you sure you want to delete this property?</h4>
</div>
<div class="modal-body">
<form method="POST" action="{{ route('user.property.delete',[$user->id,$property->id])}}">
<div class="form-group">
<div class="left-btn">
<button type="button" class="btn btn-primary" data-dismiss="modal">No</button>
</div>
<div class="right-btn">
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
How can i pass this parameter ($property->id) to modal so when user click on specific property, to delete that property?
You need to pass the variables you want to use in the included views.
Example:
#include('standardUser.includes.modals', [ "property" => $property ])

Show bootstrap modal when click on href Laravel

How can I show modal with dynamic content from database in Laravel?
my view:
<li>{!! $test->test_name !!} </li>
my model:
public function show($slug)
{
$test = Test::whereSlug($slug)->firstOrFail();
return view('tests.show', compact('test'));
}
This modal I want to show on active page instead of creating new view. I guess it could be done with return view()->with but can not implement it.
You can do this trick if you want.
in your controller:
public function show($slug)
{
$test = Test::whereSlug($slug)->firstOrFail();
return view('tests.show', compact('test'));
}
and in your view:
<li><button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#yourModal"></li>
<div class="modal fade" id="yourModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">{{$test->someTitle}}</h4>
</div>
<div class="modal-body">
{{$test->someField}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
And then if you have multiple data to get, you just have to use a foreach. For example:
controller
public function show()
{
$test = Test::all();
return view('tests.show', compact('test'));
}
view:
#foreach ($test as $t)
<li><button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#yourModal{{$t->id}}"></li>
#endforeach
#foreach ($test as $t)
<div class="modal fade" id="yourModal{{$t->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">{{$t->someTitle}}</h4>
</div>
<div class="modal-body">
{{$t->someField}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
#endforeach

Categories