前提・実現したいこと
PHP(laravel)でinstagramのようなシステムを作っています。
クリエイター(ファッションデザイナー等)と
モデル(ファッションモデルなど)専用のSNSアプリなので、
MVCフレームワークをクリエイターとモデルで2つに分けて作成しています。
「イイネ機能????」を実装中に以下のエラーメッセージが発生しました。
よろしくお願いします。
発生している問題・エラーメッセージ
エラーメッセージ
######"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'creator_likes.creator_post_id' in 'where clause' (SQL: select * from creator_likes
where creator_likes
.creator_post_id
= 7 and creator_likes
.creator_post_id
is not null) (View: /home/ec2-user/environment/myapp/resources/views/post/like_text.blade.php) (View: /home/ec2-user/environment/myapp/resources/views/post/like_text.blade.php) ◀"
カラムが見つからないというエラーですが、
なぜ[creator_post_id」というカラムを
探してしまっているのかがわかりません。
教えてください。よろしくお願いします。
該当のソースコード
creator_likes_table.php(テーブル)
public function up() { Schema::create('creator_likes', function (Blueprint $table) { $table->increments('id'); $table->integer('post_id'); $table->integer('user_id'); $table->timestamps(); }); }
CreatorLike.php (モデル)
class CreatorLike extends Model { public function user() { return $this->belongsTo('App\User'); } public function post() { return $this->belongsTo('App\CreatorPost'); } }
creator_table_php (テーブル)
public function up() { Schema::create('creator_posts', function (Blueprint $table) { $table->increments('id'); $table->string('caption'); $table->integer('user_id'); $table->timestamps(); }); }
CreatorPost.php (モデル)
class CreatorPost extends Model { public function user() { return $this->belongsTo('App\User'); } public function likes() { return $this->hasMany('App\CreatorLike'); } public function likedBy($user) { return CreatorLike::where('user_id', $user->id)->where('post_id', $this->id); } }
User.php (モデル)
public function likes() { if($this->role === 'model'){ return $this->hasMany('App\ModelLike'); } if($this->role === 'creator'){ return $this->hasMany('App\CreatorLike'); } }
CreatorLikesController.php (コンローラー)
public function store(Request $request) { $like = new CreatorLike; $like->post_id = $request->post_id; $like->user_id = Auth::user()->id; $like->save(); return redirect()->url()->current(); } public function destroy(Request $request) { $like = CreatorLike::find($request->like_id); $like->delete(); return redirect()->url()->current(); }
index.php (ビュー)
<div class="card-body"> <div class="row parts"> <div class="like-icon-post-{{ $post->id }}"> @if ($post->likedBy(Auth::user())->count() > 0 ) <a class="loved hide-text" data-remote="true" rel="nofollow" data-method="DELETE" href="creator/likes/{{ $post->likedBy(Auth::user())->firstOrFail()->id }}">いいねを取り消す</a> @endif </div> <a class="comment" href="#"></a> </div> <div id="like-text-post-{{ $post->id }}"> @include('post.like_text') </div>
like_text.php (ビュー)
<strong> @foreach ($post->likes as $like) @if ($loop->count == 1) {{ $like->user->name }} </strong> が「いいね!」しました @elseif ($loop->last) </strong>and<strong> {{ $like->user->name }}</strong> が「いいね!」しました @elseif (!$loop->first) </strong>and {{ $loop->count - 1 }}人 が「いいね!」しました @break @else {{ $like->user->name }}, @endif @endforeach
補足情報(FW/ツールのバージョンなど)
PHP 7.2.31
Laravel Framework 5.5.49
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。