laravel post request results in an error that method not allowed - laravel

<form id="login-form" action="brand-dashboard" method="post">
<span class"email">email</span>
<input type="email" name="email">
<span class"email">password</span>
<input type="password" name="password"><br><br>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="submit" value="login">
</form>
this is in my view page..then in my route..
Route::get('/brand-dashboard','BrandsController#auth_brand_admin');
in my Brands controller..i use the method like
public function auth_brand_admin()
{
return ('sample text');
}
but i got error when submiting the form ..the error is..
MethodNotAllowedHttpException in RouteCollection.php

Change your code to this
Route::post('/brand-dashboard','BrandsController#auth_brand_admin');
It's because you register route with GET method but send POST request.

Related

The POST method is not supported for this route in Laravel when external api sends data to me

I'm submitting these parameters to an external API at www.sample.com/gateway
<form action="www.sample.com/gateway/" method="POST">
<input type="hidden" name="MerchantCode" value="123abc">
<input type="hidden" name="MerchantRefNo" value="1234">
<input type="hidden" name="Particualrs" value="Transaction_type=Bill;Account Number=4321;Account Name=John Doe;Email Address=jd#test.com">
<input type="hidden" name="Amount" value="100">
<input type="hidden" name="PayorName" value="John Doe">
<input type="hidden" name="PayorEmail" value="jd#test.com">
<input type="hidden" name="ReturnURLOK" value="success.php">
<input type="hidden" name="ReturnURLError" value="{{ url('/paymentdetails') }}">
<input type="hidden" name="Hash" value="{{ md5('123abc' . '1234' . 100">
<button type="submit" class="btn btn-primary " value="POST TO GATEWAY"><i class="far fa-credit-card"></i> PAY NOW </button>
</form>
After which, on the ReturnURLError
Controller
public function getPaymentDetail()
{
$response = Http::get('https://sample.com/api.php');
return $response->json();
}
Route
Route::get('/paymentdetails', 'App\Http\Controllers\PaymentController#getPaymentDetail');
I am getting The POST method is not supported for this route. Supported methods: GET, HEAD. when external api sends back data to me.
I triend changing the route to post but then the error becomes The GET method is not supported in this route
Can someone help?

How to upload the pdf or doc file in laravel

I am trying to upload the pdf or doc file on the website made up in laravel. This is my blade page.
<form action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<input type="text" class="form-control-file" name="title" id="title" aria-
describedby="fileHelp", placeholder="title">
<input type="text" class="form-control-file" name="firstName" id="firstName" aria-
describedby="fileHelp", placeholder="First Name">
<input type="text" class="form-control-file" name="lastName" id="lastName" aria-describedby="fileHelp", placeholder="Last Name">
<input type="text" class="form-control-file" name="isReviewed" id="isReviewed" aria-describedby="fileHelp", placeholder="isReviewed">
<div class="col-md-6">
<input type="file" name="paper" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
This is my controller
public function fileUploadPost(Request $request)
{
$request->validate([
'firstName'=>'required',
'lastName'=>'required',
'isReviewed'=>'required',
'paper' => 'required|mimes:pdf,xlx,csv|max:2048',
]);
$Submission= new Submission;
$Submission->title= $request['title'];
$Submission->first_name= $request['firstName'];
$Submission->last_name= $request['lastName'];
$Submission->isReviewed= $request['isReviewed'];
$fileName= time().'.'.$request->paper->extension();
$old_path = Request::file('paper')->getPathName(); Storage::disk('Paper')->move($old_path,
public_path($fileName));
$Submission->save();
return back()
->with('success','You have successfully upload file.')
->with('file',$fileName);
}
I am getting an error saying that
Non-static method Illuminate\Http\Request::file() should not be called statically
to fix this i
use Illuminate\Support\Facades\Request
instead of
use Illuminate\Http\Request;
but then I get an error saying I cannot use validation. Any kind of help is appreciated.
You don't need to use the facade for this. You can still use the standard Illuminate\Http\Request class.
To get the file, you should use:
$request->file('paper')
rather than
Request::file('paper')

