Reputation: 207
I'm getting the error:
BadMethodCallException in Macroable.php line 74: Method delete does not exist.
route:
Route::resource('posts', 'PostController');
my controller:
public function destroy($id)
{
$user_id = Auth::user();
$post= Post::where('id', $id)->where('user_id',$user_id)->get();
$post->delete();
return view('/home', [
'posts' => $post
]);
}
view:
<form action="{{ route('posts.destroy', '$post->id') }}" method="post">
<input type="hidden" name="_method" value="DELETE" />
{{ csrf_field() }}
{{ method_field('DELETE') }}
<input type="submit" class="btn btn-danger" value="delete" />
</form>
I tried changing method="post"
to delete
: error is gone but nothing gets deleted..
Upvotes: 12
Views: 52890
Reputation: 11
You try to delete one object but in your query you calling collection. Try to change method "get()" to "first()".
public function destroy($id)
{
$user_id = Auth::user();
$post= Post::where('id', $id)->where('user_id',$user_id)->first();
$post->delete();
return view('/home', [
'posts' => $post
]);
}
Upvotes: 1
Reputation: 16
Post::where('id', $id)->where('user_id',$user_id)->delete();
Or
$post= Post::where('id', $id)->where('user_id',$user_id);
if($post->get()->count()){
$post->delete();
}
Upvotes: 0
Reputation: 1722
This is how I am able to delete one (or multiple) associated record(s) in a table:
RFIResponseFile::where('rfi_response_id',$response_id)->delete();
Upvotes: 0
Reputation: 517
Please Try This:
public function destroy($id)
{
$userId = Auth::user()->id;
$post = Post::where([
'id' => $id,
'user_id' => $userId
])->delete();
Session::flash('success', 'Post was successfully deleted!');
return redirect()->route('posts.index');
}
Upvotes: 0
Reputation: 18577
As post id is primary key of posts
table you can directly remove from table,
No need of user_id
To fetch user_id from Auth facade you should use,
$user_id = Auth::id();
Only by passing id should work,
Post::find($id)->delete()
However, if you know the primary key of the model, you may delete the model without retrieving it by calling the destroy
method. In addition to a single primary key as its argument, the destroy
method will accept multiple primary keys, an array of primary keys, or a collection of primary keys:
Post::destroy($id)
Upvotes: 0
Reputation: 312
use ->first() instead of ->get()
you can't delete an entire collection with delete()
Upvotes: 3
Reputation: 1047
This is your code.
$user_id = Auth::user();
$post= Post::where('id', $id)->where('user_id',$user_id)->get();
$post->delete();
just add ->each()
before delete like this,
$post->each->delete();
It work's for me.
Upvotes: 30
Reputation: 1
Just add this on top of view, i 've error like you and now solved, sorry for bad english.
{!! Form::model($post, ['route' => ['posts.destroy', $post->id], 'method' => 'DELETE']) !!}
and
{!! Form::close() !!}
on bottom
for controller
$post = Post::find($id);
$post->delete();
Session::flash('success', 'Menu was successfully deleted!');
return redirect()->route('posts.index');
Upvotes: -1
Reputation: 118
In your controller
Before change code
public function destroy($id)
{
$user_id = Auth::user();
$post= Post::where('id', $id)->where('user_id',$user_id)->get();
$post->delete();
return view('/home', [
'posts' => $post
]);
}
After change code
public function destroy($id)
{
$user_id = Auth::user();
$post= Post::where(['id'=>$id,'user_id'=>$user_id])->get();
Post::where(['id'=>$id,'user_id'=>$user_id])->delete();
return view('/home', [
'posts' => $post
]);
}
Upvotes: -1
Reputation: 13609
Change get
for first
, and check if the post belongs to the user afterwards.
public function destroy($id)
{
$post = Post::where('id', $id)->first();
if($post && $post->user_id == \Auth::user()->id){
$post->delete();
return view('/home');
}else{
abort(404);
}
}
Upvotes: 2
Reputation: 544
controller:
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
//redirect to
return redirect()->back();
}
view:
{!! Form::open(['method' => 'DELETE','route' => ['posts.destroy', $post->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
Try this.
hope you created the controller with
--resource
flag.
Upvotes: 0
Reputation: 9045
Remove get() and it will work
$post= Post::where('id', $id)->where('user_id',$user_id);
$post->delete();
If you want to delete first document you can use :
$post= Post::where('id', $id)->where('user_id',$user_id)->first();
$post->delete();
However, you always need to check if $post is found as a query document or its null so addd :
if($post){
$post->delete();
}
Upvotes: 18