Skip to main content

Post Edit Form: Route with Parameter

Premium
7 min read

Now let's work on editing the post. This is going to be kind of a repeating thing, similar to what we did with the create form but with minor changes.

edit form


Edit Page Route

So first, let's add a link to the Edit page in the posts list.

resources/js/components/Posts/Index.vue:

<template>
<div class="overflow-hidden overflow-x-auto p-6 bg-white border-gray-200">
<div class="min-w-full align-middle">
// ...
<table class="min-w-full divide-y divide-gray-200 border">
<thead>
<tr>
// ...
<th class="px-6 py-3 bg-gray-50 text-left">
<div class="flex flex-row items-center justify-between cursor-pointer" @click="updateOrdering('created_at')">
<div class="leading-4 font-medium text-gray-500 uppercase tracking-wider" :class="{ 'font-bold text-blue-600': orderColumn === 'created_at' }">
Created at
</div>
<div class="select-none">
<span :class="{
'text-blue-600': orderDirection === 'asc' && orderColumn === 'created_at',
'hidden': orderDirection !== '' && orderDirection !== 'asc' && orderColumn === 'created_at',
}">&uarr;</span>
<span :class="{
'text-blue-600': orderDirection === 'desc' && orderColumn === 'created_at',
'hidden': orderDirection !== '' && orderDirection !== 'desc' && orderColumn === 'created_at',
}">&darr;</span>
</div>
</div>
</th>
<th class="px-6 py-3 bg-gray-50 text-left">
<span class="text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider">Actions</span>
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200 divide-solid">
<tr v-for="post in posts.data">
// ...
<td class="px-6 py-4 whitespace-no-wrap text-sm leading-5 text-gray-900">
{{ post.created_at }}
</td>
<td class="px-6 py-4 whitespace-no-wrap text-sm leading-5 text-gray-900">
<router-link :to="{ name: 'posts.edit', params: { id: post.id } }">Edit</router-link>
</td>
</tr>
</tbody>
</table>
 
<TailwindPagination :data="posts" @pagination-change-page="page => getPosts(page, selectedCategory)" class="mt-4" />
</div>
</div>
</template>
 
<script setup>
// ...

edit action link

Now, for the edit link, we added a parameter id. Next, we need to add a route with this parameter. The syntax for adding a parameter is...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 41 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

R
rochmadnf ✓ Link copied!

"There's one image that is not available, only the text 'empty edit form' is written. Is it important or not?"

N
Nerijus ✓ Link copied!

It wasn't important screenshot. Fixed the link.

RJ
Remy Jay ✓ Link copied!

In the edit form, the modified content field from the PostResource is shown. Is there a way to display the full content in the edit form but still keep the truncated content in the post index view? Could you create a PostEditResource and return the full values of the Post? It seems clicking save updates the content field to be exactly that truncated value.

N
Nerijus ✓ Link copied!

You can just add another value to the

A
aries10 ✓ Link copied!

@Nerijus your reply seems truncated as well... I'd like to know how to update the full value of content shown in the Edit.vue

N
Nerijus ✓ Link copied!

Tell more what you want

A
aries10 ✓ Link copied!

As Remy Jay mentioned, how to keep full value of content without limiting the words.

N
Nerijus ✓ Link copied!

Add whatever values you want to a api resource and name it whatever you

H
haritjahjo ✓ Link copied!

Why I can't update post and error (Edit.vue): updatePost is not an function?

P
PatrickSIngh ✓ Link copied!

Why I can't update post and error (Edit.vue): updatePost is not an function?

Because you have to add the function in Edit.vue: const { post, getPost, updatePost, validationErrors, isLoading } = usePosts()

N
Nillos ✓ Link copied!

how to update a file using put? axios.put('/api/posts', post) - PostController $request = It works, but file is empty

let serializedPost = new FormData() for (let item in post) { if (post.hasOwnProperty(item)) {serializedPost.append(item, post[item])}} ... axios.put('/api/posts', serializedPost) - PostController $request->all() = empty array

M
mcpacific ✓ Link copied!

I was getting, "Uncaught TypeError: ctx.updatePost is not a function at Edit.vue:2:28" in Dev Console. In resources/js/components/Posts/Edit.vue, shouldn't

const { post, getPost, validationErrors, isLoading } = usePosts() be instead const { post, getPost, updatePost, validationErrors, isLoading } = usePosts() ? I added updatePost and it now works fine.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.