Post Method request not found - php

I'm having a problem with my code now and it seems like that the route that I specify is not found whenever I try to access it.
Route:
Route::post('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
Route::get('nniscaseassociates/{id}/reliever', 'NnisCaseAssociateController#reliever');
View:
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
Controller:
public function reliever($id)
{
//this will be nniscase id then
$caseassociate = NnisCaseAssociate::findOrFail($id);
//return dd($caseassociate);
return view('nniscaseassociates.reliever', compact('caseassociate'));
}
public function pushreliever(Request $request, $id)
{
...Statements...
return redirect('nniscases/'.$caseassociates->nnis_case_id.'/edit');
}
By the end of submitting I want to redirect to my edit page and display the changes that I made from the previous form. And this is the error that I got when click submit.

You are making a PUT request and not POST.
Either remove this line from your form:
{{ method_field('PUT') }}
Or
Change your POST route to PUT:
Route::put

You have not defined PUT method in your web.php
Route::put('nniscaseassociates/pushreliever/{id}', 'NnisCaseAssociateController#pushreliever');
in your view :
<form method="POST" action="/nniscaseassociates/pushreliever/{{ $caseassociate->nnis_case_id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<...Fields...>
</form>
in your controller :
public function pushreliever(Request $request, $id)
{
echo $id;
}

You should try this:
<form method="POST" action="{{ url('nniscaseassociates/pushreliever',[$caseassociate->nnis_case_id]) }}">

Related

Laravel I cannot get into Controller

The storeClientRequest Cannot be triggered, is there any mistake here ? When I hit submit, the page shows 404 Not found
Form
<form class="needs-validation" novalidate method="POST" action="store-client-request/{{ Auth::user()->id }}">
#csrf
//////content
</form>
Route
Route::group(['prefix'=>'user', 'middleware'=>['isUser','auth']], function(){
Route::post('store-client-request/{user}', [UserController::class, 'storeClientRequest']);
});
Controller
function storeClientRequest(User $user)
{
dd('hi');
return redirect()->back()->with("message", "Create request successfully");
}
Add a name to your route :
Route::post('store-client-request/{user}', [UserController::class, 'storeClientRequest'])->name("store.client.request");
Make sure you run
php artisan optimize
Reference your route by name in the opening form tag :
action="{{ route('store.client.request', ['user' => Auth::user()->id]) }}">
That way, it doesn't matter (a) what the route prefix is (that it looks like you've forgotten to include) or (b) if the address to the route changes later down the line - the {{ route() }} blade directive will always pull in the correct URL, along with the relevant parameters.
As I see through you code: your full route is /user/store-client-request/{user}
Therefore in you action you should put
<form class="needs-validation" novalidate method="POST" action="/user/store-client-request/{{ Auth::user()->id }}">
#csrf
//////content
In your Route there is /user/store-client-request/{user}
So Add this in your Action
<form class="needs-validation" novalidate method="POST" action="/user/store-client-request/{{ Auth::user()->id }}">
</form>

How to change the boolean in DB calling a function in href?

In a Laravel context, I've got this messages page, with all the messages belonging to a specific user. Initially all messages are not readed, so I put a button to change the boolean in DB (from 0 to 1) and finally show the message.
I'm doing this:
The view
#if ($message->readed != 0)
<p class="card-text message text-left">{{ $message->message }}</p>
#else
<form method="POST" action="/message/read">
#csrf
#method('PATCH')
<input type="hidden" name="message" value="{{ $message->id }}"/>
<button class="btn btn-info text-white" type="submit">
Leggi
</button>
</form>
#endif
The route in web.php
Route::patch('message/read', 'MusicianController#readMessage');
The function
public function readMessage(Request $request)
{
$message = Message::where('id', $request->id)->first();
$message->readed = 1;
$message->update();
return redirect()->back()->with('message', 'message updated');
}
But it's not working, as soon as I click the button to show the message (and even change the DB value) I've got this error: The PATCH method is not supported for this route. Supported methods: GET, HEAD.
Even if I had specified a patch method in routes and even in the form with #method('PATCH')
Could someone help me understand what's wrong please??
the main answer
your route is:
Route::patch('message/read', 'MusicianController#readMessage');
replace your route with following route that use for all CRUD opration:
Route::resource('message/read', 'MusicianController');
if you use ajax for submit data then replace your type and url with following:
type: "patch",
url: "{{url('message/read')}}",
if you don't use ajax than use following:
<form method="POST" action="{{url('message/read"')}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
</form>
update: after version 5.6 you can use these syntax for above functions in any blade file:
<form method="POST" action="{{url('message/read"')}}">
#csrf
#method('PATCH')
</form>

