404 not found when i try to delete some of my post - laravel

I try to delete some of my post with slug but it's not found.
My route:
Route::resource('/dashboard/berita', DashboardController::class)->parameters([
'berita' => 'post:slug'
])->middleware('auth');
My Controller:
public function destroy(Post $post) {
$post->delete();
return redirect('/dashboard/berita')->with('success', 'Berita sudah dihapus!');
}
My blade:
<form action="{/dashboard/berita/{{ $post->slug }}}" method="post" class="d-inline">
#method('delete')
#csrf
<button class="badge bg-danger border-0"> </button>
</form>

Your formatting is wrong, it should be
action="/dashboard/berita/{{ $post->slug }}"
Or better still, use a named route.
<form action="{{ route('post.destroy', $post )}}" >

The problem is with this section of your code: action="{/dashboard/berita/{{ $post->slug }}}"
you need to change it as below:
<form action="{{ url('/dashboard/berita/' . $post->slug) }}" method="post" class="d-inline">
</form>

Related

Laravel 9 Update Form Values with controller MVC

I want to update my input values ​​in the form in my database by sending them from the route to the controller. What am I doing wrong? Can you help me? I tried the #method('PUT') method before. It's a bit confusing. I am sharing the codes. I will be very happy if you can help, thank you.
...
<div class="modal-body">
<div class="form-group text-center"
style="position: center; margin-top:3em;">
<form action="{{ 'edit-name/' . $user->id }}" method="POST"
enctype="multipart/form-data">
#csrf
<label for="fname">
{{ __('welcome.fullname') }}:</label>
<input name="name1" type="hidden"
placeholder="{{ $user->name }}">
</div>
<br>
<br>
<br>
{{-- //Name Edit Button --}}
<button type="submit"
class="btn-sm app-btn-secondary">{{ __('welcome.confirm') }}</button>
...
my route and controller
...
Route::get('edit-name/{id}', [HomeController::class, 'editname']);
...
...
public function editname(Request $request, $id){
dd(1);
$user= Auth::user();
dd($user);
$id=$request->id;
$name=$request->input('name1');
$isUpdateSuccess = User::where('id', $id) ->update(['name1'=>$name, ]);
if($isUpdateSuccess)
echo '<h1>Update Success</h1>';
else echo '<h1>Update Failed </h1>';
}
...
you are sending the data from modal using POST method but in your route you are using this data by GET method. I will suggest you to use POST method so that user_id will not be append in URL, so try this,
Your blade
<div class="modal-body">
<div class="form-group text-center" style="position: center; margin-top:3em;">
<form action="{{ 'edit-name' }}" method="POST" enctype="multipart/form-data">
#csrf
<input type="hidden" name="id" placeholder="{{ $user->id }}">
<label for="fname">
{{ __('welcome.fullname') }}:</label>
<input name="name1" type="text" placeholder="{{ $user->name }}">
</div>
<br>
<br>
<br>
{{-- //Name Edit Button --}}
<button type="submit" class="btn-sm app-btn-secondary">{{__('welcome.confirm') }}</button>
Your route
Route::post('/edit-name', [HomeController::class, 'editname']);
Your controller
public function editname(Request $request){
// dd(1);
$user= Auth::user();
// dd($user);
$id=$request->id;
$name=$request->input('name1');
$isUpdateSuccess = User::where('id', $id) ->update(['name1'=>$name, ]);
if($isUpdateSuccess)
echo '<h1>Update Success</h1>';
else echo '<h1>Update Failed </h1>';
}
Change action
from
action="{{ 'edit-name/'. $user->id }}"
to
action="{{ url('edit-name',$user->id) }}"

Can't delete first post added by a user in laravel

I am working on social media website with laravel in which posts added by user is listed in user's profile and user can delete the post ,but everything is working perfectly except user can't delete the first post added. Why it is happening. I think the delete function from PostController is not calling when trying to delete first post.
I am giving my code
Route::delete('/deletepost/{id}', 'PostController#delete')->name('deletepost');
PostController.php
public function delete($id)
{
Post::find($id)->delete();
if (Auth::user()->id == 1) {
return redirect(route('admin.dashboard'))
->with('successMsg', 'Place Deleted Successfully');
}else{
return redirect(route('author.dashboard'))
->with('successMsg', 'Place Deleted Successfully');
}
}
code in dashboard.blade.php
#if(Auth::user()->posts)
#foreach(Auth::user()->posts as $post)
<div class="column">
<div class="col-sm-4">
<form id="delete-form-{{$post->id}}" action="{{ route('deletepost',$post->id) }}" method="post" style="display:none">
{{ csrf_field() }}
{{ method_field('delete') }}
</form>
<div class="panel panel-primary">
<button type="button" class="btn btn-link">Edit</button>
<button type="button"
onclick="if(confirm('Are you sure to delete this post?')){
event.preventDefault();
document.getElementById('delete-form-{{$post->id}}').submit();
}else{
event.preventDefault();
}"
class="btn btn-link">Delete</button>
<div class="panel-heading">{{ $post->title }}</div>
<div class="panel-body"><img src="{{ asset('images/'.$post->images->first()->pictures) }}" class="img-responsive" style="width:100%;height:140px" alt="Image"></div>
<div class="panel-footer">{{ $post->category }}</div>
</div>
</div>
</div>
#endforeach
#endif
Note: delete function is working perfectly when deleting posts except first post and i am not getting any errors messages
Try this on PostController
Post::destroy($id);
if(auth()->user()->id() === 1)
return redirect()->route('admin.dashboard')->with('msg', 'deleted!');
return redirect()->route('author.dashboard')->with('msg', 'deleted!');
First line will delete Post based on ID. rest of them are just make it cleaner
UPDATE
Update your form Route
action="{{ route('deletepost',$post->id) }}"
To
action="{{ route('deletepost', ['id' => $post->id]) }}"

I find this error: Illuminate\Routing\Exceptions\UrlGenerationException Missing required parameters for [Route: Product.update]

Controller:
public function update(Request $request, Product $product)
{
$request->validate(['name'=>'required',
'price'=>'required'
]);
$product->update($request->all());
}
In the view:
<form action="{{route('Product.update',$product)}}" method="GET">
Route:
Route::resource('Product', 'ProductController');
Parameter Request will come from the data of form
Change this line:
<form action="{{route('Product.update',$product)}}" method="GET">
to
<form action="{{ route('Product.update', ['product' => $product]) }}" method="POST">
The default Route::resource HTTP method for update action is 'patch' and 'put'.
So in view, you may pass 'post' as method instead of 'GET'.
Then you should write {{ method_field('PUT') }} and a hidden input after your form tag as below :
<form method="POST" action={{ route('product.update' , $product) }}>
#csrf
{{ method_field('PUT') }}
<input type="hidden" name="_method" value="PUT">
</form>
Looking through the Laravel docs, I think for the view you need to do POST instead of GET. You might also need to spoof the form method with #method('PUT').

getting error Missing required parameters for [Route: shortlist.update] [URI: recruiter/shortlistt/{id}/{jid}]

this is my view page route
<form method='post' action={{ route('shortlist.update', ['id' => $dd->jobseekers_unique_id, 'jid' => $dd->job_unique_id ]) }}>
{{ csrf_field() }} {{ method_field('PATCH') }}
<button type='submit' class="col-md-5 col-sm-5 btn btn-success">Shortlist</button>
</form>
and this my main route
Route::patch('shortlistt/{id}/{jid}','RecruiterController#shortlisted')->name('shortlist.update');
But it's giving me the error and I don't know how to make it work.
You have syntax error in your code, you missed the quotes for action. Here is your updated code.
<form method='post' action="{{ route('shortlist.update', ['id' =>
$dd->jobseekers_unique_id, 'jid' => $dd->job_unique_id ]) }}">
{{ csrf_field() }} {{ method_field('PATCH') }}
<button type='submit' class="col-md-5 col-sm-5 btn btn-success">Shortlist</button>
</form>
Hope it helps.

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