質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Q&A

解決済

2回答

1883閲覧

formのinputのhidden属性の値を取得したい

hirofumiimoto

総合スコア12

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

0グッド

0クリップ

投稿2020/04/07 08:14

前提・実現したいこと

現在、日本史の質問ウェブアプリを作成しています。
formの中にhidden属性のinputを用意しているのですが、answer_idという値を取得することができません。
どうして値を取得できていないのか、またどのようにすれば取得できるかご教授いただければ幸いです。

発生している問題・エラーメッセージ

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'answer_id' cannot be null (SQL: insert into `replies` (`answer_id`, `content`, `get_user_id`, `post_user_id`, `updated_at`, `created_at`) values (?, うん, ?, 5, 2020-04-07 07:48:15, 2020-04-07 07:48:15))

detail.blade.php

@foreach($answers as $answer) <div class="question"> <div class="question_title"> <ul class="list_unstyled" style="list-style: none;"> <li>解決済み</li> <li>未解決</li> </ul> </div> <div class="" style="padding-right: 500px;"> <span>質問の詳細</span> </div> <div style="border-top:solid #ddd; border-bottom:solid #ddd;" class="question_content center-block mb-30"> <span>{{$answer->question->content}}</span> </div> </div> <div class="answer"> <div style="padding-right: 540px;"> <span>回答</span> </div> <div style="border-top:solid #ddd; border-bottom:solid #ddd;" class="answer_content center-block mb-30"> <span>{{$answer->user->name}}</span> <span>{{$answer->content}}</span> </div> </div> <div class="reply"> <div class=" reply_content center-block mb-30" style="padding-right: 540px; border-bottom: solid #ddd;"> <span>返信</span> </div> @foreach($reply_list[$answer->id] as $reply) <div class="comment_reflection mb-30"> <span>{{$reply->getPostUser()->name}}</span> <span>{{$reply->content}}</span> </div> @endforeach </div> //ここから質問の部分 @if(AUth::check()) @if($question->user_id === Auth::user()->id || $answer->user_id === Auth::user()->id) <div class="reply_form mt-30 reply_content center-block mb-30" style="border-bottom: solid #ddd;"> <form class="mt-30" action="/detail/reply_post" method="POST"> <input type="hidden" name="answer_id" value="{{isset($answer->id)}}"> @csrf <textarea name="content" id="" cols="50" rows="4" value="{{$reply->content}}"></textarea> <input type="submit" value="コメントボタン" class="btn btn-primary mt-40"> </form> </div> @endif @endif @endforeach

###QuestionControllerのdetailメソッドとreplyメソッド

public function detail(Request $request){ $question = Question::find($request->id); $answers = $question->answers;//questionモデルのanswersメソッドを引用 $reply_list = []; foreach($answers as $answer){ $replies = $answer->replies; $reply_list[$answer->id] = $replies; } return view("question.detail", compact("answers", "question", "reply_list")); } public function reply(Request $request){ if(Auth::check()){ $reply = new Reply; $reply->answer_id = $request->answer_id; $reply->content = $request->content; $reply->get_user_id = $request->get_user_id; $reply->post_user_id = Auth::user()->id; $reply->save(); }else{ return redirect("/login"); } return redirect("/detail/{{$question->id}}"); }

###create_replies_table.php

public function up() { Schema::create('replies', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger("answer_id")->unsigned(); $table->foreign("answer_id")->references("id")->on("answers"); $table->text("content"); $table->bigInteger("post_user_id")->unsigned(); $table->foreign("post_user_id")->references("id")->on("users"); $table->bigInteger("get_user_id")->unsigned(); $table->foreign("get_user_id")->references("id")->on("users"); $table->timestamps(); }); }

###answersテーブルとrepliesテーブル
イメージ説明

試したこと

リレーションを用いてreply.phpに
public function getAnswer(){
return $this->belongsTo("App/Answer");
}
とし、detail.blade.phpにて
<input type="hidden" name="answer_id" value="$reply->getAnswer->id">
としましたがエラーを解消することはできませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

isset($answer->id)
は true / false
なんですが意図してそう書いてますでしょうか

投稿2020/04/07 08:43

mikkame

総合スコア5036

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hirofumiimoto

2020/04/07 17:51

teratailの他の方がisset()を用いることでエラーが解消されたと書いていたので、使いました。なので、意図して書いたわけではございません。
hirofumiimoto

2020/04/07 19:20

isset()を消すとうまくいきました! ありがとうございました!
guest

0

自己解決

isset()を消すとうまく機能しました!

投稿2020/04/07 19:20

hirofumiimoto

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問