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

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

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

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

Q&A

解決済

1回答

747閲覧

入力確認画面のstoreメソッドが実行されない

monji

総合スコア11

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Laravel 5

Laravel 5は、PHPフレームワークLaravelの最新バージョンで、2014年11月に発表予定です。ディレクトリ構造がが現行版より大幅に変更されるほか、メソッドインジェクションやFormRequestの利用が可能になります。

0グッド

0クリップ

投稿2019/07/13 06:24

編集2019/07/14 06:58

実現したいこと

laravelで新規作成した内容を確認画面で表示して、ボタンを押すと新規作成が完了し、一覧画面に戻るという処理を作りたいです。

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

確認画面のボタンを押すとコントローラ内のstoreメソッドが実行されるはずが、なぜかshowメソッドが呼ばれ、エラーになってしまっています。

Trying to get property of non-object (View: resources/views/user/question/show.blade.php)

###routes/web.php

Route::get('question/mypage', ['as' => 'question.mypage', 'uses' => 'QuestionController@showMyPage']); Route::post('question/confirm', ['as' => 'question.confirm', 'uses' => 'QuestionController@confirm']); Route::resource('question', 'QuestionController'); Route::get('comment', ['as' => 'comment.index', 'uses' => 'CommentController@index']); Route::post('comment/store', ['as' => 'comment.store', 'uses' => 'CommentController@store']); php artisan route:list | | POST | question | question.store | App\Http\Controllers\QuestionController@store | web,auth | | | GET|HEAD | question | question.index | App\Http\Controllers\QuestionController@index | web,auth | | | POST | question/confirm | question.confirm | App\Http\Controllers\QuestionController@confirm | web,auth | | | GET|HEAD | question/create | question.create | App\Http\Controllers\QuestionController@create | web,auth | | | GET|HEAD | question/mypage | question.mypage | App\Http\Controllers\QuestionController@showMyPage | web,auth | | | GET|HEAD | question/{question} | question.show | App\Http\Controllers\QuestionController@show | web,auth | | | PUT|PATCH | question/{question} | question.update | App\Http\Controllers\QuestionController@update | web,auth | | | DELETE | question/{question} | question.destroy | App\Http\Controllers\QuestionController@destroy | web,auth | | | GET|HEAD | question/{question}/edit | question.edit | App\Http\Controllers\QuestionController@edit

QuestionController

1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Http\Controllers\Controller; 7use Illuminate\Support\Facades\Auth; 8use App\Models\Question; 9use App\Models\TagCategory; 10use App\Http\Requests\QuestionsRequest; 11 12class QuestionController extends Controller 13{ 14 private $question; 15 private $tagCategory; 16 17 public function __construct(Question $question, TagCategory $tagCategory) 18 { 19 $this->question = $question; 20 $this->tagCategory = $tagCategory; 21 $this->middleware('auth'); 22 } 23 24 public function index() 25 { 26 $questions = $this->question->all(); 27 return view('user.question.index', compact('questions')); 28 } 29 30 public function create() 31 { 32 $tagCategoryNames = $this->tagCategory->all(); 33 return view('user.question.create', compact('tagCategoryNames')); 34 } 35 36 public function store(QuestionsRequest $request) 37 { 38 $input = $request->all(); 39 $input['user_id'] = Auth::id(); 40 $this->question->fill($input)->save(); 41 dd($input); 42 return redirect()->route('question.index'); 43 } 44 45 public function show($id) 46 { 47 $question = $this->question->find($id); 48 return view('user.question.show', compact('question')); 49 } 50 51 public function edit($id) 52 { 53 $question = $this->question->with(['tagCategory'])->find($id); 54 $tagCategoryNames = $this->tagCategory->all()->pluck('name', 'id'); 55 return view('user.question.edit', compact('question', 'tagCategoryNames')); 56 } 57 58 public function update(QuestionsRequest $request, $id) 59 { 60 $input = $request->all(); 61 $this->question->find($id)->fill($input)->save(); 62 return redirect()->to('question'); 63 } 64 65 public function destroy($id) 66 { 67 $question = $this->question->find($id)->delete(); 68 return redirect()->route('question.mypage'); 69 } 70 71 public function showMyPage() 72 { 73 $questions = $this->question->all(); 74 return view('user.question.mypage', compact('questions')); 75 } 76 77 public function confirm(QuestionsRequest $request) 78 { 79 $question = $request->all(); 80 $tagCategory = $this->tagCategory->find($question['tag_category_id']); 81 return view('user.question.confirm', compact('question','tagCategory')); 82 } 83}

###confirm.blade.php

@extends ('common.user') @section ('content') <h2 class="brand-header">投稿内容確認</h2> <div class="main-wrap"> <div class="panel panel-success"> <div class="panel-heading"> {{ $tagCategory->name }}&nbsp;&nbsp;の質問 </div> <div class="table-responsive"> <table class="table table-striped table-bordered"> <tbody> <tr> <th class="table-column">Title</th> <td class="td-text">{{ $question['title'] }}</td> </tr> <tr> <th class="table-column">Question</th> <td class='td-text'>{{ $question['content'] }}</td> </tr> </tbody> </table> </div> </div> <div class="btn-bottom-wrapper"> {!! Form::open(['route' => 'question.store', 'method' => 'POST']) !!} {!! Form::input('hidden', 'user_id', null) !!} {!! Form::input('hidden', 'tag_category_id', null) !!} {!! Form::input('hidden', 'title', null) !!} {!! Form::input('hidden', 'content', null) !!} {!! Form::button('<i class="fa fa-check" aria-hidden="true"></i>', ['class' => 'btn btn-success', 'type' => 'submit']) !!} {!! Form::close() !!} </div> </div> @endsection

試したこと

formタグをformファサードに変更

補足情報(FW/ツールのバージョンなど)

php 7.1.119
laravel 5.6.27

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

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

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

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

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

guest

回答1

0

ベストアンサー

問題切り分けとして、Formライブラリを使わずに、通常のformタグとinputタグで試してみると解決の糸口になるかもしれません。
Laravel 5.6 ルーティング

また、何かキャッシュがあるかもしれないので、route:clearなども試してみてください。
Laravel キャッシュクリア系コマンドなど

投稿2019/07/14 08:48

aro10

総合スコア4106

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

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

monji

2019/07/14 11:59

回答ありがとうございます。キャッシュクリア、formタグに直してみたところ、一覧画面には戻るのですが、storeメソッドが実行されず、入力した内容がデータベースに反映されませんでした。
monji

2019/07/14 12:30

追記>storeメソッドが実行されていないと書いてしまいましたが、ちゃんと保存され、一覧にも表示されていました!ありがとうございます!
aro10

2019/07/15 01:44

????
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問