Laravel - set session from form input

I cannot display session value in my view. I only want to display it to see if it's correctly set inside the controller. Is it correctly set in controller? How can I check?
I have this in view:
<div class="panel panel-success">
<form action="{{ route('get_table') }}" method="POST">
{{ csrf_field() }}
#foreach($tables as $data)
<button type="submit" name="tables" class="btn btn-primary" value="{{ $data->id }}">
{{$data->name }}
</button>
#endforeach
</form>
{{ Session::get('table_id') }}
</div>
This in ListController:
public function index() {
$tables = DB::table('tables')->get();
return view('welcome', compact('tables'));
}
public function getTable(Request $request) {
$table_id = $request->get('tables');
$request->session()->put('table_id', $table_id);
}
And this in web.php routes:
Route::get('/', 'ListController#index')->name('get_table');
Route::post('/', 'ListController#getTable');
I even tried
public function getTable(Request $request) {
$request->session()->put('table_id', '1234');
}
Nothing shows up in the view at {{ Session::get('table_id') }}
What am I doing wrong?
You can try this:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->back()->with('table_id',$table_id);
}
if you want to redirect to specific route then:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->route('RouteName')->with('table_id',$table_id);
}
and then in view:
#if(Session::has('table_id'))
{{ Session::get('table_id') }}
#endif
Hope you got your answer

Error while editing a post using Laravel update

This is my html form
<form class="form-horizontal" action="{{action('BlogController#update',[$blog->id]) }}" method="post">
<input name="method" type="hidden" value="patch"/>
<div class="form-group">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
Here is route:
Route::patch('blog/{id}','BlogController#update');
Controller :
public function update(Request $request,$id){
$input = $request->all();
$blog =findOrFail($id);
Blog::update($input);
//var_dump($input);
return back();
}
Can you please show me where is the issue?
In your code you have write $blog = findOrFail($id); to get blog which is not correct. You can do it using
$blog = Blog::findOrFail($id);
Now you have the blog, you need to update the blog. So, the update code should be
$blog->update($input);
To make this update method works, you need to make the fields(the fields you are updating) fillable in Blog model.
You're using the wrong syntax. Do something like this to make it work:
public function update(Request $request, $id)
{
Blog::where('id', $id)->update($request->all());
return back();
}
give the name whatever you wish say blog:
Route::patch('blog/{id}','BlogController#update')->name('blog');
your HTML code
<form class="form-horizontal" action="{{route('blog', $blog->id)}}" method="post">
hope this help you!!
you have many syntax problems!
try it this way:
form:
<form class="form-horizontal"
action="{{ route('blog.update', ['id' => $blog->id]) }}"
method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="patch"/>
<!-- other inputs -->
</form>
Route:
Route::any('blog/{id}','BlogController#update')->name('blog.update');
Controller:
public function update(Request $request, $id){
$blog = Blog::findOrFail($id);
$blog->update([
'key' => 'value'
]);
// never use $request->all() because of security issues!
return back();
}
<form class="form-horizontal" action="{{route('blog.update',[$blog->id]) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
Your Route Like This
Route::resource('blog', 'BlogController');
Your Controller
public function update(Request $request,$id){
$blog =Blog::findOrFail($id);
$blog->database_fieldname1=$request->value1;
$blog->database_fieldname2=$request->value2;
$blog->database_fieldname3=$request->value3;
$blog->save();
return back();
}

How to submit a form using PUT http verb in Laravel

I know that this question may have been made but I just can't get it to work. if someone could help me I would be very grateful. I have colletive/form installed but the answer can be an html form tag too.
Now listing my form, my route and my exception.
{{ Form::model( array('route' => array('casas.update', 238), 'method' => 'PUT')) }}
<input type="hidden" name="_method" value="PUT">
-
Route::resource('casas', 'CasasController');
exception:
MethodNotAllowedHttpException in RouteCollection.php line 218:
With plain html / blade
<form action="{{ route('casas.update', $casa->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}
{{-- Your form fields go here --}}
<input type="submit" value="Update">
</form>
Wirth Laravel Collective it may look like
{{ Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put']) }}
{{-- Your form fields go here --}}
{{ Form::submit('Update') }}
{{ Form::close() }}
In both cases it's assumed that you pass a model instance $casa into your blade template
In your controller
class CasasController extends Controller
{
public function edit(Casa $casa) // type hint your Model
{
return view('casas.edit')
->with('casa', $casa);
}
public function update(Request $request, Casa $casa) // type hint your Model
{
dd($casa, $request->all());
}
}

Categories