skip to Main Content

This is the code I have written so far:

Here is my Controller that contains the edit and update function:

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');
    }

Here are my Web.php routes:

Route::get('/post/{id}/edit', [PostController::class, 'edit']);
Route::put('/post/{id}/update', [PostController::class, 'update']);

Here is my blade(view) file

<form method="POST" action="/post/{{$post->id}}/update">
    @csrf
    @method('PUT')
    <label class="underline" for="title">change title:</label></br>
    <input type="text" name="title" value="{{$post->title}}"></input></br>
    <label class="underline" for="decsription">change description:</label></br>
    <input type="text" name="description" value="{{$post->description}}"></input></br>
    <label class="underline" for="price">change price:</label></br>
    <input type="text" name="price" value="{{$post->price}}"></input></br>

    <input type="submit" value="submit">
</form>

2

Answers


  1. 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 class="underline" for="title">change title:</label></br>
    <input type="text" name="title" value="{{$post->title}}" required></input></br>
    <label class="underline" for="decsription">change description:</label></br>
    <input type="text" name="description" value="{{$post->description}}" required></input></br>
    <label class="underline" for="price">change price:</label></br>
    <input type="text" name="price" value="{{$post->price}}" required></input></br>
    
    <input type="submit" value="submit">
    
    Login or Signup to reply.
  2. 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 class="underline" for="title">change title:</label></br>
    <input type="text" name="title" value="{{$post->title}}" required></input></br>
    <label class="underline" for="description">change description:</label></br>
    <input type="text" name="description" value="{{$post->description}}" required></input></br>
    <label class="underline" for="price">change price:</label></br>
    <input type="text" name="price" value="{{$post->price}}" required></input></br>
    
    <input type="submit" value="submit">
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search