the put method is not supported - php

when i try to update my the section name i am having this error: The PUT method is not supported for this route. Supported methods: GET, HEAD.
there is the form:
<form action="sections.update" method="POST" autocomplete="off">
#csrf
#method('PUT')
<div class="form-group">
<input type="hidden" name="id" id="id" value="">
<label for="recipient-name" class="col-form-label">section name</label>
<input class="form-control" name="section_name" id="section_name" type="text">
</div>
<div class="form-group">
<label for="message-text" class="col-form-label">description</label>
<textarea class="form-control" id="description" name="description"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">confirm</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">close</button>
</div>
</form>
and that is the update controller
public function update(Request $request)
{
$id = $request->id;
$this->validate($request, [
'section_name' => 'required|max:255|unique:sections,section_name,' . $id,
'description' => 'required',
], [
'section_name.required' => 'section name is required',
'section_name.unique' => 'section_name should be unique',
'description.required' => 'description is required',
]);
$sections = sections::find($id);
$sections->update([
'section_name' => $request->section_name,
'description' => $request->description,
]);
session()->flash('edit', 'the section is edited successfully');
return redirect('/sections');
}

you have to pass the id/parameter in your form action when using put method but u re fetching id from form , so you can pass some dummy data on it. Change the form to
<form action="action="{{ route('sections.update', ['section' => '1']) }}" method="POST" autocomplete="off">

Related

Update not working in Resource Controller for Laravel 8

Previously, I was trying for a few hours to get my "destroy()" function working. Which would sound ridiculous to most of you but simply putting "method("DELETE")" in the form made it work, which I still don't why.
Now, I'm stuck with the update() function. Once I click update, the page just reloads and does nothing. It doesn't update the info in the database nor does it redirect me back to the index. Here's the code:
Route (web.php):
Route::resource('books', BookController::class);
Form (Edit.blade.php):
#section('content')
<div class="row">
<div class="col-lg-12">
<form action="{{ route('books.update', $book->id) }}" method="POST">
#csrf
#method('PUT')
<div class="mb-3 mt-3">
<label for="exampleFormControlInput1" class="form-label">Book Title</label>
<input type="text" name="title" class="form-control" value="{{ $book->title }}">
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Book Author</label>
<input type="text" name="author" class="form-control" value="{{ $book->author }}" placeholder="Author Name..">
</div>
<div class="btn-group" role="group">
<button type="submit" class="btn btn-block btn-danger">Update</button>
<button type="button" class="btn btn-success">Go Back</button>
</div>
</form>
</div>
</div>
#endsection
Controller (BookController):
public function update(Request $request, Book $book)
{
$validated = $request->validate([
'title' => 'required|max:255|unique:books',
'author' => 'required|max:255'
]);
Book::where('id',$book->id)->update($validated);
return redirect()->route('books.index')->with('message','Book Updated!');
}
What am I doing wrong here? Some help would be appreciated.
I think the problem is in your unique validation. When you don't change the title in the form and hit updated, the unique validation fails. It fails because the title already exists in the database. We have to skip unique validation for the resource that is being updated.
$validated = $request->validate([
'title' => 'required|max:255|unique:books,title,'.$book->id, // <-- change this
'author' => 'required|max:255'
]);
$book->update( $request->only(['title', 'author']) );

Function Store not save in database laravel 8

I m using laravel 8 controller for saving text input type using "Store function".My input for 'name' are not stored in database when i submited it.
Here is my blade
<form class="text-center p-4" action="{{ route('store') }}" method = "POST">
#csrf
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">names</label>
<input type="text" class="form-control" id="name" placeholder="name">
</div>
<button class="btn btn-primary" type="submit">Button</button>
</form>
Here is my ProductController.php
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
Product::create($request->all());
$products -> save();
return redirect('/saving-list') ->with('success','Umrah record has been updated');
}
your parameter is not complete, 'detail' => 'required' but it's not found
you need new value input <input type="text" class="form-control" id="detail" placeholder="detail" name="detail">
and you have to add attribute name in every input
for complete code like this bellow
<form class="text-center p-4" action="{{ route('store') }}" method = "POST">
#csrf
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">names</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
<input type="text" class="form-control" id="detail" name="detail" placeholder="detail">
</div>
<button class="btn btn-primary" type="submit">Button</button>
</form>
and for save or store method https://laravel.com/docs/8.x/eloquent#inserts
$product= new Product;
$product->name = $request->name;
$product->detail= $request->detail;
$product->save();
or like this
$product= Product::create([
'name' => $request->name,
'detail' => $request->detail,
]);

