Deleting record in Laravel - laravel

Below is my code which I have written to delete data from MySql using laravel but I am facing a problem while deleting; which is it always delete the top most row; regardless of which row I clicked.
Edit
<a href="#" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form').submit();
}
">
Delete
<form id="delete-form" action="{{ route('posts.destroy',[$post->id]) }}" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>
Here is my controller file:
public function destroy(Post $post)
{
dd($post->id); same id comes here always rregardless of which row I might click...
$findpost = Post::find($post->id);
if($findpost->delete()){
return redirect('/posts')->with('success','Post Removed Successfully') ;
}
return back()->withinput()->with('error',"Company Post be deleted");
}

It's because of you define same ID for all your fomrs.
You can define unique form id by adding POST ID at the end of it.
So, your code would be:
Edit
<a href="#" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form{{$post->id}}').submit();
}
">
Delete
<form id="delete-form{{$post->id}}" action="{{ route('posts.destroy',[$post->id]) }}" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>

The problem you are facing is because you are referencing to the same form every single time. ID in HTML is unique for an element, and it will always reference to one element randomly in your case always the first element with that ID on the page.
A better approach would be to have the url for each element in the anchor tag:
<a href="{{ route('posts.destroy',[$post->id]) }}" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form').setAttribute("action", this.href).submit();
}
return false; // to prevent submitting the form on click before the alert is displayed
">
and have one <form> element in your view:
<form id="delete-form" action="" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>

Related

laravel 7 - unable to pass input parameter to form action for searching purpose

how i can pass input value form action and replace number 5? the below works if i put numbers only, then it pass it to route then controller:
<!--Search to mysql -->
<form action="{{ route('propertyname.show', 5)}}" method="GET" role="search">
#csrf
<Label for="id">search:</Lebal>
<input type="text" id='id' name="id" placeholder="Search plzz">
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />
<input type="submit" name="submit" value="Serach">
</form>
Route::get('/property_name/{id}','RsmsController#show')->name('propertyname.show');
the above works if I typed number 5 manually which I don't want, I want it to read\input from FORM TEXT and fired when clicking on the form button to send the value to the controller.
http://127.0.0.1:8000/property_name/5
enter image description here
You can take a look at simple jQuery on click/submit method.
$(function() {
$("#myform").submit(function(e) {
var actionUrl = "{{ url('/property_name/') }}" + $("#id").val();
$(this).attr("action", actionUrl);
});
});

Laravel datatables raw columns

i have a code like this to display an action button for datatables
->addColumn('action', function () {
return '<form id="delete" action="{{ route(' . 'admin.posts.destroy' . ', $model) }}" method="POST">
#csrf
#method("DELETE")
Edit
<input type="submit" class="btn btn-danger" value="Delete">
</form>';
})
But the #csrf and #method("DELETE") become a string/text (not method). I tried to append {{ }} in #csrf and #method("DELETE") but it doesn't work. How to change that text to method in blade templates without make a new view for action button like that?
Thank you!
#csrf replace with <input type="hidden" name="_token" value="{{ csrf_token() }}"> and
#method("DELETE") with <input type="hidden" name="_method" value="delete">

pass input value to url delete method laravel

