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

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?

Related

How to redirect form to different url on different button click

i am making a blog on Laravel and i am trying to redirect form to different url based on different button click . you can also refer my code
<form action="{{url('/storepost')}}" id="submitform" method="POST" enctype="multipart/form-data">
#csrf
<input type="text" name="blogtitle" placeholder="Enter the blog Title" class="form-control" >
<textarea class="form-control" id="editor" name="editor" rows="3"></textarea>
<button class="btn btn-success" type="submit">Publish</button>
<span><button class="btn btn-warning">Save as Draft</button></span>
</form>
as i have two buttons that is publish and save as draft and i want to redirect the form to different url based on button clicks , i don't understand how to do this .
can anybody help me out on this .
Thanks .
You can submit your form with a name and in the controller check for the name of the input.
<form action="{{url('/storepost')}}" id="submitform" method="POST" enctype="multipart/form-data">
#csrf
<input type="text" name="blogtitle" placeholder="Enter the blog Title" class="form-control" >
<textarea class="form-control" id="editor" name="editor" rows="3"></textarea>
<input class="btn btn-success" type="submit" value="publish" name="publish" />
<input class="btn btn-success" type="submit" value="Save as Draft" name="draft" /></span>
</form>
And in the controller check for the input:
Controller
if(\request()->has('draft')){
\\ Do draft
} else {
\\ Do publish
}
There is a way you could work around with it
you need to change your button tag to input:submit and add a name="type" for example
and in your backend, you could check this value
<form action="{{url('/storepost')}}" id="submitform" method="POST" enctype="multipart/form-data">
#csrf
<input type="text" name="blogtitle" placeholder="Enter the blog Title" class="form-control" >
<textarea class="form-control" id="editor" name="editor" rows="3"></textarea>
<input type="submit" class="btn btn-success" name="type" value="Publish">
<span><input type="submit" class="btn btn-warning" name="type" value="Save as Draft"></span>
</form>
after that in your controller, you could simply check the value like this
public function store(Request $request){
if($request->input('type') == 'Publish'){
// do something
} else {
// do another thing
}
}

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>

zoho remote api normal form server side code

I am using zoho remote api for normal form, but i get error whenever i try to save my document Please help me to correct my code that is given below
i need help to save my document. every time i save the document get the error "unable post the content"
<form accept-charset="UTF-8" target="_blank" action="https://sheet.zoho.com/remotedoc.im" method="POST">
<input type="hidden" value="http://example.com/demo1/test.csv" name="url">
<input type="hidden" value="**********" name="apikey">
<input type="hidden" value="editor" name="output">
<input type="hidden" value="normaledit" name="mode">
<input type="hidden" value="test.csv" name="filename">
<input type="hidden" value="en" name="lang">
<input type="hidden" value="12345678" name="id">
<input type="hidden" value="csv" name="format">
<input type="hidden" value="save.php" name="saveurl">
<input c type="submit" value="Details" name="submit">
</form>
<?php
$filepath = '/home/spatials/public_html/demo1/'.$_FILES['content']['name'];
$tmp_filename = $_FILES['content']['tmp_name'];
$upload_status = move_uploaded_file($tmp_filename, $filepath);
?>
Pleas correct my code
Wrong Save URL:
<input type="hidden" value="php/save.php" name="saveurl" />
Correct Save URL:
<input type="hidden" name="saveurl" value="http://example.com/demo1/save.php" />
WIKI page link for reference: https://apihelp.wiki.zoho.com/Save-Document.html

laravel post request results in an error that method not allowed

<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.

Resources