My Laravel Website is redirecting all post request to index page

Having Problem with POST Request
All the get routes are working fine.
Post route when submiting any form it gets updated in DB and then redirected to homepage but where I go back to previous link. I see that I am having all the success and errors on that page before getting redirected to homepage.
My POST Route:
Route::post('/update-faq', 'FaqController#update')->name('admin.faq.update');
My Controller Function:
public function index(){
$all_faqs = Faq::all();
return view('backend.pages.faqs')->with(['all_faqs' => $all_faqs]);
}
public function update(Request $request){
$this->validate($request,[
'title' => 'required|string',
'description' => 'required|string',
'status' => 'nullable|string|max:191',
]);
Faq::find($request->id)->update([
'title' => $request->title,
'description' => $request->description,
'status' => $request->status,
'is_open' => !empty($request->is_open) ? 'on' : '',
]);
return redirect()->back()->with(['msg' => 'Faq Updated...','type' => 'success']);
}
My View:
<div class="modal fade" id="faq_item_edit_modal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ 'Edit Faq Item' }}</h5>
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
</div>
<form action="{{ route('admin.faq.update') }}" id="faq_edit_modal_form" enctype="multipart/form-data"
method="post">
<div class="modal-body">
#csrf
<input type="hidden" name="id" id="faq_id" value="">
<div class="form-group">
<label for="edit_title">{{ 'Title' }}</label>
<input type="text" class="form-control" id="edit_title" name="title"
placeholder="{{ 'Title' }}">
</div>
<div class="form-group">
<label for="edit_is_open">{{ 'Is Open' }}</label>
<label class="switch">
<input type="checkbox" name="is_open" id="edit_is_open">
<span class="slider"></span>
</label>
</div>
<div class="form-group">
<label for="edit_description">{{ 'Description' }}</label>
<textarea name="description" id="edit_description" cols="30" rows="10"
class="form-control max-height-150" placeholder="{{ 'Description' }}"></textarea>
</div>
<div class="form-group">
<label for="edit_status">{{ 'Status' }}</label>
<select name="status" id="edit_status" class="form-control">
<option value="publish">{{ 'Publish' }}</option>
<option value="draft">{{ 'Draft' }}</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Close' }}</button>
<button type="submit" class="btn btn-primary">{{ 'Save Changes' }}</button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$(document).on('click', '.faq_edit_btn', function() {
var el = $(this);
var id = el.data('id');
var title = el.data('title');
var form = $('#faq_edit_modal_form');
form.find('#faq_id').val(id);
form.find('#edit_title').val(title);
form.find('#edit_description').val(el.data('description'));
form.find('#edit_status option[value="' + el.data('status') + '"]').attr('selected', true);
if (el.data('is_open') != '') {
form.find('#edit_is_open').attr('checked', true);
} else {
form.find('#edit_is_open').attr('checked', false);
}
});
});
</script>

Laravel 5.7 : Update method return "No message"

