Laravel 6 The POST method is not supported for this route. Supported methods: GET, HEAD - laravel

I try to create edit account user form. But i got an error with The POST method is not supported for this route. Supported methods: GET, HEAD notification. Here is my view blade:
Blade.php
<form action="{{ route('account_update.user') }}" method="POST">
#csrf
<input type="hidden" name="id" value="{{ $account->id }}" required>
<input type="text" name="name" value="{{ $account->name }}" required>
<button type="submit">Save</button>
</form>
Here is my routes:
Web.php
Route::post('account/update', 'AccountController#account_update')->name('account_update.user');
And here is my controller
Controller.php
public function account_update(Request $request)
{
DB::table('users')->where('id',$request->id)->update([
'name' => $request->name
]);
return redirect()->route('account.user');
}
Can anyone help me how to fix it?

Use Like that
<form action="{{ url('account/update') }}" method="POST">
#csrf
<input type="hidden" name="id" value="{{ $account->id }}" required>
<input type="text" name="name" value="{{ $account->name }}" required>
<button type="submit">Save</button>
</form>

Related

How to update or create at same time in laravel

``I had one page on that I'm Showing data from two table with left join and also edit button in front of every column when user click to edit button ,it goes a different page where user can update input fields but at the same time i want if some field is not showing data we can create data only when both the id of tables are matching
By this i can easily update but i can't create it shows error
Attempt to assign property "SchemeId" on null
Controller
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::find($SchemeId);
$scheme->SchemeId = $request->input('SchemeId');
$scheme->MerchantCode = $request->input('MerchantCode');
$scheme->BankAccountNumber = $request->input('BankAccountNumber');
$scheme->BankAccountIFSC = $request->input('BankAccountIFSC');
$scheme->save();
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}
**view**
<form action="{{ url('SchConfigration-update/' . $user->SchemeId) }}" method="post"
enctype="multipart/form-data">
#csrf
#method('PUT')
<input type="hidden" name="SchemeId" value="{{ $user->SchemeId }}">
<div class="form_row">
<div class="form_item">
<label>Scheme Name</label>
<input type="text" id="SchemeId" name="SchemeId"
placeholder="Enter scheme name" class="form-control" required
value="{{ $user->SchemeId }}">
{{-- value="{{ request()->SchemeId }}" --}}
{{-- if we want to pass scheme name just pass $user->SchemeName --}}
</div>
<div class="form_item">
<label>Merchant Code</label>
<input type="text" id="MerchantCode" name="MerchantCode"
placeholder="Enter Merchant Code" class="form-control" required
value="{{ $user->MerchantCode }}">
</div>
</div>
<div class="form_row">
<div class="form_item">
<label>Bank Account Number</label>
<input type="text" id="BankAccountNumber" name="BankAccountNumber"
placeholder="Bank account Number" maxlength="17" class="form-control" required
value="{{ $user->BankAccountNumber }}">
</div>
<div class="form_item">
<label>Bank Account IFSC</label>
<input type="text" id="BankAccountIFSC" name="BankAccountIFSC"
placeholder="Bank account IFSC" maxlength="11" class="form-control" required
value="{{ $user->BankAccountIFSC }}">
</div>
</div>
<div class="btn_row">
<input type="submit" value="submit" class="primary_btn">
<a class="btn btn-primary" href="{{ url('SchConfigration') }}"
role="button">Back</a>
</div>
</form>
`
Try This
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::updateOrCreate(
['SchemeId' => $request->input('SchemeId')],
[
'MerchantCode' => $request->input('MerchantCode'),
'BankAccountNumber' => $request->input('BankAccountNumber'),
'BankAccountIFSC' => $request->input('BankAccountIFSC')
]
);
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}

ErrorException Undefined array key 0 (View: C:\Users\Teacher\CNHSSystem\resources\views\section\index.blade.php)

Hello everyone I was trying to pass value from my resource section blade
<form action="POST" action="{{ ('/import-students') }}">
{{ csrf_field() }}
<div class="form-group">
#foreach($programs as $program)
#if($section->program_id == $program->id)
<input type="hidden" name="strand" value="{{ $program->strand }}">
#endif
#endforeach
<input type="hidden" name="section" value="{{ $section->section_name }}">
<input type="hidden" name="grade_level" value="{{ $section->grade_level }}">
<input type="hidden" name="semester" value="{{ $section->semester }}">
<input type="hidden" name="school_year" value="{{ $section->school_year }}">
</div>
<button type="submit" name="submit">
add
</button>
<!-- <input type="submit" class="text-primary" name="submit"> -->
</form>
to an import blade from another resource
<form method="POST" action="{{ ('/upload-students') }}">
{{ csrf_field() }}
<div class="form-group">
<input type="hidden" name="strand" value="{{ request('strand') }}">
<input type="hidden" name="section" value="{{ request('section') }}">
<input type="hidden" name="grade_level" value="{{ request('grade_level') }}">
<input type="hidden" name="semester" value="{{ request('semester') }}">
<input type="hidden" name="school_year" value="{{ request('school_year') }}">
<input type="file" name="file" class="form-control" />
<button type="submit" class="btn btn-primary mt-1">Save</button>
</div>
</form>
the problem is that the action from the section blade doesn't seem to read the controller
public function importStudents(Request $request)
{
// dd($request);
$strand = $request->input('strand');
$section = $request->input('section');
$grade_level = $request->input('grade_level');
$semester = $request->input('semester');
$school_year = $request->input('school_year');
return view('student.import', $strand, $section, $grade_level, $semester, $school_year);
}
Here's my web route
Route::post('/import-students', [StudentController::class, 'importStudents']);
I'am hoping for your help maam/sirs
and a very big THANK YOU in advance for the help that I can receive maam/sirs.
First, correct forms:
<form action="POST" action="{{ ('/import-students') }}">
this form has two action (method="post" not action) etc. Open this page: https://laravel.com/docs/9.x/blade#forms and see how to write it correctly.
Second, in view('view', $data) the second parameter should contain data (https://laravel.com/docs/5.0/views). So:
return view('student.import', [
'strand' => $strand,
'section' => $section,
'grade_level' => $grade_level,
'semester' => $semester,
'school_year' => $school_year
]);

Laravel datatables raw columns

i have a code like this to display an action button for datatables
->addColumn('action', function () {
return '<form id="delete" action="{{ route(' . 'admin.posts.destroy' . ', $model) }}" method="POST">
#csrf
#method("DELETE")
Edit
<input type="submit" class="btn btn-danger" value="Delete">
</form>';
})
But the #csrf and #method("DELETE") become a string/text (not method). I tried to append {{ }} in #csrf and #method("DELETE") but it doesn't work. How to change that text to method in blade templates without make a new view for action button like that?
Thank you!
#csrf replace with <input type="hidden" name="_token" value="{{ csrf_token() }}"> and
#method("DELETE") with <input type="hidden" name="_method" value="delete">

The GET method is not supported for this route. Supported methods: PATCH. when doing update

i want to submit for updating the data, i already use the patch method, but it keep telling me that the get method is not supported
the edit formedit.info.blade
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="patch" enctype="multipart/form-data">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
the route list
web.php
//info penting
route::get('/info-admin','InfosController#index')->middleware('auth','admin')->name('admin.info-admin');
route::get('/tambah-info','PagesController#tambah')->middleware('auth','admin')->name('info.add');
route::patch('/update-info/{info}/update','InfosController#update')->name('info.update');
route::get('/edit-info/{info}/edit','InfosController#edit')->middleware('auth','admin')->name('info.edit');
route::delete('/info/{id}','InfosController#destroy')->middleware('auth','admin')->name('info.destroy');
here's for update logic
InfosController
public function update(Request $request, Info $info) {
Info::where('id', $info->id)->update([
'judul' => $request->judul,
'konten' => $request->konten,
'image' => $request->image,
]);
return redirect('')->route('admin.info-admin')->with('success', 'Successful');
}
what did i do wrong?
HTML5 forms only supports GET, POST and DIALOG method only. so using PATCH won't work.
you have to add it inside the from
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PATCH')
this link will help you to understand form methods.
You have to change the method to POST. Because in some browser PUT/PATCH is not suported
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

Laravel 5.4 - Updating a resource

I'm building a blog post to learn Laravel 5.4 and am struggling to find any examples of how to update a Post anywhere.
My form is as follows
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
My Routes are as follows
Route::get('/posts/{post}/edit', 'PostsController#edit');
Route::patch('/posts/{post}', 'PostsController#update');
And my controller methods are
public function edit( Post $post )
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post )
{
Post::where('id', $post)->update($request->all());
return redirect('home');
}
I get a MethodNotAllowedHTTPException error but am not sure which part / parts of this I am getting wrong.
I'm assuming it must be the point at which I'm using the PATCH function, or potentially just the way I'm mass-assigning the new values. Any help would be greatly appreciated.
you should use
{{ method_field('PATCH') }}
as your form field
and change action to
/posts/{{ $post->id }}
like this:
<form method="POST" action="/posts/{{ $post->id }}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
There are a couple of things you have missed out.
Firstly, as #Maraboc pointed out in the comments you need to add method spoofing as standard HTML forms only allow for GET and POST methods:
<input type="hidden" name="_method" value="PATCH">
or
{{ method_field('PATCH') }}
https://laravel.com/docs/5.4/routing#form-method-spoofing
Then you will also need to omit the "edit" uri in your forms action:
<form method="POST" action="/posts/{{ $post->id }}">
or
<form method="POST" action="{{ url('posts/' . $post->id) }}">
https://laravel.com/docs/5.4/controllers#resource-controllers
(scroll down a little bit to the Actions Handled By Resource Controller section)
You also may find it helpful to watch https://laracasts.com/series/laravel-5-from-scratch/episodes/10
Hope this helps!
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.

Resources