I will wish in fact to learn to update my data on Laravel. I am able to recuperate the ID with the informations from recording but when I change several elements on my form and that I confirm.
I have a problem concerning Illuminate\Routing\RouteCollection methodNotAllowed the error message is Auth::routes(); route::resource('student','AdminController');
My level is yet slow to understand the problem... What should I do ?
For information here is my code
I have a file named AdminController
class AdminController extends Controller
{
public function index()
{
$students = Student::paginate(5);
return view('student.index', compact('students'))
->with('i', (request()->input('page',1)-1)*5);
}
public function create()
{
return view('student.create');
}
public function store(Request $request)
{
$request->validate([
'firstname' => 'required',
'lastname' => 'required'
]);
Student::create($request->all());
return redirect()->route('student.index')
->with('success', 'save');
}
public function edit($id)
{
$student = Student::find($id);
return view('student.edit', compact('student'));
}
public function update(Request $request, $id)
{
$request->validate([
'firstname' => 'required',
'lastname' => 'required'
]);
$student = Student::find($id);
$student->firstname = $request->get('firstname');
$student->lastname = $request->get('lastname');
$student->save();
return redirect()->route('student.index')
->with('success', 'updated successfully');
}
}
In View I have a folder nammed Student with 2 files
index.blade.php
@section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1>
<i class="px-nav-icon ion-android-apps"></i>List
</h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('student.create') }}">Create</a>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
@foreach($students as $student)
<tr>
<td> {{$student->firstname}}</td>
<td> {{$student->lastname}} </td>
<td>
<a class="btn btn-sm btn-warning" href="{{route('student.edit',$student->id)}}">Edit</a>
</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
</div>
{!! $students->links() !!}
</div>
@endsection
And also edit.blade.php
@section('content')
<div class="px-content">
<div class="page-header">
<div class="row">
<div class="col-md-4 text-xs-center text-md-left text-nowrap">
<h1>
<i class="px-nav-icon ion-android-apps"></i>Edit {{$student->id}}
</h1>
</div>
<hr class="page-wide-block visible-xs visible-sm">
<!-- Spacer -->
<div class="m-b-2 visible-xs visible-sm clearfix"></div>
</div>
</div>
<div class="row">
<div class="panel">
<div class="panel-body">
<div class="table-responsive">
<form class="panel-body" action="/student/edit/{{$student->id}}" method="POST">
@csrf
<fieldset class="form-group">
<label for="form-group-input-1">Firstname</label>
<input type="text" name="firstname" class="form-control" id="form-group-input-1" value="{{$student->firstname}}">
</fieldset>
<fieldset class="form-group">
<label for="form-group-input-1">Lastname</label>
<input type="text" name="valeur" class="form-control" id="form-group-input-1" value="{{$lastname->valeur}}">
</fieldset>
<fieldset class="form-group">
<label for="form-group-input-1">Page</label>
<input type="text" name="page" class="form-control" id="form-group-input-1" value="{{$tariff->page}}">
</fieldset>
<button type="submit" class="btn btn-primary pull-right">MAJ</button>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Concerning the folder route I have that
Auth::routes();
route::resource('student','AdminController');
Do you have an idea please ? Because I don't understand my problem...
just add the below code in your blade after your <form>
<input name="_method" type="hidden" value="PATCH">
and also change your route with Route::PATCH('/update/{id}','YourController@update');