Contact Form error MethodNotAllowedHttpException - laravel

I'm trying to make a Contact form in Laravel
when i submit the message i got this error
i'm mostly new to laravel so i don't know why it's appearing
Routes.php
Route::post('/contact/sendmail', [
'uses' => 'ContactMessageController#postSendMessage',
'as' => 'contact.send'
]);
ContactMessageController.php
class ContactMessageController extends Controller
{
public function postSendMessage(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'name' => 'required|max:100',
'subject' => 'required|max:140',
'message' => 'required|min:10'
]);
$message = new ContactMessage();
$message->email = $request['email'];
$message->sender = $request['name'];
$message->subject = $request['subject'];
$message->body = $request['message'];
$message->save();
return redirect()->route('contact')->with(['success' => 'Message Succesfully sent']);
}
}
and
contact.blade.php
#extends ('layouts.master')
#section('title')
Contact
#endsection
#section('styles')
<link rel="stylesheet" href="{{ URL::secure('src/css/form.css') }}" />
#endsection
#section('content')
#include('includes.info-box')
<form action="{{ route('contact.send') }}" mathod="post" id="contact-form">
<div class="input-group">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" value="{{ Request::old('name') }}" />
</div>
<div class="input-group">
<label for="email">Your E-Mail</label>
<input type="text" name="email" id="email" value="{{ Request::old('email') }}" />
</div>
<div class="input-group">
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" value="{{ Request::old('subject') }}" />
</div>
<div class="input-group">
<label for="message">Your Message</label>
<textarea name="message" id="message" rows="10">{{ Request::old('message') }}</textarea>
</div>
<button type="submit" class="btn">Submit Message</button>
<input type="hidden" value="{{ Session::token() }}" name="_token" />
</form>
#endsection
I cant find why i got this error
MethodNotAllowedHttpException in RouteCollection.php line 218:

Just a typo. You need to change mathod="post" to method="post".

Related

Once the password has been reset, I cannot log into my application

I'm following the documentation on this link about resetting the password:
Reset Password
So first I create the view containing a form just to request the email and once the email has been received I click on the button to reset the password.
So far everything ok! Once I reset the password I try to log into my app with the new password but I cannot log in with this new password.
Can anyone kindly tell me where the problem lies? Thank you all
Route:
Route::get('/forgot-password', [Controller::class,'passwordRequest'])->middleware('guest')->name('password.request');
Route::post('/forgot-password', [Controller::class,'passwordEmail'])->middleware('guest')->name('password.email');
Route::get('/reset-password/{token}', [Controller::class,'passwordReset'])->middleware('guest')->name('password.reset');
Route::post('/reset-password', [Controller::class,'passwordUpdate'])->middleware('guest')->name('password.update');
Controller:
public function passwordRequest() {
return view('auth.forgot-password');
}
public function passwordEmail(Request $request) {
$request->validate(['email' => 'required|email']);
$status = Password::sendResetLink(
$request->only('email')
);
return $status === Password::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}
public function passwordReset($token) {
return view('auth.reset-password', ['token' => $token]);
}
public function passwordUpdate(Request $request) {
$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
]);
$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60));
$user->save();
event(new PasswordReset($user));
}
);
return $status === Password::PASSWORD_RESET
? redirect()->route('login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
View:
ForgotPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
ResetPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
You're almost done! In your auth.reset-password view, you must send the request to the password.update route, not the password.email route.
The password.update route will run the passwordUpdate method to update the User's password.
https://laravel.com/docs/9.x/passwords#password-reset-handling-the-form-submission
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{ route('password.update') }}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>

Laravel $request->all() is empty

I start study laravel.
The route to contacts page
Route::match(['get', 'post'], '/contacts', [ 'uses' => 'Admin\ContactController#show', 'as' => 'contacts' ] );
class ContactController extends Controller {
public function show( Request $request ) {
print_r( $request->all() );
return view( 'default.contacts', [ 'title' => 'Contacts' ] );
}
}
Form
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
When i submit the form, i get an array with token.
Array
(
[_token] => JMTxTwh5Cb4sPeDjGcVetgTt2yGy6mDsFs6jW3Tx
)
What can be a problem?
Thanks for answer.
You are missing the name in your inputs. Please add to them.
<form method="post" action="{{ route('contacts') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" name="name" id="inputEmail4" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" name="address" class="form-control" id="inputAddress" placeholder="1234 Main St">
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>

Getting values to pass in Payfast

