Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laksh's avatar
Level 1

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

i got that error : Missing required parameter for [Route: update] [URI: post/update/{post}] [Missing parameter: post].

and here's my route file

Route::middleware('auth')->group(function(){ Route::any('/home', [App\Http\Controllers\PostController::class, 'index'])->name('home');

    Route::get('/post/create', [App\Http\Controllers\PostController::class, 'create']);

    Route::post('post/store', [App\Http\Controllers\PostController::class, 'store'])->name('store');

    Route::get('post/edit/{post}', [App\Http\Controllers\PostController::class, 'edit']);

    Route::get('post/show/{post}', [App\Http\Controllers\PostController::class, 'show']);

    Route::put('post/update/{post}', [App\Http\Controllers\PostController::class, 'update'])->name('update');

    Route::delete('post/delete/{post}', [App\Http\Controllers\PostController::class, 'destroy']);

});
0 likes
34 replies
tykus's avatar

You have a route helper method in a form action?

<form action="{{ route('update', $post) }}" method="post">

But (probably) $post is actually null in that view. Can you show how that view, and the controller action that returns the view - likely PostController's edit method

laksh's avatar
Level 1

@tykus Here is the controller action

public function edit(post $post){

    return view('posts.edit');
}

public function update(post $post, Request $request){
    
    $request->validate([
       'tittle' => 'required',
        'body' => 'required'
    ]);
    
    $post->tittle = $request->tittle;
    $post->body = $request->body;
    
    $post->save();
    
    return redirect('/home')->with('success', 'post updated successfully');
}
laksh's avatar
Level 1

@tykus i am trying to edit the record here which was previously saved so it doesn't have to be null

tykus's avatar

@laksh you don't pass anything the the edit view

return view('posts.edit');

Pass the post:

return view('posts.edit', compact($post));

I don't know what the edit view looks like, so unsure how you might have been using that original update route there.

laksh's avatar
Level 1

@tykus Here's the edit view which am using

{{ __('Edit Post') }}
				<div class="card-body">
					@if(session('status'))
						<div class="alert alert-success">
							{{ session('status') }}
						</div>
					@endif
					
					@php( $posts = \App\Models\post::all() )
					
					<form action="{{ route('update') }}" method="post">
						@csrf
						
						@method('PUT')
						<div class="form-group">
							<label for="">Post Tittle</label>
							<input type="text" name="tittle" class="form-control">
						</div>
						
						<div class="form-group">
							<label for="">Post Body</label>
							<textarea name="body" id="" cols="30" rows="10" class="form-control" ></textarea>
						</div>
						
						<div class="form-group">
							<label for="">Publish At</label>
							@foreach($posts as $post)
							<input type="date" name="published_at" class="form-control" value="{{ date('y-m-d', strtotime($post->published_at)) }}">
							@endforeach
						</div>
						
						<button type="submit" class="btn btn-primary">Submit</button>
					
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
tykus's avatar

@laksh again the update route, as defined, needs a Post ID, but you are not passing a Post ID to the route helper method. I don't understand the intent of your form - you have an empty textarea and are iterating over all Posts to display a date type form input??? What are you trying to achieve here?

This would be a typical Edit/Update flow - where the subject Post is a single Post!

Route::get('post/edit/{post}', [App\Http\Controllers\PostController::class, 'edit']);
Route::put('post/update/{post}', [App\Http\Controllers\PostController::class, 'update'])->name('update');
public function edit(post $post)
{
    return view('posts.edit', compact('post'));
}

public function update(post $post, Request $request){
    
    $request->validate([
       'tittle' => 'required',
        'body' => 'required'
    ]);
    
    $post->tittle = $request->tittle;
    $post->body = $request->body;
    
    $post->save();
    
    return redirect('/home')->with('success', 'post updated successfully');
}
<form action="{{ route('update', $post) }}" method="post">
    @csrf
    @method('PUT')
    <div class="form-group">
        <label for="">Post Tittle</label>
        <input type="text" name="tittle" class="form-control">
    </div>
						
    <div class="form-group">
        <label for="">Post Body</label>
        <textarea name="body" id="" cols="30" rows="10" class="form-control" >{{ $post->body }}</textarea>
    </div>
						
    <div class="form-group">
        <label for="">Publish At</label>
        <input type="date" name="published_at" class="form-control" value="{{ date('y-m-d', strtotime($post->published_at)) }}">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>
laksh's avatar
Level 1

@tykus i got an error after changing into code

Missing required parameters for [Route: update] [URI: post/update/{post}] [Missing parameters: "id":2,"tittle":"new post","body":"testingg","published_at":null,"created_at":"2022-08-08T07:59:35.000000Z","updated_at":"2022-08-08T07:59:35.000000Z", "id":3,"tittle":"new body1","body":"1st blog","published_at":null,"created_at":"2022-08-08T08:07:48.000000Z","updated_at":"2022-08-08T08:07:48.000000Z", "id":4,"tittle":"xyz","body":"qwerty","published_at":"2022-08-02 00:00:00","created_at":"2022-08-08T08:13:12.000000Z","updated_at":"2022-08-08T08:13:12.000000Z"]. (View: D:\xampp\htdocs\demo\resources\views\posts\edit.blade.php)

tykus's avatar

@laksh what Laravel version are you using? It is unusual these days to explicitly pass the Post ID:

<form action="{{ route('update', $post->id) }}" method="post">
laksh's avatar
Level 1

@tykus i am using laravel 8 and i also used this way to pass id before but it doesn't work and givng me error

tykus's avatar
tykus
Best Answer
Level 104

@laksh did you pass the Post model to the edit view:

public function edit(post $post)
{
    return view('posts.edit', compact('post'));
}

