Ich versuche, einige Daten in die Datenbank mit Vuejs und Laravel zu posten. Unten sind die KodeWie bekomme ich die Daten in vuejs?
HTML-Teil:
{{ csrf_field() }}
<div class="field mini">
<textarea required v-model="newComment.text" placeholder="Write a comment" id="comment_text" name="comment_text"></textarea>
</div>
<button class="ui blue labeled submit icon button" @click.prevent="postComment()">
<i class="icon edit"></i> Add Reply
</button>
der Vuejs Teil
<script>
Vue.component('comments',{
template: '#comment-vue-template',
data:() => {
return {
comments: [],
newComment: {
'text': ''
}
}
},
created: function() {
this.getComments();
},
methods: {
getComments() {
this.$http.get('/comments').then(response => {
this.comments = response.body
});
setTimeout(this.getComments,1000);
},
postComment() {
this.$http.post('/comments').then(response => {
this.newComment = {
'text': ''
};
this.getComments();
})
}
}
});
new Vue({
el:'#app',
});
</script>
die Route Teil (web.php)
Route::resource('comments', 'CommentsController');
Routelist
| | POST | comments | comments.store | App\Http\Controllers\[email protected] | web,auth |
| | GET|HEAD | comments | comments.index | App\Http\Controllers\[email protected] | web,auth |
| | GET|HEAD | comments/create | comments.create | App\Http\Controllers\[email protected] | web,auth |
| | GET|HEAD | comments/{comment} | comments.show | App\Http\Controllers\[email protected] | web,auth |
| | PUT|PATCH | comments/{comment} | comments.update | App\Http\Controllers\[email protected] | web,auth |
| | DELETE | comments/{comment} | comments.destroy | App\Http\Controllers\[email protected] | web,auth |
| | GET|HEAD | comments/{comment}/edit | comments.edit | App\Http\Controllers\[email protected] | web,auth
und die CommentsController
public function store(Request $request)
{
$owner = Auth::User();
$comment = new Comment;
$comment->posted = Carbon::now();
$comment->text = $request->comment_text;
$comment->owner = array('id'=>$owner->id, 'name'=>$owner->name, 'avatar'=>$owner->avatar);
$comment->save();
}
habe ich versucht, die api Route und die normale Route, aber es funktioniert nicht. i halten
status bekommen: "Internal Server Error" url: "/ Kommentare"
selbst wenn die Ressource Route in der web.php vorhanden ist. Die Daten werden jedoch korrekt geladen. Wo ist der Fehler bitte. Vielen Dank im Voraus.
Sie scheinen keine Daten in Ihrer Post-Anfrage zu senden. –
@RossWilson i hinzugefügt 'http: { Header: { 'X-CSRF-token':. Document.querySelector ('# token') getAttribute ('value') } }' auf das Skript und '< meta name = "Token" id = "Token" Wert = "{{csrf_token()}}"> 'zum Header. Jetzt bekomme ich keinen Fehler, aber der Inhalt von newComment.text (textarea) wird nicht in der Datenbank veröffentlicht. der Besitzer und andere Felder werden jedoch gespeichert. –
@Ross Wilson Danke :) –