前提
Laravelで簡易的な申請and承認アプリを作っています。
実現したいこと
右側のセレクトボックスに申請の現状ステータスを常に表示して、
さらにセレクトボックスからステータスを選択をして更新ボタンを押したら。
データベースのstatus_idが更新されるという機能を実装したいです。
発生している問題・エラーメッセージ
処理が通ってsuccesのメッセージが出るが、 データベース内がきちんと更新されていない。
該当のソースコード
index.blade.php
1@extends('layouts.app') 2 3@section('content') 4<div class="card p-5 mt-4 w-100 h-100"> 5 <form method='POST' action="{{ route('change_status', $sing_up_status->status_id) }}"> 6 @csrf 7 <table class="table"> 8 <thead class="thead-light"> 9 <tr> 10 <th>#</th> 11 <th>名前</th> 12 <th>日時</th> 13 <th>申請内容</th> 14 <th>状態</th> 15 <th>更新</th> 16 <th>編集</th> 17 </tr> 18 </thead> 19 20 <tbody> 21 @foreach ($signing_up as $signup) 22 <tr> 23 <th scope="row">{{ $signup->id }}</th> 24 <td>{{ $signup->user->name }}</td> 25 <td>{{ $signup->updated_at }}</td> 26 <td>{{ $signup->content }}</td> 27 <td> 28 <select class="form-control" id="status" name="status"> 29 @foreach ($statuses as $status) 30 <option value="{{ $status->id }}"> {{ $status->name }} </option> 31 @endforeach 32 </select> 33 </td> 34 <td> 35 @if($signup->status->id <= 2) 36 <button type="submit" id="update_button" value="更新">更新</button> 37 @else 38 <button type="submit" id="update_button" value="更新" disabled>更新</button> 39 @endif 40 41 </td> 42 <td> 43 <a href="{{ route('edit_signing_up', $signup->id) }}"> 編集 </a> 44 </td> 45 </tr> 46 @endforeach 47 </tbody> 48 </table> 49 </form> 50</div> 51@endsection
SigningUpController.php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\SigningUp; 6use App\Status; 7use Illuminate\Http\Request; 8 9class SigningUpController extends Controller 10{ 11 public function __construct(){ 12 $this->middleware('auth'); 13 } 14 15 /** 16 * Display a listing of the resource. 17 * 18 * @return \Illuminate\Http\Response 19 */ 20 public function index() 21 { 22 $users = \Auth::user(); 23 $statuses = Status::get(); 24 $sing_up_status = SigningUp::first(); 25 $signing_up = SigningUp::where('users_id', $users->id)->orderby('updated_at', 'DESC')->get(); 26 return view('index', compact('signing_up','statuses', 'sing_up_status')); 27 } 28 29 /** 30 * Show the form for creating a new resource. 31 * 32 * @return \Illuminate\Http\Response 33 */ 34 public function create() 35 { 36 return view('create'); 37 } 38 39 /** 40 * Store a newly created resource in storage. 41 * 42 * @param \Illuminate\Http\Request $request 43 * @return \Illuminate\Http\Response 44 */ 45 public function store(Request $request) 46 { 47 $user = \Auth::user(); 48 $signing_up = new SigningUp; 49 $signing_up->users_id = $user->id; 50 $signing_up->content = $request->content; 51 $signing_up->save(); 52 53 return redirect()->route('index')->with('success', '申請しました'); 54 } 55 56 /** 57 * Display the specified resource. 58 * 59 * @param int $id 60 * @return \Illuminate\Http\Response 61 */ 62 public function change_status(Request $request, SigningUp $signing_up) 63 { 64 $signing_up->status_id = $request->input('status'); 65 $signing_up->save(); 66 67 return redirect()->route('index')->with('success', 'ステータスを変更しました'); 68 } 69 70 /** 71 * Show the form for editing the specified resource. 72 * 73 * @param int $id 74 * @return \Illuminate\Http\Response 75 */ 76 public function edit(SigningUp $signing_up) 77 { 78 return view('edit_signing_up', compact('signing_up')); 79 80 } 81 82 /** 83 * Update the specified resource in storage. 84 * 85 * @param \Illuminate\Http\Request $request 86 * @param int $id 87 * @return \Illuminate\Http\Response 88 */ 89 public function update(Request $request, SigningUp $signing_up) 90 { 91 $validatedData = $request->validate([ 92 'content' => 'required|max:50', 93 ]); 94 95 $signing_up->update([ 96 'content' => $request->input('content') 97 ]); 98 99 return redirect()->route('index')->with('success', '申請内容を編集しました!'); 100 } 101 102 /** 103 * Remove the specified resource from storage. 104 * 105 * @param int $id 106 * @return \Illuminate\Http\Response 107 */ 108 public function destroy($id) 109 { 110 // 111 } 112} 113
SigningUp.php
1<?php 2 3namespace App; 4 5use Illuminate\Database\Eloquent\Model; 6 7class SigningUp extends Model 8{ 9 protected $fillable = ['content', 'status']; 10 11 protected $table = 'signing_up'; 12 13 public function user() 14 { 15 return $this->belongsTo('\App\User','users_id','id'); 16 } 17 18 public function status() 19 { 20 return $this->belongsTo('\App\Status','status_id','id'); 21 } 22}
2022_08_26_111804_create_signing_up_table.php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7class CreateSigningupTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('signing_up', function (Blueprint $table) { 17 $table->bigIncrements('id'); 18 $table->integer('users_id'); 19 $table->text('content'); 20 $table->integer('status_id')->default(1); 21 $table->timestamps(); 22 }); 23 } 24 25 /** 26 * Reverse the migrations. 27 * 28 * @return void 29 */ 30 public function down() 31 { 32 Schema::dropIfExists('application'); 33 } 34} 35
補足情報(FWのバージョン)
Laravel Framework 6.20.31
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/30 14:06
2022/08/30 14:09
2022/08/30 16:16
2022/08/30 16:20