I'm just trying to make a simple form that deletes a model record from the database. I've done it before, but the following code only flashes the success message and redirects. The record never leaves the db.
Here is my form:
#foreach ($users as $user)
<li class="list-group-item {{($loop->iteration % 2 == 0) ? 'even' : 'odd'}}">
<h3>{{$user->first_name}} {{$user->last_name}}</h3>
<b>AccessID: </b> {{$user->id}} <br />
<b>Created: </b> {{$user->updated_at}}
<form action="{{ url('/users/delete', ['id' => $user->id]) }}" method="post">
<input type="hidden" name="_method" value="delete" />
{!! csrf_field() !!}
<button type="submit">Delete</button>
</form>
</li>
#endforeach
Here is my controller method:
public function destroy(User $user)
{
$user->delete();
Session::flash('action', 'Deleted');
Session::flash('status', $user->first_name . $user->last_name .'s access successfully removed.');
return redirect('/users');
}
Here is my route: Route::delete('/users/delete/{id}', 'UserController#destroy');
The answer was that my route parameter needed to be named user for laravel to recognize that it should expect a user object. I had {id} instead of {user}
Related
working with Laravel 6 project and I have following CommentController,
public function update(Request $request, $id)
{
$comment = Comment::find($id);
$this->validate($request, array('comment' => 'required'));
$comment->comment = $request->comment;
$comment->save();
Session::flash('success','Comment Created');
return redirect()->route('posts.show', $comment->post->id);
}
public function delete($id)
{
$comment = Comment::find($id);
return view('comments.delete')->withComment($comment);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$comment = Comment::find($id);
$post_id = $comment->post->id;
$comment->delete();
Session::flash('success','Deleted Comment');
return redirect()->route('posts.show', $post_id);
}
and My routes are as following
Route::delete('comments/{id}', ['uses' => 'CommentsController#destroy', 'as' => 'comments.destroy']);
Route::get('comments/{id}/delete', ['uses' => 'CommentsController#delete', 'as' => 'comments.delete']);
but when I try to delete comment got following validation error message
The comment field is required.
how could I fix this problem here?
edit.blade.php
#section('content')
<div class="col-sm-6">
<form action="{{ route('comments.destroy', $comment->id) }}" method="post">
#csrf
{{ method_field('PUT') }}
<button type="submit" class="btn btn-danger btn-block">Delete</button>
</form>
</div>
</div>
</div>
#endsection
Because HTML forms can't send PUT, PATCH and DELETE requests, we must "spoof" the request and include an input field that tells Laravel which type it is sending. You can read about that here.
Technically, this means inserting the following for your delete form.
<input type="hidden" name="_method" value="delete">
Laravel comes with different helpers that help you achieve this. In your update form, you have already done this by adding the method_field('PUT');. Here, you are instructing Laravel that this is a PUT request, and we need to do this too in your delete form.
The {{ method_field('PUT') }} simply needs to change to {{ method_field('DELETE') }} or you can use the built-in #method('delete')
Like this
#section('content')
<div class="col-sm-6">
<form action="{{ route('comments.destroy', $comment->id) }}" method="post">
#csrf
#method('delete')
<button type="submit" class="btn btn-danger btn-block">Delete</button>
</form>
</div>
</div>
</div>
#endsection
You can inspect your form HTML in the browser and notice the hidden input field added automatically.
$this->validate($request, array('comment' => 'required'));
In your blade view, if you are not posting a comment in your form, you will be facing this error The comment field is required
Make sure you are posting a comment, or any typo's.
Posting your blade view would help us alot more.
The 'Delete' method was not working so I used 'get' but my data is not deleting
<form method="post" class="delete_form" action="{{ url('/data', $row['id']) }}" id="studentForm_{{$row['id']}}">
{{ method_field('GET') }}
{{ csrf_field() }}
<button type="submit" class="btn btn-danger">{{ trans('Delete') }}</button>
</form>
public function destroy($id)
{
dd($id);
$student = Student::where('id',$id)->delete();
return redirect('/data/{id}')->with('success', 'Data Deleted');
}
}
Route::get('/data/{id}','App\Http\Controllers\StudentController#index');
Route::delete('/data/{id}','App\Http\Controllers\StudentController#destroy');
This is I finally got the answer
I ran the following command:
php artisan route:clear
php artisan route:list
Then it gave me suggested methods and delete method was shown for "Controller#destroy" route then I changed from "get" method to "delete" method. Then, my post was able to delete.
The form must be submitted via delete method
<form method="POST" action="{{ url('/data', $row['id']) }}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">{{ trans('Delete') }}</button>
</form>
More details can be found here: https://laravel.com/docs/8.x/routing#form-method-spoofing
I want to delete the record selected from the index list, but I'm having trouble using deleteRoute
web.php
Route::delete('/destroy/{id}', 'ProjectController#destroy')->name('despro');
Controller
public function destroy($id)
{
Project::destroy($id);
return redirect('/project');
}
index.blade.php
#foreach($view as $v)
<tr>
<td>{{$v->id}}</td>
<td>{{$v->project_name}}</td>
<td>{{$v->name}}</td>
<td>{{$v->division}}</td>
<td>{{$v->content}}</td>
<td>{{$v->date}}</td>
<td>{{$v->preferred_date}}</td>
<td>{{$v->user_name}}</td>
<td>
#foreach($cats as $cat)
#if($v->category_id == $cat->id)
{{$cat->name}}
#endif
#endforeach
</td>
<td>{{$v->status}}</td>
<td>{{$v->estimated_work_time}}</td>
<td>delete</td>
</tr>
#endforeach
Will be deleted and redirected to index.blade.php but
The GET method is not supported for this route. Supported methods: DELETE.
Come out
delete route require DELETE method.
If you're using Laravel 5.1 or later
<form action="{{ route('despro', ['id' => $v->id]) }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button>delete</button>
</form>
If you're using Laravel 5.6 or later then
<form action="{{ route('despro', ['id' => $v->id]) }}" method="POST">
#method('DELETE')
#csrf
<button>delete</button>
</form>
I have a problem to store data in a table. And after hours I still couldnt figure out, what the problem is. I would be so happy for some help!
WishlistController.php:
public function store($book_id)
{
$user_id=Auth::id();
$wishlist=new Wishlist;
$wishlist->book_id=$book_id;
$wishlist->user_id= $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id->id)
->with('success', 'Buch gewünscht');
The Model:
class Wishlist extends Model
{
public $table = 'wishlist';
public $fillable = ['book_id','user_id',];
the view.blade:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
<form action="someaction" method="POST">
<input type="hidden" name="book_id" value="{{$book->id}}"/>
</form>
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD.store', 'WishlistController#store');
When I check the table, nothing new is added.
Its frustrating :-(
Just change post to get in this line:
Route::post('wishlistCRUD.store', 'wishlistCRUD#store');
Try to do this. In your Controller, you don't need $book_id as a parameter because you can get it from the $request:
public function store(Request $request)
{
$user_id = Auth::id();
$book_id = $request->book_id;
$wishlist = new Wishlist;
$wishlist->book_id = $book_id;
$wishlist->user_id = $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id)
->with('success', 'Buch gewünscht');
}
Don't forget to add use Illuminate\Http\Request;
And then in your blade, you don't need to set a route in an anchor, because it is already defined in the form. And you don't need two forms, because Form::open already does that:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
{{ csrf_field() }}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
{!! Form::submit('wünschen') !!}
{!! Form::close() !!}
This should work.
You have created POST method in your web.php for adding book to wish list, in your blade template you need to submit hidden form on click of link.
in your web.php
Route::get('wishlistCRUD/book/{book_id}','WishlistController#get_book_by_id')->name('get_book_by_id');
Route::post('wishlistCRUD.store', 'WishlistController#store')->name('store');
In your blade template
<a class="btn btn-primary" href="javascript:void(0)" onclick="event.preventDefault();document.getElementById('addToWishlist').submit();">wünschen</a>
<form style="display:none" id="addToWishlist" method="POST" action="{{ route('store')}}">{{csrf_field()}} <input type="hidden" name="bookid" value="{{$book->id}}" /> </form>
in your controller
public function store(Request $request){
$bookID= $request->input('bookid');
$wishlist=new Wishlist();
$wishlist->book_id= $bookID
$wishlist->user_id= Auth::user()->id;
$wishlist->save();
return redirect()->route('get_book_by_id', ['book_id' => $bookID]);
}
public function get_book_by_id(Request $request,$book_id){
// find book by ID;
$book=Book::find($book_id);
// book found
if($book){
return view('book')->with('book',$book);
}else{
// book not found , redirect to 404 page or home page
return redirect('/');
}
}
In blade, change the
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
to a <button class="btn btn-primary" type="submit">wünschen</button>
Delete the tag form, it's not necesary. You declared it with the {!! form:open... !!}
You are trying to access to route('wishlistCRUD.store',$book->id) by GET with the <a></a>
Try this:
the view.blade:
{!! Form::open(['url' => 'wishlistCRUD/store', 'method' => 'POST']) !!}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
<button class="btn btn-primary" type="submit">wünschen</button>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD/store', 'WishlistController#store');
Destroy method not working
I am trying to delete an image from my database but I am receiving the following error:
Undefined variable: portfolio (View: C:\MyProjects\bubblehouseProject\resources\views\admin\portfolio\addPortfolio.blade.php)
I believe there is something wrong with the logic I am trying to implement in my Laravel application. I am using Eloquent ORM.
Controller:
public function Destroy($id) {
$portfolio = new Portfolio();
$portfolio::find($id);
$portfolio->delete();
return redirect('addPortfolio')->with('delete', 'The image has been successfully deleted!');
}
Route:
Route::get('addPortfolio/{id}', 'AddPortfolioController#Destroy');
View:
<form action="{{ route('addPortfolioController.Destroy', $portfolio->id) }}">
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
{{ method_field('DELETE') }}
</form>
I am not entirely sure that this is the right way to delete data from the database.
Route
Route::delete('addPortfolio/{id}', 'AddPortfolioController#Destroy')->name('portfolio.destroy');
View
<form action="{{ route('portfolio.destroy', $portfolio->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
</form>
Controller
public function destroy($id)
{
if(Portfolio::destroy($id)) {
return redirect('addPortfolio')->with('success', 'The image has been successfully deleted!');
} else {
return redirect('addPortfolio')->with('error', 'Please try again!');
}
}