現在『更新』画面を作成中なのですが、お聞きしたいことがあります。
Laravel Framework 6.0.3
- optionがoldに対応して、selectedされない
様々なサイトを見たりして、同じように書いていますが、どうもうまく行きません。
Laravel
1// index.blade.php 2@extends('layouts.app') 3 4@section('content') 5<div class="container"> 6 <div class="row justify-content-center"> 7 <div class="col-md-8"> 8 <div class="card"> 9 <div class="card-header">全ての思い出</div> 10 11 <div class="card-body"> 12 @if (session('status')) 13 <div class="alert alert-success" role="alert"> 14 {{ session('status') }} 15 </div> 16 @endif 17 @foreach ($memories as $memory) 18 <div class="card mb-3"> 19 <div class="card-body"> 20 <h5 class="card-title">{{ $memory->title }}</h5> 21 <p class="card-text"> 22 カテゴリー:<a href="{{ route('memories.index', ['category_id' => $memory->category_id])}}">{{ $memory->category->category_name }}</a> 23 </p> 24 <p class="card-text">{{ $memory->content }}</p> 25 <a href="{{ route('memories.show', $memory->id) }}" class="btn btn-primary">詳細</a> 26 <a href="{{ route('memories.edit', $memory->id) }}" class="btn btn-primary">編集</a> 27 <form action="{{ route('memories.destroy', ['memory' => $memory]) }}" method="POST"> 28 @csrf 29 @method('DELETE') 30 <button type="submit" class="btn btn-danger">削除</button> 31 </form> 32 </div> 33 </div> 34 @endforeach 35 </div> 36 </div> 37 </div> 38 </div> 39</div> 40@endsection 41
laravel
1 2// edit.blade.php 3@extends('layouts.app') 4 5@section('content') 6<div class="container"> 7 <div class="card"> 8 <div class="card-header">思い出</div> 9 <div class="col-6"> 10 <div class="card-body"> 11 @if ($errors->any()) 12 <div class="alert alert-danger"> 13 <ul> 14 @foreach ($errors->all() as $error) 15 <li>{{ $error }}</li> 16 @endforeach 17 </ul> 18 </div> 19 @endif 20 @if (session('status')) 21 <div class="alert alert-success" role="alert"> 22 {{ session('status') }} 23 </div> 24 @endif 25 <div class="card mb-3"> 26 <div class="card-body"> 27 <form method="POST" action="{{ route('memories.update', $memory->id) }}"> 28 @csrf 29 @method('put') 30 <div class="form-group"> 31 <label>いつの思い出ですか?</label> 32// ここら辺です。 33 <select class="form-control" name="category_id"> 34 <option value="1" @if( old('category_id')=='1') selected @endif>中学生</option> 35 <option value="2" @if( old('category_id')=='2') selected @endif>高校生</option> 36 <option value="3" @if( old('category_id')=='3') selected @endif>大学生</option> 37 </select> 38 </div> 39 <div class="form-group"> 40 <label for="exampleInputEmail1">memories title</label> 41 <input type="text" class="form-control" value="{{ old('title') ?: $memory->title }}" name="title"> 42 <small class="form-text text-muted">title</small> 43 </div> 44 <div class="form-group"> 45 <label>memories</label> 46 <textarea class="form-control" rows="3" name="content">{{ old('content') ?: $memory->content }}</textarea> 47 </div> 48 <button type="submit" class="btn btn-light">更新</button> 49 </form> 50 </div> 51 </div> 52 </div> 53 </div> 54 </div> 55</div> 56@endsection 57
laravel
1// MemoryControllor.php 2 3 public function edit($id) 4 { 5 $memory = Memory::find($id); 6 7 return view('memories.edit', [ 8 'memory' => $memory, 9 ]); 10 } 11 12 13 public function update(MemoryRequest $request, $id) 14 { 15 $memory = Memory::find($id); 16 17 if (\Auth::id() === $memory->user_id) { 18 $memory->category_id = $request->category_id; 19 $memory->title = $request->title; 20 $memory->content = $request->content; 21 dd($memory); 22 $memory->save(); 23 } 24 return redirect('/'); 25 } 26
laravel
1// web.php 2 3 4Auth::routes(); 5 6Route::get('/', 'MemoryController@index')->name('memories.index'); 7Route::resource('/memories', 'MemoryController', ['except' => ['index']]); 8
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/21 07:17