laravel更新機能を作成後、例えば投稿①と投稿②それぞれ違う内容を投稿、一覧ページでも違う内容として表示されていますが、投稿ごとに設置した編集ボタンを押すと、すべて①の内容が表示されます。その後投稿①のIDの内容を更新すると、②を含めたすべてのIDの値が更新されてしまいます。
お手数ですが、教えて頂きたいです。
よろしくお願いいたします。
GradeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Grade;
class GradeController extends Controller
{
protected $namespace = 'App\Http\Controllers';
public function postpage (Request $request){
return view ('student.grade');
}
public function savenew (Request $request){
//
}
public function index()
{
$grades=Grade::orderBy('created_at','desc')->get();
$user=auth()->user();
return view('student.index2', compact('grades','user'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('student.grade');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$inputs=$request->validate([
'grade'=>'required|max:255',
'term'=>'required|max:255',
'japanese'=>'required|max:255',
'math'=>'required|max:255',
'science'=>'required|max:255',
'social_studies'=>'required|max:255',
'music'=>'required|max:255',
'home_economics'=>'required|max:255',
'english'=>'required|max:255',
'art'=>'required|max:255',
'health_and_physical_education'=>'required|max:255',
]);
$grade=new grade();
$grade->grade = $request->grade;
$grade->term = $request->term;
$grade->japanese = $request->japanese;
$grade->math = $request->math;
$grade->science = $request->science;
$grade->social_studies = $request->social_studies;
$grade->music = $request->music;
$grade->home_economics = $request->home_economics;
$grade->english = $request->english;
$grade->art = $request->art;
$grade->health_and_physical_education = $request->health_and_physical_education;
$grade->save();
return back()->with('message','投稿を作成しました');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Grade $grade)
{
$grades=Grade::orderBy('created_at','desc')->get();
$user=auth()->user();
return view('student.show2', compact('grades','user'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Grade $grade)
{
return view('student.edit2', compact('grade'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Grade $grade)
{
$inputs=$request->validate([
'grade'=>'required|max:255',
'term'=>'required|max:255',
'japanese'=>'required|max:255',
'math'=>'required|max:255',
'science'=>'required|max:255',
'social_studies'=>'required|max:255',
'music'=>'required|max:255',
'home_economics'=>'required|max:255',
'english'=>'required|max:255',
'art'=>'required|max:255',
'health_and_physical_education'=>'required|max:255',
]);
$grade->grade = $inputs['grade'];
$grade->term = $inputs['term'];
$grade->japanese = $inputs['japanese'];
$grade->math = $inputs['math'];
$grade->science = $inputs['science'];
$grade->social_studies = $inputs['social_studies'];
$grade->music = $inputs['music'];
$grade->home_economics = $inputs['home_economics'];
$grade->english = $inputs['english'];
$grade->art = $inputs['art'];
$grade->health_and_physical_education = $inputs['health_and_physical_education'];
$grade->save();
return back()->with('message','投稿を更新しました');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
edit2.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-md-10 mt-6">
<div class="card-body">
<h1 class="mt4">成績編集</h1>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if(session('message'))
<div class="alert alert-success">{{session('message')}}</div>
@endif
<form method="post" action="{{route('grade.update',$grade)}}" enctype="multipart/form-data">
@csrf
@method('put')
<div class="form-group">
<label for="grade">学年</label>
<input type="text" name="grade" class="form-control"
id="grade" value="{{old('grade',$grade->grade)}}" placeholder="grade">
</div>
<div class="form-group">
<label for="term">学期</label>
<input type="text" name="term" class="form-control"
id="term" value="{{old('term',$grade->term)}}" placeholder="term">
</div>
<div class="form-group">
<label for="japanese">国語</label>
<input type="text" name="japanese" class="form-control"
id="japanese" value="{{old('japanese',$grade->japanese)}}" placeholder="japanese">
</div>
<div class="form-group">
<label for="math">数学</label>
<input type="text" name="math" class="form-control"
id="math" value="{{old('math',$grade->math)}}" placeholder="math">
</div>
<div class="form-group">
<label for="science">理科</label>
<input type="text" name="science" class="form-control"
id="science" value="{{old('science',$grade->science)}}" placeholder="science">
</div>
<div class="form-group">
<label for="social_studies">社会</label>
<input type="text" name="social_studies" class="form-control"
id="social_studies" value="{{old('social_studies',$grade->social_studies)}}" placeholder="social_studies">
</div>
<div class="form-group">
<label for="music">音楽</label>
<input type="text" name="music" class="form-control" id="music" value="{{old('music',$grade->music)}}" placeholder="music">
</div>
<div class="form-group">
<label for="home_economics">家庭科</label>
<input type="text" name="home_economics" class="form-control"
id="home_economics" value="{{old('home_economics',$grade->home_economics)}}"placeholder="home_economics">
</div>
<div class="form-group">
<label for="english">英語</label>
<input type="text" name="english" class="form-control"
id="english" value="{{old('english',$grade->english)}}" placeholder="english">
</div>
<div class="form-group">
<label for="art">美術</label>
<input type="text" name="art" class="form-control"
id="art" value="{{old('art',$grade->art)}}" placeholder="art">
</div>
<div class="form-group">
<label for="health_and_physical_education">保健体育</label>
<input type="text" name="health_and_physical_education" class="form-control"
id="health_and_physical_education" value="{{old('health_and_physical_education',$grade->health_and_physical_education)}}"placeholder="health_and_physical_education">
</div>
<x-button type="submit" class="btn btn-success">編集</x-button>
<x-button type="button" onclick="history.back()">戻る</x-button>
</form>
</div>
</div>
</div>
@endsection
show2.blade.php
@extends('layouts.app')
@section('content')
{{-- 投稿一覧表示用のコード --}}
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
@foreach($grades as $grade)
<div class="mx-4 sm:p-8">
<div class="mt-4">
<div class="bg-white w-full rounded-2xl px-10 py-8 shadow-lg hover:shadow-2xl transition duration-500">
<div class="mt-4">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->grade }}</h1>
<a href="{{route('grade.edit', $grade)}}"><x-button class="bg-teal-700 float-right">編集</x-button></a>
</h1><hr class="w-full">
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->term }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->japanese }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->math }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->science }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->social_studies }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->music }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->home_economics }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->english }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->art }}</h1>
<hr class="w-full">
<h1 class="text-lg text-gray-700 font-semibold hover:underline cursor-pointer"> {{ $grade->health_and_physical_education }}</h1>
</div>
</div>
</div>
</div>
@endforeach
</div>
@endsection
grade.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-md-10 mt-6">
<div class="card-body">
<h1 class="mt4">成績登録</h1>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if(session('message'))
<div class="alert alert-success">{{session('message')}}</div>
@endif
<form method="post" action="{{route('grade.store')}}" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="grade">学年</label>
<input type="text" name="grade" class="form-control" id="grade" value="{{old('grade')}}" placeholder="grade">
</div>
<div class="form-group">
<label for="term">学期</label>
<input type="text" name="term" class="form-control" id="term" value="{{old('term')}}" placeholder="term">
</div>
<div class="form-group">
<label for="japanese">国語</label>
<input type="text" name="japanese" class="form-control" id="japanese" value="{{old('japanese')}}" placeholder="japanese">
</div>
<div class="form-group">
<label for="math">数学</label>
<input type="text" name="math" class="form-control" id="math" value="{{old('math')}}" placeholder="math">
</div>
<div class="form-group">
<label for="science">理科</label>
<input type="text" name="science" class="form-control" id="science" value="{{old('science')}}" placeholder="science">
</div>
<div class="form-group">
<label for="social_studies">社会</label>
<input type="text" name="social_studies" class="form-control" id="social_studies" value="{{old('social_studies')}}" placeholder="social_studies">
</div>
<div class="form-group">
<label for="music">音楽</label>
<input type="text" name="music" class="form-control" id="music" value="{{old('music')}}" placeholder="music">
</div>
<div class="form-group">
<label for="home_economics">家庭科</label>
<input type="text" name="home_economics" class="form-control" id="home_economics" value="{{old('home_economics')}}"placeholder="home_economics">
</div>
<div class="form-group">
<label for="english">英語</label>
<input type="text" name="english" class="form-control" id="english" value="{{old('english')}}" placeholder="english">
</div>
<div class="form-group">
<label for="art">美術</label>
<input type="text" name="art" class="form-control" id="art" value="{{old('art')}}" placeholder="art">
</div>
<div class="form-group">
<label for="health_and_physical_education">保健体育</label>
<input type="text" name="health_and_physical_education" class="form-control" id="health_and_physical_education" value="{{old('health_and_physical_education')}}"placeholder="health_and_physical_education">
</div>
<x-button type="submit" class="btn btn-success">登録</x-button>
<x-button type="button" onclick="history.back()">戻る</x-button>
</form>
</div>
</div>
</div>
@endsection
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;
use App\Http\Controllers\GradeController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//Route::get('/', function () {
// return view('welcome');
//});
Route::get('/student1', function () {
return view('student');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
require __DIR__.'/auth.php';
Route::resource('/student', StudentController::class);
Route::get('/student2', [StudentController::class,'show']);
Route::resource('/grade', GradeController::class);
Route::get('/grade2', [GradeController::class,'show']);