I'm trying to intergrate Payfast with my laravel project. The problem I'm having is that the data isn't being passed to Payfast so Payfast keeps giving me an error saying The supplied variables are not according to specification:
Here is my code in my controller
public function payPayfast(Request $request)
{
$data = [
'merchant_id' => $request->merchant_id,
'merchant_key' => $request->merchant_key,
'return_url' => $request->return_url,
'cancel_url' => $request->cancel_url,
'm_payment_id' => $request->m_payment_id,
'amount' => $request->amount,
'item_name' => 'Test Item From Controller',
'item_description' => 'This is a test product',
'email_confirmation' => '1',
'confirmation_address' => '',
'payment_method' => $request->payment_method,
'signature' => $request->signature,
];
return redirect()->to('https://sandbox.payfast.co.za/eng/process')->with('data', $data);
}
here is my code in my confirmation.blade.php
<div class="content_wrapper">
<h1>Order Confrimation</h1>
<div class="row">
<div class="col-lg-12">
<form action="{{ route('payfast.payPayfast') }}" method="POST">
#csrf
<input type="hidden" name="merchant_id" value="11111111">
<input type="hidden" name="merchant_key" value="2222222222222">
<input type="hidden" name="return_url" value="{{ route('payfast.success') }}">
<input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}">
<input type="hidden" name="notify_url" value="{{ route('payfast.notify') }}">
<input type="hidden" name="m_payment_id" value="01AB">
<input type="hidden" name="amount" class="completePrice" value="{{ $total }}">
<input type="hidden" name="item_name" value="Test Item">
<input type="hidden" name="item_description" value="A test product">
<input type="hidden" name="email_confirmation" value="1">
<input type="hidden" name="confirmation_address" value="test#test.com">
<input type="hidden" name="payment_method" value="eft">
<?php
$success = url('payfast-success');
$cancel = url('payfast-cancel');
$notify = url('payfast-notify');
$original_str = getAscii('merchant_id=11111111&merchant_key=2222222222222&return_url='.$success.'&cancel_url='.$cancel.'&notify_url='.$notify.'&m_payment_id=01AB&amount='.$totalPrice.'&item_name=Test Item&item_description=A test product&email_confirmation=1&confirmation_address=test#test.com&payment_method=eft');
$hash_str = hash('MD5', $original_str);
$hash = strtolower($hash_str);
?>
<input type="hidden" name="signature" value="{{ $hash }}">
<button class="btn btn-dark" type="submit"><img src="{{ asset('img/eft-payfast.jpg') }}"></button>
</form>
</div>
</div>
</div>
The with function adds the data to the session, but PayFast expects the data to be in the url, e.g. https://sandbox.payfast.co.za/eng/process?amount=100&merchant_id=12&merchant_key=23&item_name=cool.
It doesn't seem like the URLGenerator provides an easy way to turn an array into query parameters. You could simply construct the query using string concatenation or have a look at this answer which includes a function to construct such a path.

How to upload picture and store to database in Laravel 5

I need to upload picture and store to database in Laravel 5.
My current code is:
Form:
<form method="POST" enctype="multipart/form-data" action="{{ url('products/new) }}">
{!! csrf_field() !!}
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" required>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Controller:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:100|unique:products',
]);
$input = $request->all();
Product::create($input);
return redirect('products');
}
The function $request->hasFile('image')) returns false.
hasFile('image') returns false because there is not an input with name 'image', only the id is 'image'
You should give it a name property, since that's the way the inputs are sent through http.
Instead of this:
<input type="file" id="image">
use this
<input name="image" type="file" id="image">
and in your validator use this:
$this->validate($request, [
'image' => 'required|max:100|unique:products',
]);

dd($request->country_flag); returns null in laravel

This is my form to upload a file:
<div class="col-lg-6 col-lg-offset-3">
<form method="post" action="{{ route('admin.country.store') }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="country_id">Country ID</label>
<input type="number" class="form-control" id="country_id" name="country_id">
</div>
<div class="form-group">
<label for="country_name">Country name</label>
<input class="form-control" type="text" id="country_name" name="country_name">
<p class="danger">{{ $errors->first('country_name') }}</p>
</div>
<div class="form-group">
<label for="alternate_title">Alternate Title</label>
<input class="form-control" type="text" id="alternate_title" name="alternate_title">
<p class="danger">{{ $errors->first('alternate_title') }}</p>
</div>
<div class="form-group">
<label for="country_flag">Country Flag</label>
<input class="" type="file" id="country_flag" name="country_flag">
<p class="danger">{{ $errors->first('country_flag') }}</p>
</div>
<div class="btn-group" role="group">
<button class="btn btn-default" type="reset">Reset</button>
<button class="btn btn-success" type="submit">Upload</button>
</div>
</form>
This is a function in my controller to handle form request.
public function store(Request $request)
{
$new_country = new SelectCountry();
$message = [
'required' => "This field can not be empty",
];
$this->validate($request, [
'country_name' => 'required',
'alternate_title' => 'required',
'country_flag' => 'required',
], $message);
dd($request->country_flag);
}
When I do dd($request->country_flag);, it returns null. It seems like file is not uploaded by the form.
What am I doing wrong?
I'm not sure if you can or not access a file like you are doing. Try this:
$file = $request->file('country_flas');
Try this
$input = $request->all();
$file = $input['country_flag'];
dd($file);

Resources