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

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

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

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

Q&A

解決済

1回答

752閲覧

Laravel5.7系でブレード機能のUndefined variableのエラーを消したい。

amaturePy

総合スコア131

Laravel 5

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

0グッド

0クリップ

投稿2020/02/01 03:48

編集2020/02/01 04:09

Laravel5.7系でブレード機能を活用した入力欄を作成してます。
そこで「Undefined variable: chat_node」のエラーが出てしまいます。
コントローラー側のcreateメソッドにあるビューに渡してる変数名は$chat_nodeで
ビュー側の変数名と一致していると思うのですが、上記エラーが出てしまいます。
今回初めてHTMLからブレードの機能を使うため、理解しきれてない部分もありますが、
アドバイス頂けるとありがたいです。

chat_nodes.blade.php @extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-md-center"> <div class="col-6"> <form action="{{ url('chat_nodes/' . $chat_node->id) }}" method="post" class="form-group"> {{ csrf_field()}} {{ Form::label('id', 'ノード番号', ['class' => 'mt-3']) }} {{ Form::text('id', $chat_node->id, null, ['class' => 'form-control']) }} @if ($errors->has('id')) <span class="error text-danger">{{ $errors->first('id') }}</span> @endif {{ Form::label('platform', 'SNS', ['class' => 'mt-3']) }} {{ Form::select('platform', ['LINE', 'WeChat', 'Messanger', 'Kakao Talk'], null, ['class' => 'form-control']) }} {{ Form::label('type', 'ノードタイプ', ['class' => 'mt-3']) }} {{ Form::text('type', $chat_node->type, null, ['class' => 'form-control']) }} @if ($errors->has('type')) <span class="error text-danger">{{ $errors->first('type') }}</span> @endif {{ Form::label('title', 'タイトル', ['class' => 'mt-3']) }} {{ Form::text('title', $chat_node->title, null, ['class' => 'form-control']) }} @if ($errors->has('title')) <span class="error text-danger">{{ $errors->first('title') }}</span> @endif {{ Form::label('image', '画像', ['class' => 'mt-3']) }} {{ Form::text('image', $chat_node->image, null, ['class' => 'form-control']) }} @if ($errors->has('image')) <span class="error text-danger">{{ $errors->first('image') }}</span> @endif {{ Form::label('buttton_1', '選択肢1', ['class' => 'mt-3']) }} {{ Form::text('button_1', $chat_node->button_1, null, ['class' => 'form-control']) }} @if ($errors->has('button_1')) <span class="error text-danger">{{ $errors->first('button_1') }}</span> @endif {{ Form::label('button_2', '選択肢2', ['class' => 'mt-3']) }} {{ Form::text('button_2', $chat_node->button_2, null, ['class' => 'form-control']) }} @if ($errors->has('button_2')) <span class="error text-danger">{{ $errors->first('button_2') }}</span> @endif {{ Form::label('button_3', '画像', ['class' => 'mt-3']) }} {{ Form::text('button_3', $chat_node->button3, null, ['class' => 'form-control']) }} @if ($errors->has('button_3')) <span class="error text-danger">{{ $errors->first('button_3') }}</span> @endif {{ Form::label('creative_type', 'クリエイティブタイプ', ['class' => 'mt-3']) }} {{ Form::select('platform', [10 => '10', 20 => '20', 30 => '30', 40 => '40', 50 => '50', 60 => '60' , 70 => '70', 80 => '80', 90 => '90'], null, ['class' => 'form-control']) }} <div class="mt-3">{{ Form::submit('作成', ['class' => 'btn btn-primary px-5']) }}</div> </form> </div> </div> </div> @endsection コード
ChatNodeController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use \App\Model\ChatNode; class ChatNodeController extends Controller { public function __construct(){ $this->middleware('auth'); } public function index(){ $chat_node = ChatNode::paginate(10); return view('chat_nodes/index',['chat_node'=>$chat_node]); } public function show($id) { $chat_node = ChatNode::find($id); return view('chat_nodes/show')->with('chat_node',$chat_node); } public function create(){ return view('chat_nodes/create'); } public function store(Request $request){ $validatedData = $request->validate([ 'id' => 'required', 'platform' => 'required', 'type' =>'required', 'title' => 'required', 'image' => 'required', 'button_1' =>'required', 'button_2' =>'required', 'button_3' =>'required', 'creative_type'=>'required' ]); $chat_node = New ChatNode(); $chat_node->id = $request->id; $chat_node->platform = $request->platform; $chat_node->type = $request->type; $chat_node->title = $request->title; $chat_node->image = $request->image; $chat_node->button_1 = $request->button_1; $chat_node->button_2 = $request->button_2; $chat_node->button_3 = $request->button_3; $chat_node->creative_type = $request->creative_type; $chat_node->save(); return redirect('/chat_nodes'); } public function edit(ChatNode $chat_node){ return view('chat_nodes/edit', ['chat_node'=>$chat_node]); } public function update(Request $request, ChatNode $chat_node){ $validatedData = $request->validate([ 'id' => 'required', 'platform' => 'required', 'type'=>'required', 'title' => 'required', 'image' => 'required', 'button_1'=>'required', 'button_2'=>'required', 'button_3'=>'required', 'creative_type'=>'required' ]); $chat_node->id = $request->id; $chat_node->platform = $request->platform; $chat_node->type = $request->type; $chat_node->title = $request->title; $chat_node->image = $request->image; $chat_node->button_1 = $request->button_1; $chat_node->button_2 = $request->button_2; $chat_node->button_3 = $request->button_3; $chat_node->creative_type = $request->creative_type; $chat_node->save(); return redirect('/chat_nodes'); } } ?> コード
web.php //Route::get('/chat_nodes', 'ChatNodeController@index')->name('index'); //Route::get('chat_nodes/create', 'ChatNodeController@create')->name('create'); //Route::post('chat_nodes/store', 'ChatNodeController@store')->name('store'); //Route::get('chat_nodes/{chat_node}/edit', 'ChatNodeController@edit')->name('edit'); //Route::put('chat_nodes/{chat_node}', 'ChatNodeController@update')->name('update'); //Route::get('chat_nodes/{id}', 'ChatNodeController@show')->name('show'); Route::resource('chat_nodes', 'ChatNodeController');上記記法からこちらに変更しました。 コード

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

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

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

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

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

m.ts10806

2020/02/01 04:05

ルーティングもご提示ください。 また、「どのときに」提示のNoticeが出るのでしょうか
amaturePy

2020/02/01 04:14

はい。追加させて頂きました。 chat_nodes/createのURLを叩くとエラーが出現します。
guest

回答1

0

ベストアンサー

createメソッドにある

createメソッドではviewに何も渡してないように見えますが。

php

1 public function create(){ 2 return view('chat_nodes/create'); 3 }

投稿2020/02/01 04:11

m.ts10806

総合スコア80852

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

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

amaturePy

2020/02/06 14:18

ご返事が遅れて申し訳ありませんでした。 form内の$chat_nodeを$chat_nodesに変更して、その他変数の名前も$chat_nodesにすることで解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問