ほぼほぼ自分用メモです。
編集
編集の画面と処理。
Route
web.php
Route::group(
['prefix' => 'contacts', 'middleware' => 'auth'],
function () {
Route::get('index', 'ContactFormController@index')->name('contacts.index');
Route::get('create', 'ContactFormController@create')->name('contacts.create');
Route::post('store', 'ContactFormController@store')->name('contacts.store');
Route::get('show/{id}', 'ContactFormController@show')->name('contacts.show');
Route::get('edit/{id}', 'ContactFormController@edit')->name('contacts.edit'); //追加
}
);
Controller
ContactFormController.php
//モデルの呼び出し
use App\Models\ContactForm;
//ファサードの呼び出し
use Illuminate\Support\Facades\DB;
public function edit($id)
{
$contact = ContactForm::find($id);
return view('contacts/edit', compact('contact'));
}
View
show.blade.php
{{-- createで入れた情報の表示 --}}
<div>{{$contact->name}}</div>
<div>{{$gender}}</div>
<div>{{$contact->contact}}</div>
{{-- editページに移動 --}}
<a href="{{route('contacts.edit',[$contact->id])}}" class="btn btn-primary" type="submit">編集する</a>
edit.blade.php
<form method="POST" action="{{route('contacts.edit',['id'=>$contact->id])}}">
@csrf
<input class="input-group" type="text" name="name" value="{{$contact->name}}">
<input class="input-group" type="text" name="email" value="{{$contact->email}}">
男性:<input type="radio" name="gender" value="0" @if($contact->gender===0)checked @endif>
女性:<input type="radio" name="gender" value="1" @if($contact->gender===1)checked @endif>
<input class="input-group" type="text" name="title" value="{{$contact->title}}">
<textarea class="input-group" name="contact">{{$contact->contact}}</textarea>
<input class="btn btn-primary" type="submit" value="更新する">
</form>
ラジオボタンはif文で現在のデータを表示する。
保存
編集ができるようになったので、編集したデータをアプデートして表示させます。
Route
web.php
Route::group(
['prefix' => 'contacts', 'middleware' => 'auth'],
function () {
Route::get('index', 'ContactFormController@index')->name('contacts.index');
Route::get('create', 'ContactFormController@create')->name('contacts.create');
Route::post('store', 'ContactFormController@store')->name('contacts.store');
Route::get('show/{id}', 'ContactFormController@show')->name('contacts.show');
Route::get('edit/{id}', 'ContactFormController@edit')->name('contacts.edit');
Route::post('update/{id}', 'ContactFormController@update')->name('contacts.update'); //追加
}
);
Controller
ContactFormController.php
public function update(Request $request, $id)
{
$contact = ContactForm::find($id);
$contact->name = $request->input('name');
$contact->email = $request->input('email');
$contact->title = $request->input('title');
$contact->gender = $request->input('gender');
$contact->contact = $request->input('contact');
$contact->save();
return redirect('contacts/show/' . $id);
}
redirectでcontacts/showにリダイレクトさせる。
「.」でidをつなげてるので該当idのページがリダイレクトさせる。
ちゃんと変わってたらおk。
削除
作成したデータを削除します。
Route
web.php
Route::group(
['prefix' => 'contacts', 'middleware' => 'auth'],
function () {
Route::get('index', 'ContactFormController@index')->name('contacts.index');
Route::get('create', 'ContactFormController@create')->name('contacts.create');
Route::post('store', 'ContactFormController@store')->name('contacts.store');
Route::get('show/{id}', 'ContactFormController@show')->name('contacts.show');
Route::get('edit/{id}', 'ContactFormController@edit')->name('contacts.edit');
Route::post('update/{id}', 'ContactFormController@update')->name('contacts.update');
Route::post('destroy/{id}', 'ContactFormController@destroy')->name('contacts.destroy'); //追加
}
);
Controller
ContactFormController.php
public function destroy($id)
{
$contact = ContactForm::find($id);
$contact->delete();
return redirect('contacts/index');
}
View
destroy.show.php
<form method="POST" action="{{route('contacts.destroy',[$contact->id])}}">
@csrf
<button type="submit" class="btn btn-danger" data-id="{{$contact->id}}" onclick="deletePost(this);">削除する</button>
</form>
<script>
function deletePost(e) {
if (confirm('本当に削除してもいいですか?')) {
document.getElementById(('delete_' + e.dataset.id).submit());
}
}
</script>
javascriptで削除前に警告を表示しています。
まとめ
メモ程度ですが、参考になれば。