前提
Laravel
Vue
できていること
Vueのaxiosで値をGetして、その値を表示させることはできた
つまづいていること(要件定義)
Getしてきた値をフォーム送信で送るため、自動でその値を入れたいのですが、
{{ user.id }}をv-modelに入れてみたり、v-modelをvalueに変えて入れてみたりしても
コンパイルエラーが出てうまくできません。
また、axios.post('/api/topics', {…………})でLaravel側に値を渡すのですが、
dataの「user_id: '',」を「user_id: user.id,」と書いてもうまく値を入れることができません。
どうすれば、この要件を達成することができるでしょうか?
お詳しい方、ご教示お願いします。
[参考]Laravel + SPA投稿アプリ作成方法
こちらのサイトでSPAの投稿アプリを作成しました。
投稿自体は確認済みです。
Laravel5.6とVue.jsで簡単なシングルページアプリケーション
参考までにフォームのFormのコンポーネントデータのみアップしておきます。
js
1<template lang="html"> 2 <div class="container"> 3 <div v-if="saved" class="alert alert-primary" role="alert"> 4 保存しました 5 </div> 6 <form> 7 <!-- 8 ここがaxios.get('/api/me')で取得している値。表示はされているので、値は確実に取れている。 9 この値を下のhiddenフォームで送りたい。もしくは何らかの形で送りたい。 10 --> 11 {{ user.id }} 12 <input type="hidden" class="form-control" id="TopicUserId" v-model="user_id"> 13 <div class="form-group"> 14 <label for="TopicTitle">タイトル</label> 15 <input type="text" class="form-control" id="TopicTitle" v-model="title"> 16 </div> 17 <div class="form-group"> 18 <label for="TopicContent">内容</label> 19 <textarea class="form-control" id="TopicContent" rows="3" v-model="content"></textarea> 20 </div> 21 <button type="submit" class="btn btn-primary" @click.prevent="create">登録</button> 22 </form> 23 </div> 24</template> 25 26<script> 27export default { 28 data: function() { 29 return { 30 saved: false, 31 // axios.get('/api/me')でGetしてきた値をuserの配列に格納 32 user: {}, 33 34 // 以下3点データベーステーブルのカラム 35 user_id: '', 36 title: '', 37 content: '', 38 } 39 }, 40 methods: { 41 create : function() { 42 // ここでLaravel側に値を渡す 43 axios.post('/api/topics', { 44 user_id: this.user_id, 45 title: this.title, 46 content: this.content, 47 }) 48 .then((res) => { 49 this.user_id = ''; 50 this.title = ''; 51 this.content = ''; 52 this.saved = true; 53 console.log('created'); 54 }); 55 } 56 }, 57 // axiosでGetしてきた値 58 created() { 59 axios.get('/api/me').then(res => { 60 this.user = res.data; 61 }); 62 } 63} 64</script>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/01 05:33