Home > Enterprise >  I am getting this error while want to update in the database
I am getting this error while want to update in the database

Time:10-30

Missing required parameter for [Route: blog.update] [URI: blog/{post}/update] [Missing parameter: post].

in routes : Route::put('/blog/{post}/update', [BlogController::class, 'update'])->name('blog.update');

in BlogController : `

public function update(Request $request,Post $post){
        $request->validate([
            'title' => 'required',
            'image' => 'required | image',
            'body' => 'required'
        ]);

        $postId = $post->id;
        $title = $request->input('title');
        $slug = Str::slug($title,'-').'-'.$postId;
        // $user_id = Auth::user()->id; 
        $body = $request->input('body');
        
        //File upload
        $imagePath =  'storage/'. $request->file('image')->store('postImages','public');
        
        // $post = new Post();
        $post->title = $title;
        $post->slug = $slug;
        // $post->user_id = $user_id;
        $post->body = $body;
        $post->imagePath = $imagePath;
        
        $post->save();
        
        return redirect()->back()->with('status', 'Post edited successfully');
        
        dd('validation passed . You can request the input');
    }

`

Please solve this issue

I want to update the post

CodePudding user response:

If you are using the route() helper in your form. You can pass the parameter using it :

<form action="{{ route('blog.update', ['post' => $post_id]) }}">...</form>

which is the post is the parameter you name in the route.

CodePudding user response:

Since you haven't posted your blade file so here's the full method:

in your route:

Route::put('/blog/{post}/update' , [BlogController::class, 'update']);

in your blade form make tag like this:

<form method="post" action="/blog/{{$post->id}}/update">  
//This will show url like /blog/1/update
//For using PUT method add this:
    {{ method_field('PUT') }}
// Do not forget to use @csrf

Then in your controller update function handle the request and $id like this:

public function update(Request $request, $id){
    //Find post in your data with help of model
    $post = \App\Models\Post::findOrFail($id);
    //validate and update with the post instance of Post class
    //Feel free to add many functions same as of your controller before i'm leaving empty
    $post->update([
        //Add fields for update
    ]);
}
  • Related