Change PUT request to POST request to include image update

I wrote a post edit method which at first consisted only of text. I did the update using a PUT request.
Now I want to include images to my posts, so I added it, and it works for my POST request of creating a post but doesn't work for my update post, when I want to change image, since PUT request doesn't support file uploads.
So now I'm stuck trying to change my update method from PUT request that only updates text to a POST request that updates both the text and an image if supplied.
This is the code I wrote so far:
public function update(Request $request, Post $post)
{
//store updated image
if($request->hasFile('image') && $request->file('image')->isValid()){
if($post->hasMedia('posts')) {
$post->media()->delete();
}
$post->addMediaFromRequest('image')->toMediaCollection('post', 's3');
}
$post->update(request()->validate([
'body' => 'required'
]));
return redirect($post->path());
}
I think that the $post->update doesn't work for the POST request. I just want to update a text if an update was given.
Using Laravel 6.
EDIT: My form layout structure (simplified)
<form action="action="/posts/{post}" method="POST">
#method('PUT')
#csrf
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
My routes:
Route::get('/posts/{post}/edit', 'PostsController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostsController#update');
I tried your code and it worked fine with me , only added #csrf in form tag.
<form action="/posts/{post}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>

The GET method is not supported for this route. Supported methods: POST in laravel ...is there something i'm missing?

blade file :
<form action="{{URL::to('/admin/sender')}}"method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Controller:
public function notificationSender(Request $request)
{
$text= request()->text;
print_r($request->input());
event(new OrderComplete($text));
return view('admin.sender');
}
Route:
Route::post('/sender','HomeController#notificationSender');
the route is a subroute for a group..is there something i'm missing?
Give space between method and route parameters here
if your laravel version is 5 then use this
<form action="{{URL::to('/admin/sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Or else you can also name the route to make it easy to pass in action parameter of form like this.
Route::post('/sender','HomeController#notificationSender')->name('sender');
Then you can pass it in form like this
<form action="{{route('sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
For GET Method (Laravel 6)
blade file
<form method="GET" action="{{ route('sender') }}" enctype="multipart/form-data" >
#csrf
<input type="text" name="text">
<input type="submit">
</form>
Write Controller
public function notificationSender(Request $request)
{
$text= $request->get('text');
echo "<pre>"; print_r($text);
event(new OrderComplete($text));
return view('admin.sender');
}
Route
Route::get('sender','HomeController#notificationSender');

post method show error as soon ass change post into get working better in laravel routing

here is my route
Route::post('/posts', 'Cdesigination#index');
HTNL and input Field
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="name" id="name" data-validate="required" placeholder="Enter Name" />
</div>
<div class="form-group">
<label class="control-label">Detail</label>
<textarea class="form-control" name="detail" id="detail" placeholder="Enter Detail"></textarea>
</div>
<input type="hidden" id="_token" name="_token" value="{{ csrf_token() }}">
<div class="form-group col-sm-offset-3">
<button type="submit" onclick="postdata();" id="post" class="btn btn-success">Submit Now</button>
</div>
my controller file
<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\models\Designation as desig;
class Cdesigination extends Controller
{
public function index()
{
return view('designation');
}
}
where is the error have trouble me get method working fine but post method show error as soon as change
this route show following error
Route::post('/posts', 'Cdesigination#index');
MethodNotAllowedHttpException in RouteCollection.php line 219:
as soon as change post to get like Route::get('/posts', 'Cdesigination#index');
error not show working fine tell me how solve it
Make sure your <form> has method="" that matches your route declaration
<form method="post">
for
Route::post('/posts', 'Cdesigination#index');
Or
<form method="get">
for
Route::get('/posts', 'Cdesigination#index');

Resources