以下のサイトを参考にしました。
リンク内容
宜しくお願い致します。
###エラー
POST 500 (Internal Server Error)
###ログファイル
local.ERROR: Class 'App\Http\Controllers\User' not found {"userId":1,"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Class 'App\Http\Controllers\User' not found at /var/www/twitter_pj/app/Http/Controllers/FollowUserController.php:16)
###api.php
Route::post('/follow-users', 'FollowUserController@store'); Route::delete('/follow-users/{id}', 'FollowUserController@destroy');
###FollowUser.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; class FollowUser extends Model { protected $table = 'follow_users'; public $timestamps = false; protected $guarded = []; }
###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'); } public function followUsers() { return $this->belongsToMany(self::class, 'follow_users', 'user_id', 'followed_user_id') ->using(FollowUser::class); } /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'tweets' ]; /** * 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', ]; }
###FollowUserController.php
<?php namespace App\Http\Controllers; use App\FollowUser; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; class FollowUserController extends Controller { public function __construct() { // 認証が必要 $this->middleware('auth'); } // フォロー public function store(Request $request) { $followedUser = User::where('id', $request->id); FollowUser::Create([ 'user_id' => Auth::user()->id, 'followed_user_id' => $followedUser->id, ]); return response()->json(['result' => true]); } // フォロー解除 public function destroy($id) { $followedUser = User::find($id); $user = Auth::user(); $user->followUsers()->detach($followedUser->id); return response()->json(['result' => true]); } }
###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> <RouterLink class="tweet__overlay" :to="`/users/${user.id}/edit`" > <div class="list-group-item"> <span> 編集 </span> </div> </RouterLink> <!-- フォロー機能 --> <div> <button v-if="currentFollowing" type="button" class="btn btn-point btn-raised" @click="unfollow"> <div v-if="sending" class="spinner-border spinner-border-sm" role="status"> <span class="sr-only">Sending...</span> </div> <div v-else>フォロー中</div> </button> <button v-else type="button" class="btn btn-default btn-raised" @click="follow"> <div v-if="sending" class="spinner-border spinner-border-sm" role="status"> <span class="sr-only">Sending...</span> </div> <div v-else> フォローする <i class="material-icons">add</i> </div> </button> </div> </div> </nav> <div class="tweet-list"> <div class="grid"> <UserTweet class="grid__item" v-for="tweet in user.tweets" :key="tweet.id" :item="tweet" /> </div> </div> </div> </template> <script> import { OK } from '../util' import UserTweet from '../components/UserTweet.vue' import axios from 'axios' export default { components: { UserTweet, }, props: { type: String, required: true, type: Boolean, default: false, id: { type: String, required: true }, }, data () { return { user: null, currentFollowing: this.following, sending: false, } }, 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.tweets }, async follow() { if (this.sending) { return } this.sending = true const data = { id: this.id } await axios.post('/api/follow-users', data) this.currentFollowing = true this.sending = false }, async unfollow() { if (this.sending) { return } this.sending = true await axios.delete(`/follow-users/${this.id}`) this.currentFollowing = false this.sending = false } }, watch: { $route: { async handler () { await this.fetchUser() }, immediate: true } } } </script>
回答1件
あなたの回答
tips
プレビュー