i have form in my delete modal, and i want to pass hidden input to url method delete/destroy
this is my delete modal :
<form action="{{url('/pages').'/' ... }}" method="POST" enctype="multipart/form-data">
{{ method_field('DELETE') }}
{{csrf_field()}}
<div class="form-group">
<label for="">Are you sure to delete this Page ?</label>
<input type="text" class="form-control" id="page_id" style="display: none;">
</div>
<button type="submit" class="btn btn-danger" id="save">Delete</button>
<button type="button" class="btn btn-link" data-dismiss="modal">{{__('Close')}}</button>
</form>
how to append input value(page_id) to form action ?
so i can use that page_id in form action :
<form action="{{url('/pages').'/'page_id}}" method="POST" enctype="multipart/form-data">
can some one help me ? thanks before :)
You have to add a new attribute on each delete button contains the id.
please check the following code
$('.delete-button').click(function(){
var pageId = $(this).data('page-id');
$('.page_id').html(pageId);
$('#modal-form').attr('action', deleteUrl+pageId);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
page 1
page 2
page 3
<form action="#" id="modal-form" method="POST" enctype="multipart/form-data">
<label for="">Are you sure to delete this Page <span class="page_id"></span>?</label>
</form>
<script>
/* window.deleteUrl = "{{url('/pages').'/'}}"; // save globally */
window.deleteUrl = "http://test.com/"; // temporary
</script>
If you are using boostrap modal then you have to pass data from that button something like this,
<button id="submit-btn" data-action="{{route('test')}}">Submit</button>
In JS,
replace modal's action with data-action.
Something like this,
$('#form-id').val($('#submit-btn').data('action))
And if you are not using modal then just store that id in variable and pass in route directly.
Hope it helps :)

form displaying in controller popup

i make form that i want to link in with controller called companies controller but i add form code inside the list it show the whole code ink the web page
<li>
<a href="#" onclick="
var result = confirm('are you sure you want to delete this project?');
if(result){
event.preventDefault();
document.getElementById('delete-form').submit();
}
">
Delete
</a>
<form id="delete-form" action="{{ route('companies.destroy',[$company->id]) }}" method="post" style="display:none;">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
</form>
</li>
in the web page only the delete should visible in the section not other part of the code
first of all, separate your code of javascript from html so that it be understandable and also easy to debug. try this:
<li>
Delete
<form id="delete-form" action="{{ route('companies.destroy',[$company->id]) }}" method="post" style="display:none;">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
</form>
</li>
<script type="text/javascript">
function delete()
{
var result = confirm('are you sure you want to delete this project?');
if(result){
event.preventDefault();
document.getElementById('delete-form').submit();
}
}
</script>
Found some typos which I corrected, maybe that was the issue. Also, you could very well send an ajax instead of submitting a hidden form, just an idea.

Laravel Cannot retrieve a specific record from a form

I am outputting a list from a database table 'books' like this:
function edit()
{
$books = Book::all();
return view('layouts/editbooks', ['books' => $books]);
}
And displaying them like this:
#extends('layouts.master')
#section('title')
#section('content')
<h1>Edit Book</h1>
<form action="{{url('editbook')}}" method="GET">
{{ csrf_field() }}
#foreach ($books as $book)
<div>
<label>{{$book->title}}</label>
<input type='radio' value='{{$book->id}}' name='books[]'/>
</div>
#endforeach
<input type="submit" class="btn btn-warning form-control" value="Edit">
</form>
#endsection
I then want user to choose which record they want to edit by selecting a radio button, and upon clicking a submit button, redirect user to a new page with details of a book.
Therefore I am trying to get a book id from radio button and then if id matches, display everything from that record:
function editing(Request $request)
{
$edit = $request->books;
return view('layouts/editing', ['edit' => $edit]);
}
function updateEdit()
{
$books = DB::table('books')->where('id', $edit)->first();
}
And that is displayed in a view:
#extends('layouts.master')
#section('title')
#section('content')
<h1>Delete Book</h1>
<form action="{{url('removebook')}}" method="POST">
{{ csrf_field() }}
<div>
<input name="name" type="textbox" value="{{ old('name', $edit['name']) }}"/>
</div>
<input type="submit" name="submitBtn" value="Delete Book">
</form>
#endsection
However I get an error message saying:
Undefined index: name (View: C:\xampp\htdocs\laraveladvweb\resources\views\layouts\editing.blade.php)
What is causing the issue?
Isn't $edit just the id of the book?
try this
function editing(Request $request)
{
$edit = $request->books;
$book = DB::table('books')->where('id', $edit)->first();
return view('layouts/editing', ['edit' => $book]);
}
#extends('layouts.master')
#section('title')
#section('content')
<h1>Delete Book</h1>
<form action="{{url('removebook')}}" method="POST">
{{ csrf_field() }}
<div>
<input name="name" type="textbox" value="{{ old('name', $edit['name']) }}"/>
</div>
<input type="submit" name="submitBtn" value="Delete Book">
</form>
#endsection

Resources