Reputation: 31
I want to retrieve the information for the edition of my article but I come across an error
My controller
public function show($id){
return new PostResource(post::where('id',$id)->findOrFail($id));
}
export default {
data() {
return {
form: new Form({
id: '',
title: '',
})
}
},
methods:{
updateItem() {
this.form.put('/api/orders/posts/' + this.form.id)
....
},
created(){
axios.get(`/api/orders/posts/${id}`).then(({data}) => this.form.fill(data));
}
}
[Vue warn]: Error in created hook: "ReferenceError: id is not defined"
found in
---> <UpdatePage> at resources/js/components/user/orders/post/Updatepage.vue
<Root> app.js:112186:15
ReferenceError: "id is not defined"
Upvotes: 2
Views: 261
Reputation: 299
I think you should try to replace that line
axios.get(`/api/orders/posts/${id}`).then(({data}) => this.form.fill(data));
By that
axios.get(`/api/orders/posts/${this.$router.params.id}`).then(({data}) => this.form.fill(data));
It will look for an id
route parameter (you must change the name of that parameter if you set an other name than id
. I would also call that from mounted
instead of created
(mounted
is run before and will avoid to see an empty form).
Moreover, in your controller, you could write a simpler form of your code :
public function show($id){
return new PostResource(post::findOrFail($id));
}
Or even
public function show(post $post){
return new PostResource($post);
}
If the right configuration is set.
Let me know if it helped you :)
Upvotes: 0
Reputation: 349
try this
axios.get(`/api/orders/posts/${this.$router.params.id}`).then(({data}) => this.form.fill(data.data));
Upvotes: 1