If you did, check that you get a Post instance - that exists and has an id:

public function edit(post $post)
{
    dd($post);
    return view('posts.edit', compact('post'));
}
laksh's avatar
Level 1

@tykus yeah it has an id you can see

#fillable: array:3 [▶] #connection: "mysql" #table: "posts"

#primaryKey: "id"

#keyType: "int" +incrementing: true #with: [] #withCount: [] +preventsLazyLoading: false #perPage: 15 +exists: true +wasRecentlyCreated: false #escapeWhenCastingToString: false #attributes: array:6 [▶] #original: array:6 [▶] #changes: [] #casts: [] #classCastCache: [] #attributeCastCache: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] #relations: [] #touches: [] +timestamps: true #hidden: [] #visible: [] #guarded: array:1 [▶] #accessToken: null }

tykus's avatar

@laksh ok, so as a sanity check... (i) you are visiting the URL /post/edit/2; (ii) you are getting a valid Post instance from the database; (iii) you are passing that Post instance to the edit view template (iv) you are using that Post instance in the route helper method in the form action?

If you are doing all of these things, it should work (so long as $post is not being overwritten).

laksh's avatar
Level 1

@tykus sorry i couldn't understand what do you mean in your last line?

tykus's avatar

@laksh did you assign $post to something else in the view

laksh's avatar
Level 1

@tykus No. post is used only three times in form first where it is assigned then into action then in text area

Sinnbeck's avatar

@laksh I think it might be an idea if you posted what you currently have

tykus's avatar

Can you post full routes file, and the full edit view? I will assume that your controller is the same as what I posted earlier

laksh's avatar
Level 1

@tykus Here's my route file

Route::middleware('auth')->group(function(){
    Route::any('/home', [App\Http\Controllers\PostController::class, 'index'])->name('home');
    Route::get('/post/create', [App\Http\Controllers\PostController::class, 'create']);
    Route::post('post/store', [App\Http\Controllers\PostController::class, 'store'])->name('store');
    Route::get('post/edit/{post}', [App\Http\Controllers\PostController::class, 'edit']);
    Route::get('post/show/{post}', [App\Http\Controllers\PostController::class, 'show']);
    Route::put('post/update/{post}', [App\Http\Controllers\PostController::class, 'update'])->name('update');
    Route::delete('post/delete/{post}', [App\Http\Controllers\PostController::class, 'destroy']);
    
});
    


        
laksh's avatar
Level 1

@tykus and this is edit view

{{ __('Edit Post') }}
				<div class="card-body">
					@if(session('status'))
						<div class="alert alert-success">
							{{ session('status') }}
						</div>
					@endif
					
					@php( $posts = \App\Models\post::all() )
					
					<form action="{{ route('update', $posts->id) }}" method="post">
						@csrf
						
						@method('PUT')
						<div class="form-group">
							<label for="">Post Tittle</label>
							<input type="text" name="tittle" class="form-control">
						</div>
						
						<div class="form-group">
							<label for="">Post Body</label>
							<textarea name="body" id="" cols="30" rows="10" class="form-control" >{{ $post->body }}</textarea>
						</div>
						
						
						<button type="submit" class="btn btn-primary">Submit</button>
					
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
tykus's avatar

@laksh The form action is using a posts collection not the post instance

You do not need to be fetching all of the posts in the edit view it’s unnecessary

laksh's avatar
Level 1

@tykus can you please tell me how do i only fetch the content of related id using foreach

tykus's avatar

@laksh why do you want all of the posts in this view? What is “content with f related id”?

laksh's avatar
Level 1

@tykus no i want only one post at a time in this view but, i end up getting whole what's the error?

tykus's avatar

@laksh several times I have shown you that you must pass a single Post instance the the edit view template and use that Post instance within the route helper.

     {{ __('Edit Post') }} 
				<div class="card-body">
					@if(session('status'))
						<div class="alert alert-success">
							{{ session('status') }}
						</div>
					@endif
					
					<form action="{{ route('update', $post->id) }}" method="post">
						@csrf
						@method('PUT')
						<div class="form-group">
							<label for="">Post Tittle</label>
							<input type="text" name="tittle" class="form-control">
						</div>
						<div class="form-group">
							<label for="">Post Body</label>
							<textarea name="body" id="" cols="30" rows="10" class="form-control" >{{ $post->body }}</textarea>
						</div>
						<button type="submit" class="btn btn-primary">Submit</button>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
laksh's avatar
Level 1

@tykus yeah i am passing single instance like you tell but am getting error on this

Here's Error : Property [id] does not exist on this collection instance.

can you suggest me way to do that Thanks in advance.

Sinnbeck's avatar

@laksh Notice how @tykus removed this

@php( $posts = \App\Models\post::all() )

and how he has this (post)

<form action="{{ route('update', $post->id) }}" method="post">

instead of this (posts)

<form action="{{ route('update', $posts->id) }}" method="post">
laksh's avatar
Level 1

@Sinnbeck yeah i noticed that am passing (post) using ->with is it right way to do that?

Sinnbeck's avatar

@laksh It does the same. You can just pass it like Tykus have shown you earlier.. This is just a copy paste of his code

public function edit(post $post)
{
    return view('posts.edit', compact('post'));
}
laksh's avatar
Level 1

@Sinnbeck yeah it is worked but if i want to update only one at a time then it doesn't work it works only when we update both can you suggest what i am doing wrong

Sinnbeck's avatar

@laksh Sorry I dont know what you mean by "we update both". Both what?

laksh's avatar
Level 1

it's working now Thankyou

Sinnbeck's avatar

@laksh Good. Pick an answer by tykus and mark as best, as he gave the answers. I just reposted them :)

Please or to participate in this conversation.