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');上記記法からこちらに変更しました。 コード
回答1件
あなたの回答
tips
プレビュー