前提・実現したいこと
Laravelで勉強用にInstagramのクローンアプリを作成しています。
投稿した画像に対して、他のユーザーや自分がコメントを投稿できる機能を実装したいのですが、下記のエラーが出ており、自分の思っていることが実現できなくて困っています。
解決方法をよろしくお願いします!
発生している問題・エラーメッセージ
views/contents/show.blade.phpに設置しているコメント投稿用フォームにコメントを入力して、投稿する際にこちらのエラーが出てきます。
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'content_id' cannot be null (SQL: insert into `comments` (`user_id`, `content_id`, `message`, `updated_at`, `created_at`) values (1, , 投稿するコメントの内容, 2019-03-16 01:05:40, 2019-03-16 01:05:40))
該当のソースコード
2019_03_03_102820_create_comments_table.php
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateCommentsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('comments', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->integer('user_id')->unsigned()->index(); 19 $table->integer('content_id')->unsigned()->index(); 20 $table->string('message'); //コメントのメッセージカラム 21 $table->timestamps(); 22 23 $table->foreign('user_id')->references('id')->on('users'); 24 $table->foreign('content_id')->references('id')->on('contents'); 25 }); 26 } 27 28 /** 29 * Reverse the migrations. 30 * 31 * @return void 32 */ 33 public function down() 34 { 35 Schema::dropIfExists('comments'); 36 } 37} 38
Content.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Content extends Model 8{ 9 protected $fillable = ['caption','toShareImg','user_id']; 10 11 public function user() 12 { 13 return $this->belongsTo(User::class); 14 } 15 16 public function comments() 17 { 18 return $this->hasMany(Comment::class); 19 } 20} 21
Comment.php
php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Comment extends Model 8{ 9 protected $fillable = ['user_id','content_id','message']; 10 11 public function content() 12 { 13 $this->belongsTo(Content::class); 14 } 15} 16
CommentsController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6 7use App\User; 8use App\Content; 9use App\Comment; 10 11class CommentsController extends Controller 12{ 13 public function store(Request $request) 14 { 15 $this->validate($request,[ 16 'message' => 'required|max:191', //下記↓にここの部分について言及しています 17 ]); 18 19 20 Comment::create([ 21 'user_id' => $request->user()->id, 22 'content_id' => $request->content_id, 23 'message' => $request->message, 24 ]); 25 26 return redirect()->back(); 27 } 28 29 public function show($content_id) 30 { 31 $content = Content::find($content_id); 32 $user = User::where('id',$content->user_id)->first(); 33 34 return view('contents.show',compact('content','user')); 35 } 36}
views/contents/show.blade.php
php
1@extends('layouts.app') 2 3@section('content') 4 5 <h1>投稿詳細</h1> 6 7 <img src="/storage/{{ $content->toShareImg }}" alt="" width="400px"> 8 <p>{{ $content->caption }}</p> 9 10 {!! link_to_route('contents.edit','編集',['id' => $content->id]) !!} 11 12 {!! Form::model($content, ['route' => ['contents.destroy',$content->id], 'method' => 'delete' ]) !!} 13 {!! Form::submit('削除') !!} 14 {!! Form::close() !!} 15 16 {!! Form::open(['route' => ['comments.store',$content->id]]) !!} 17 18 {!! Form::label('message','コメントを入力してください') !!} 19 {!! Form::textarea('message',old('message')) !!} 20 21 {!! Form::submit('コメントする') !!} 22 23 {!! Form::close() !!} 24 25 @forelse($content->comments as $comment) 26 27 {!! $user->name !!} 28 {!! nl2br(e($comment->message)) !!} 29 30 @empty 31 32 <p>コメントはまだありません</p> 33 34 @endforelse 35 36@endsection
web.php
php
1Route::resource('comments','CommentsController',['only' => ['store']]);
試したこと
まず、この機能を実装するにあたって、こちらを参考にこの機能のコーディングをしています。
CommentsController.phpのバリデーションの部分でcontent_idもrequiredと記述したいのですが、テストする際にcontent_idが無いよ!!というバリデーションエラーがでて進まなかったので、確認のためわざと消して試したあと、上記のエラー文が出てきました。確かにバリデーションエラーの通りなのでした。
この後の対処の方法をエラー文でググり、いろいろなサイトを見てみたのですが、自分と同じ状況でなやんでいるようなものを見つけられなかったためコチラで質問してみました。
僕の思っていることもここに記入しておくと、content_idがnullの状態でもコメントが投稿できるようにしてしまうと、ありもしない投稿に対しコメントをしてしまうのではないか、と思い、content_idはnullを許可したくありません。もし、思い込みでこのように思ってしまっているのでしたら指摘もお願いします!
補足情報(FW/ツールのバージョンなど)
Laravel Framework 5.5.45
AWS Cloud9を使用しています

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/03/16 14:42
2019/03/18 00:56
退会済みユーザー
2019/03/18 02:24