Here is my code so far: Controller:
public function edit($id){ $post = Posts::find($id);
return view('edit', compact('post'));
}
public function update(Request $request, $id){
$post = Posts::find($id);
$validatedRequests = $request->validate([
'title' => 'required|max:255|string|integer',
'description' => 'required|max:255|string|integer',
'price' => 'required|integer|max:255|'
]);
$post->create($validatedRequests);
return redirect('/Post/{{$post->id}}')->with('mssg', 'updates successfully');
}
Web.php:
Route::get('/post/{id}/edit', [PostController::class, 'edit']);
Route::put('/post/{id}/update', [PostController::class, 'update']);
View:
<form method="POST" action="/post/{{$post->id}}/update">
@csrf
@method('PUT')
<label for="title">change title:</label></br>
<input type="text" name="title" value="{{$post->title}}"></input></br>
<label for="decsription">change description:</label></br>
<input type="text" name="description" value="{{$post->description}}"></input></br>
<label for="price">change price:</label></br>
<input type="text" name="price" value="{{$post->price}}"></input></br>
<input type="submit" value="submit">
</form>
CodePudding user response:
In your controller
public function edit(Request $request, $id)
{
$post = Post::find($id);
$post->title = $request->title;
$post->description = $request->description;
$post->price = $request->price;
$post->save();
return redirect('/Post/{{$post->id}}')->with('mssg', 'updates successfully');
}
Your Route
Route::post('/post/{id}/edit', [PostController::class, 'edit']);
Your view
<form method="POST" action="/post/{{$post->id}}/edit">
@csrf
<label for="title">change title:</label></br>
<input type="text" name="title" value="{{$post->title}}" required></input></br>
<label for="decsription">change description:</label></br>
<input type="text" name="description" value="{{$post->description}}" required></input></br>
<label for="price">change price:</label></br>
<input type="text" name="price" value="{{$post->price}}" required></input></br>
<input type="submit" value="submit">
CodePudding user response:
Your Controller should be:
public function edit(Request $request, Post $post) {
$validatedRequests = $request->validate([
'title' => 'required|max:255|string|integer',
'description' => 'required|max:255|string|integer',
'price' => 'required|integer|max:255|'
]);
$post->update([
'title' => $request->title,
'description' => $request->description,
'price' => $request->price
]);
$post->save();
return redirect('/Post/{{$post->id}}')->with('mssg', 'updates successfully');
}
Your Route
Route::post('/post/{post:id}/edit', [PostController::class, 'edit'])->name('post.update');
Your View should be:
<form method="POST" action="{{route('post.update', ['post'=> $post->id])}}">
@csrf
<label for="title">change title:</label></br>
<input type="text" name="title" value="{{$post->title}}" required></input></br>
<label for="description">change description:</label></br>
<input type="text" name="description" value="{{$post->description}}" required></input></br>
<label for="price">change price:</label></br>
<input type="text" name="price" value="{{$post->price}}" required></input></br>
<input type="submit" value="submit">