I have tried everything and I can't figure out where comes my mistake.
the update() method doesn't update anaything, i only get back "No message" error...
Here's Routes in web.php:
Route::get('/user/edit/{id}', ['as' => 'users.edit', 'uses' => 'UserAdController#edit']);
Route::post('/user/update/{id}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
the view users/edit.blade.php :
<div class="container">
<br>
<h3>Edit your ad</h3>
<br>
<form method="post" action="{{route('users.update', $ad->id)}}">
<input name="_method" type="hidden" value="PATCH">
{{ method_field('post') }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" value="{{$ad->title}}">
</div>
<div class="form-group">
<label for="title">Price</label>
<input type="text" name="price" class="form-control" id="title" value="{{$ad->price}}">
</div>
<div class="form-group">
<label for="content">Your content</label>
<textarea name="content" class="form-control" id="content" rows="3">{{$ad->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-info">
</div>
</form>
</div>
#endsection
Update method from UserAdController:
public function update($id, Request $request){
$request->validate([
'title'=>'required',
'price'=> 'required|integer',
'content' => 'required'
]);
$data = \App\Ad::find($id);
$data->title = $request->get('title');
$data->price = $request->get('price');
$data->content = $request->get('content');
$data->save();
return redirect()->back()->with('success', 'Data updated');
}
I'm not a laravel dev. I just stumbled on the documentation. You should also add the csrf field to your blade
In edit.blade.php, add this after the opening <form> tag
{{csrf_field()}}
Also the parameters in your update method aren't well arranged
It should be
public function update(Request $request, $id) {
}
The second parameter($id), comes from what you've defined as your routes in web.php file
Route::post('/user/update/{id}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
Where {id} would be replaced by the original id
Try this instead
public function update(Request $request){
//your code here
}
Request->only() returns an array with one element, While validator is the most common way to handle validation for the incoming request.
use Validator;
public function update(Request $request, $id){
$v = validator($request->only('title', 'price', 'content'), [
'title' => 'required|string|max:255',
'price' => 'required|integer',
'content' => 'required',
]);
$data = request()->only('title','price','content');
$userData = ([
'title' => $data['title'],
'price' => $data['price'],
'content' => $data['content'],
]);
$data = \App\Ad::find($id);
$data->update($userData);
return response()->json($data);
}
Thank you all !!
Seems like I wasn't doing it right.
I needed to add {{csrf_field()}} in edit form and use $request->only()
I think it would be better if you use put method like this:
Route::put('ad/{ad}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
update your form to be like this:
<div class="container">
<br>
<h3>Edit your ad</h3>
<br>
<form method="post" action="{{route('users.update', ['ad' => $ad->id])}}">
<input name="_method" type="hidden" value="PATCH">
{{ method_field('put') }}
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" value="{{$ad->title}}">
</div>
<div class="form-group">
<label for="title">Price</label>
<input type="text" name="price" class="form-control" id="title" value="{{$ad->price}}">
</div>
<div class="form-group">
<label for="content">Your content</label>
<textarea name="content" class="form-control" id="content" rows="3">{{$ad->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-info">
</div>
</form>
and now your update function :
public function update(\App\Ad $ad, Request $request){
$request->validate([
'title'=>'required',
'price'=> 'required|integer',
'content' => 'required'
]);
//$data = \App\Ad::find($id);
$ad->update([
"title" => $request->title,
"price" => $request->price,
"content" => $request->content,
]);
return redirect()->back()->with('success', 'Data updated');
}
when you get use to put, delete and patch methods you can read about Route::resource and your code will be easier.

how to display form data for preview before submitting in Laravel 5.6

My View have a simple form. I want to show this data to user for review before submitting to the database. I am using Laravel 5.6
<form action="{{url('/posts')}}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title of your Post</label>
<input type="text" class="form-control" placeholder="Post Title Goes here" name="title">
</div>
<div class="form-group">
<label for="body">Post Details</label>
<textarea class="form-control" id="summernote" placeholder="Detail of Post Goes here" name="body"></textarea>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
I also want my user to edit it if he wants to make changes.
Controller:
public function store(Request $request)
{
//validate the form
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
//save data in database
$data = $request->all();
if(!empty ($data)){
try{
$newpost = new Post();
$newpost->title = $data['title'];
$newpost->body = $data['body'];
$newpost->save();
return redirect('/posts')->with('success', 'Record Added Successfuly');
}catch(\Exception $e){
// echo $e;
}
}//if ends here
}
PHP approach
Use a confirmation URL, it can either be a get or a post, i rather use post
Route::post('/posts/confirmation', 'PostController#confirmation');
Add this to your controller
public function confirmation(Request $request)
{
//validate the form
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$data['myForm'] = $request->all();
return view('posts.confirmation', $data)
}
Your confirmation view should look something like this:
<div>
// your displaying view goes here
</div>
<form action="{{url('/posts')}}" method="POST">
#csrf
<input type="hidden" name="title" value="{{ $myForm->title }}">
<textarea style="display:none;" name="body">{{ $myForm->body }}</textarea>
<button type="submit" class="btn btn-primary">Save</button>
</form>

Categories