i am working on laravel project for practice. I have got a error while i sending a data in html form by use PUT / PATCH . I read laravel documentation and i putin html this code line "@method('PUT')" However this error still coming up. Have can i fix this error? Thank you.
web.php file
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/course', 'CourseController@index');
Route::post('/course','CourseController@store');
Route::get('/profile','ProfileController@index');
Route::get('/about','HomeController@about');
Route::get('/contact','HomeController@contact');
Route::get('/organization','OrganizationController@index');
Route::get('/organization/create','OrganizationController@create');
Route::post('/organization','OrganizationController@store');
Route::get('/organization/{organizationId}','OrganizationController@show');
Route::get('/organization/{organizationId}/edit','OrganizationController@edit');
Route::patch('/organization/{organizationId}','OrganizationController@update');
OrganizationController(this case update function)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class OrganizationController extends Controller
{
public function index(){
$organizations = \App\Organization::All();
return view('organization.index',compact('organizations'));
}
public function create(){
return view('organization.create');
}
public function store(){
$data = request()->validate([
'organizationName' => 'required',
'password' => 'required',
'organizationType' => 'required',
'email' => 'email',
'telephoneNo',
'webpage',
'organizationEmail'
]);
\App\Organization::create($data);
return redirect('/organization');
}
public function show($organizationId){
$organization = \App\Organization::findOrFail($organizationId);
return view('organization.show',compact('organization'));
}
public function edit(\App\Organization $organization){
return view('organization.edit',compact('organization'));
}
public function update(\App\Organization $organization){
$data = request()->validate([
'organizationName' => 'required',
'password' => 'required',
'organizationType' => 'required',
'email' => 'email',
'telephoneNo',
'webpage',
'organizationEmail'
]);
$organization->update($data);
return redirect('/organization');
}
}
edit.blade.php
<h1>Edit Organization</h1>
<form action="/organization/{{ $organization->id }}" method="POST">
<input type="hidden" name="_method" value="PATCH">
@csrf
<div>
<label for="name">Organization Name</label>
<input type="text" name="organizationName" autocomplete="off" value="{{
$organization>organizationName }}">
@error('name')<p>{{ $message }}</p> @enderror
</div>
<div>
<label for="organizationType">Organization Type</label>
<input type="text" name="organizationType" autocomplete="off" value="{{
$organization>organizationType }}">
@error('organizationType')<p>{{ $message }}</p> @enderror
</div>
<div>
<label for="password">Organization Password</label>
<input type="password" name="password" autocomplete="off" value="{{ $organization->password
}}">
@error('password')<p>{{ $message }}</p> @enderror
</div>
<div>
<label for="name">Telephone Number</label>
<input type="text" name="telephoneNo" autocomplete="off" value="{{
$organization>telephoneNo }}">
@error('telephoneNo')<p>{{ $message }}</p> @enderror
</div>
<div>
<label for="webpage">Web Site</label>
<input type="text" name="webpage" autocomplete="off" value="{{ $organization->webpage }}">
@error('webpage')<p>{{ $message }}</p> @enderror
</div>
<div>
<label for="organizationEmail">E-Mail</label>
<input type="email" name="organizationEmail" autocomplete="off" value="{{
$organization>organizationEmail }}">
@error('email')<p>{{ $message }}</p> @enderror
</div>
<button>Save Organization</button>
</form>
I try edit organization this page http://localhost:8000/organization/1/edit click save and i see this error. Error page
Thank you.
Put after your form
tag this line:
@method('PATCH')
if you want put make it put
@method('PUT')
The url in your error is /organization/
without the id, which indeed does not have a patch route.
Try to debug or validate that your edit method actually passes an $organization
object to your view that has an id set.
Please give this a try, I've not tested it, but this is how it's all usually work
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Organization;
class OrganizationController extends Controller
{
public function index(){
$organizations = Organization::All();
return view('organization.index',compact('organizations'));
}
public function create(){
return view('organization.create');
}
public function store(Request $request){
try{
$validatedData = $request->validate([
'organizationName' => 'required',
'password' => 'required',
'organizationType' => 'required',
'email' => 'email',
'telephoneNo',
'webpage',
'organizationEmail'
]);
Organization::create($validatedData);
return redirect()->back()->with([
'status' => 'success',
'message' => 'Organization Created'
]);
}catch(Exception $e){
return redirect()->back()->with([
'status' => 'error',
'message' => $e->getMessageBag()
]);
}
}
public function show($organizationId = 0){
$organization = Organization::findOrFail($organizationId);
if($organization === null){
return redirect()->back()->with([
'status' => 'error',
'message' => 'Invalid Organization'
])
}
return view('organization.show',compact('organization'));
}
public function edit($organizationId = 0){
$organization = Organization::findOrFail($organizationId);
if($organization === null){
return redirect()->back()->with([
'status' => 'error',
'message' => 'Invalid Organization'
]);
}
return view('organization.edit',compact('organization'));
}
public function update(Request $request, $organizationId = 0){
try{
$organization = Organization::findOrFail($organizationId);
if($organization === null){
return redirect()->back()->with([
'status' => 'error',
'message' => 'Invalid Organization'
])
}
$validatedData = $request->validate([
'organizationName' => 'required',
'password' => 'required',
'organizationType' => 'required',
'email' => 'email',
'telephoneNo',
'webpage',
'organizationEmail'
]);
$organization->where('id',$org_id)->update($validatedData);
return redirect()->back()->with([
'status' => 'success',
'message' => 'Organization Updated'
]);
}catch(Exception $e){
return redirect()->back()->with([
'status' => 'error',
'message' => $e->getMessageBag()
]);
}
}
}
Here is web.php
Route::get('/', function () {
return view('welcome');
});
//Auth::routes();
//Route::get('/home', 'HomeController@index')->name('home');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/course', 'CourseController@course')->name('course');
Route::post('/course','CourseController@storeCourse')->name('storeCourse');
Route::get('/profile','ProfileController@profile')->name('profile');
Route::get('/about','HomeController@about')->name('about');
Route::get('/contact','HomeController@contact')->name('contact');
Route::group([
'prefix' => 'organization',
'as' => 'organization.',
],function(){
Route::get('/','OrganizationController@index')->name('list');
Route::get('/create','OrganizationController@create')->name('create');
Route::post('/store','OrganizationController@store')->name('store');
Route::get('/show/{organizationId}','OrganizationController@show')->name('show');
Route::get('/edit/{organizationId}','OrganizationController@edit')->name('edit');
Route::post('/update/{organizationId}','OrganizationController@update')->name('update');
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.