editアクション(編集)からupdateアクション(更新)に各レコードのユニークidを渡したいのですが、
うまく渡せず困っております。
現状は編集用のedit.blade.phpにidカラムの値を乗せたinputのフォームからupdateアクションにidを
渡してupdateアクションの方でそのレコードのデータか特定しています。ですが、編集画面にはユニークキーは乗せない(入力しなくてもいい)ように処理したいです。
もし参考になる資料などありましたらご教授頂きたいです。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use \App\Model\ChatConversation; class ChatConversationController extends Controller { public function __construct() { $this->middleware('auth'); } public function index() { $chat_conversations = ChatConversation::paginate(10); return view('chat_conversations/index',['chat_conversations' => $chat_conversations]); } public function show($id) { $chat_conversations = ChatConversation::find($id); return view('chat_conversations/show')->with('chat_conversations',$chat_conversations); } public function create() { return view('chat_conversations/create'); } public function store(Request $request) { $validatedData = $request->validate([ 'node_id' => 'required', 'line_id' => 'required', 'message'=>'required', 'version_type' => 'required', 'ip_addr' => 'required', 'platform'=>'required', 'translate'=>'required' ]); $chat_conversations = new ChatConversation(); $chat_conversations->node_id = $request->node_id; $chat_conversations->line_id = $request->line_id; $chat_conversations->message = $request->message; $chat_conversations->version_type = $request->version_type; $chat_conversations->ip_addr = $request->ip_addr; $chat_conversations->platform = $request->platform; $chat_conversations->translate = $request->translate; $chat_conversations->save(); return redirect('/chat_conversations'); } public function edit(Request $request){ $chat_conversations = ChatConversation::find($request->id); return view('chat_conversations/edit', ['chat_conversations'=>$chat_conversations]); } public function update(Request $request) { $chat_conversations = new ChatConversation(); $chat_conversations = ChatConversation::find($request->id); $chat_conversations->node_id = $request->node_id; $chat_conversations->line_id = $request->line_id; $chat_conversations->message = $request->message; $chat_conversations->version_type = $request->version_type; $chat_conversations->ip_addr = $request->ip_addr; $chat_conversations->platform = $request->platform; $chat_conversations->translate = $request->translate; $chat_conversations->save(); return redirect('/chat_conversations'); } } ?> コード
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-md-center"> <div class="col-6"> {{Form::open(['action' => 'ChatConversationController@update','method'=>'Post'])}} <div class="form-group"> <label for="exampleFormControlInput1">node_id</label> <input type="text" class="form-control" name="node_id" value='{{ $chat_conversations->node_id }}'> </div> <div class="form-group"> <label for="exampleFormControlInput1">line_id</label> <input type="text" class="form-control" name="line_id" value='{{ $chat_conversations->line_id }}'> </div> <div class="form-group"> <label for="exampleFormControlInput1">message</label> <input type="text" class="form-control" name="message" value='{{ $chat_conversations->message }}'> </div> <div class="form-group"> <label for="exampleFormControlInput1">version_type</label> <input type="text" class="form-control" name="version_type" value='{{ $chat_conversations->version_type}}'> <div class="form-group"> <label for="exampleFormControlInput1">ip_addr</label> <input type="text" class="form-control" name="ip_addr" value='{{ $chat_conversations->ip_addr }}'> </div> <div class="form-group"> <label for="exampleFormControlInput1">platform</label> <input type="text" class="form-control" name="platform" value='{{ $chat_conversations->platform}}'> </div> <div class="form-group"> <label for="exampleFormControlInput1">translate</label> <input type="text" class="form-control" name="translate" value='{{ $chat_conversations->translate }}'> </div> <input type="submit" class="btn btn-primary" name="post" value="Send"> {{Form::close()}} </div> </div> </div> @endsection コード
Route::get('product_orders/{id}/edit', 'ProductOrderController@edit')->name('edit'); Route::post('product_orders/update', 'ProductOrderController@update')->name('update'); コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/22 09:36