vue-devtoolsで確認するとuserの詳細はdataとしてありvueに表示されるのですが、そのuserに紐づくtweetsがdataにありません。
宜しくお願い致します。
###User.php
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * リレーションシップ - tweetsテーブル * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function tweets() { return $this->hasMany('App\Tweet'); } /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'email_verified_at', 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; protected $visible = [ 'id', 'name', 'email', 'tweets', ]; }
###UserController.php
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; class UserController extends Controller { public function __construct() { // 認証が必要 $this->middleware('auth')->except(['show']); } /** * user詳細 * @param string $id * @return User */ public function show(string $id) { $user = User::where('id', $id)->with(['tweets'])->first(); return $user ?? abort(404); } }
###UserDetail.vue
<template> <div v-if="user" class="tweet-detail"> <nav class="panel panel-default"> <div class="list-group"> <img src="" alt="icon" class="img-circle"> <div class="list-group-item"> <span> {{ user.name }}さん </span> </div> </div> </nav> <div class="tweet-list"> <div class="grid"> <Tweet class="grid__item" v-for="tweet in user.tweets" :key="tweet.id" :item="tweet" :id="tweet.id" /> </div> </div> </div> </template> <script> import { OK } from '../util' import Tweet from '../components/Tweet.vue' export default { components: { Tweet }, props: { id: { type: String, required: true }, }, data () { return { user: null, } }, methods: { async fetchUser () { const response = await axios.get(`/api/users/${this.id}`) if (response.status !== OK) { this.$store.commit('error/setCode', response.status) return false } this.user = response.data this.user.tweets = response.data.data } }, watch: { $route: { async handler () { await this.fetchUser() }, immediate: true } } } </script>
###Tweet.vue
<template> <div v-if="item" class="col"> <nav class="panel panel-default"> <div class="list-group"> <img src="" alt="icon" class="img-circle"> <RouterLink class="tweet__overlay" :to="`/users/${item.owner.id}`" :title="`View the user detail`" :item="item.owner" > <div class="list-group-item"> <span> {{ item.owner.name }}さん </span> </div> </RouterLink> <RouterLink class="tweet__overlay" :to="`/tweets/${item.id}`" :title="`View the tweet by ${item.owner.name}`" > <div class="list-group-item"> <span> {{ item.text }} </span> </div> </RouterLink> <div class="tweet__controls"> <button class="tweet__action tweet__action--like" title="Like tweet" > <i class="icon ion-md-heart"></i>12 </button> </div> </div> </nav> </div> </template> <script> export default { props: { item: { type: Object, required: true }, } } </script>
###api.php
<?php use Illuminate\Http\Request; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ // 会員登録 Route::post('/register', 'Auth\RegisterController@register')->name('register'); // ログイン Route::post('/login', 'Auth\LoginController@login')->name('login'); // ログアウト Route::post('/logout', 'Auth\LoginController@logout')->name('logout'); // ログインユーザー Route::get('/user', function () { return Auth::user(); })->name('user'); // tweet投稿 Route::post('/tweets', 'TweetController@create')->name('tweet.create'); // tweet一覧 Route::get('/tweets', 'TweetController@index')->name('tweet.index'); // tweet詳細 Route::get('/tweets/{id}', 'TweetController@show')->name('tweet.show'); // user詳細 Route::get('/users/{id}', 'UserController@show')->name('user.show');
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/17 14:50