Alert Message not showing in Laravel 5.4 - laravel

I have got a contact form that is build using Laravel 5.4, parsley.js and Bootstrap 3, it works fine but it does not display a success message at the top of the form on successful delivery of a message or display any errors in case there is an error. Please assist?
Contoller
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#kapsol.com');
});
Session::flash('success', 'Hello $name, Your Form was successfully sent');
return redirect()->route('pages.index');
}
Index.blade.php
<div class="col-sm-4">
{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!}
<div id="success">
<div class="col-sm-12">
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Route
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
Route::post('/', 'PagesController#postIndex') ->name('index.post');

You are redirecting to the same route your form is posting. I assume that the route that render the view is a GET route and have another name, you should redirect to that route or the route that renders index.blade.php

This is why you have the with() method to chain the route() method on. So rather than use Session:flash(), you can simply add a with() method that flashes that message to the session for the next request:
return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent');
Or rather if your form was originally from the index page, then you simply don't have to remember the name of the route, simply use back() helper method, i.e:
return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');

Related

Laravel 8.0 Error "Action App\Http\Controllers\SkladController not defined."

This is my problem in SkladController. I want to fetch data from the DB but it does show me anything because I have that error in the picture.
SkladController
class SkladController extends Controller
{
public function index()
{
$sklads = Sklad::all();
return view('sklads.index')->with('sklads', $sklads);
}
public function create()
{
return view('sklads.index');
}
public function store(Request $request)
{
$this->validate($request, [
'datle' => 'required',
'mandle' => 'required',
'marcipan' => 'required',
'orechy' => 'required',
]);
//vytvorit v sklade
$sklads = new Sklad;
$sklads->datle = $request->input('datle');
$sklads->mandle = $request->input('mandle');
$sklads->marcipan = $request->input('marcipan');
$sklads->orechy = $request->input('orechy');
$sklads->save();
return redirect('/sklad')->with('success', 'Uložené');
}
}
index.blade.php
<h3> Príjem v sklade</h3>
<br>
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-md-2">
{{ Form::number('datle', '', ['class' => 'form-control', 'placeholder' => 'Ďatle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('mandle', '', ['class' => 'form-control', 'placeholder' => 'Mandle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('marcipan', '', ['class' => 'form-control', 'placeholder' => 'Marcipán']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('orechy', '', ['class' => 'form-control', 'placeholder' => 'Orechy']) }}
</div>
<div class="form-group col-md-2">
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
#if(count($sklads) > 0)
#foreach($sklads as $sklad)
<br>
{{$sklad->datle}} {{$sklad->mandle}} {{$sklad->marcipan}} {{$sklad->orechy}}
#endforeach
#else
<p>nenasli sa zaznamy</p>
#endif
web.php
Route::resource('/sklad', App\Http\Controllers\SkladController::class)
->except(['create', 'store', 'update', 'destroy']);
The issue is the action in
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
It should be
'action' => 'App\Http\Controllers\SkladController#index'
Usually you'd use the store method but you only don't have it according to your route declaration.
or you could use route instead.
{!! Form::open(['route' => 'sklads.index', 'method' => 'POST']) !!}

Email Sending and Validation Failing in Laravel 5.4

I am creating a contact form using bootstrap3 build on Laravel 5.4. When I click the submit button, I expect an email to be sent to my inbox or if there are errors they should be validated on the back end and errors displayed at the top of the form. I am using Laravel collective to build the form, when I fill the form and click the submit button, the page only reloads and no validation happens or in case of correct input no email is sent. Please assist?
Form Section
<div class="col-sm-4 wow animated fadeInLeft">
<div id="success" class="col-sm-12">
#if(Session::has('success'))
<span class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</span>
#endif
#if(count($errors) > 0)
<span class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</span>
#endif
</div>
{!! Form::open(array('route' => 'index.post', 'method' => 'POST','class' => 'contact-form')) !!}
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => ''))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => ''))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Routes File
Route::post('/', 'PagesController#postIndex') ->name('index.post');
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
PagesController
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#pwebk.com');
});
Session::flash('success', 'Hello '.$name.', Your Form was successfully sent');
return redirect()->route('pages.index');
}
public function getIndex(){
return view('pages.welcome');
}

Laravel 5.4 - send mail

i have web site make in Laravel 5.4. I have contact form and try to send mail but when send i got in mail this data
Name: {{ $name }}
Email: {{ $email }}
Message: {{ $message1 }}
In laravel 5.1 I got the data but in Laravel 5.4 I can not pass data.
My web.php
Route::post('mailContact', 'SiteController#postEmailContact');
My contorller:
protected function postEmailContact() {
Mail::send('requestContact', array(
'name' =>Input::get("name"),
'email' =>Input::get("email"),
'message1' =>Input::get("message1")
), function ($message) {
$message->from('myMail#gmail.com', 'Contact');
$message->to('yourMail#gmail.com')->subject('Contact');
});
return redirect('/');
}
and my requestContact.blade.php
Name: {{ $name }}
Email: {{ $email }}
Message: {{ $message1 }}
and contact.blade.php
{!! Form::open(array('url' => 'mailContact','class'=>'form-group')) !!}
<div id="content-page" class="content group">
<div class="hentry group">
<div class="usermessagea"></div>
<label for="name-contact-us">
Name
</label>
<div class="input-prepend"> {!! Form::text('name', null, array('class' => 'form-control','placeholder' => 'ime')) !!}</div>
<div class="msg-error"></div>
<label for="email-contact-us">
Email
</label>
<div class="input-prepend"> {!! Form::text('name', null, array('class' => 'form-control','placeholder' => 'email')) !!}</div>
<div class="msg-error"></div>
<label for="message-contact-us">
Message
</label>
<div class="input-prepend"> {!! Form::textarea('message1', null,
array( 'placeholder'=>'message',
'class'=>'form-control'
)) !!}</div>
</br>
{!! Form::submit('send' , array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
Any idea? How to pass data?
You should try this:
Please change email field
{!! Form::text('name', null, array('class' => 'form-control','placeholder' => 'email')) !!}
to:
{!! Form::text('email', null, array('class' => 'form-control','placeholder' => 'email')) !!}
Updated answer
protected function postEmailContact() {
$data = array(
'name' =>Input::get("name"),
'email' =>Input::get("email"),
'message1' =>Input::get("message1")
);
Mail::send('requestContact',$data, function ($message) {
$message->from('myMail#gmail.com', 'Contact');
$message->to('yourMail#gmail.com')->subject('Contact');
});
return redirect('/');
}
Follow 3 step only
1] configure in .evn file at root dir. as above
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username#gmail.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls
2]create controller
use Mail;
class mailController extends Controller
{
public function send(){
Mail::send(
['text' => 'post.mail'], //e.g post/mail.blade.php <view file mentioned here>
['name' => 'Name'],
function($message){
$message->to('username#gmail.com','To username');
$message->subject('test email yagnesh');
$message->from('username#gmail.com','from username');
}
);
}
}
AND create view file <post/mail.blade.php> set this name
3] run command at root dir. to Restart server <php artisan serve>
And
U can allow google less security at [https://www.google.com/settings/security/lesssecureapps][1]
Just enabled
4] create Route
//for send mail
Route::get('/send','mailController#send');
and run 'send' keyword in your url.
For more visit <https://www.youtube.com/watch?v=a08ouL3wjjQ&list=PLe30vg_FG4OQz1yZq0z19ZuWD_C3MZbA4&index=26>
Good luck!!!

htmlentities error when passing from a modal

New to Laravel, please bare with.
Error:
htmlentities() expects parameter 1 to be string, object given (View: /var/www/html/willow/resources/views/emails/valuation.blade.php)
The modal from which it is being sent:
{!! Form::open(['action' => ['EnquiryController#valuationRequest']]) !!}
<div class="form-group">
{!! Form::text('name', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Name']) !!}
</div>
<div class="form-group">
{!! Form::text('email', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Email Address']) !!}
</div>
<div class="form-group">
{!! Form::text('telephone', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Telephone Number']) !!}
</div>
<div class="form-group">
{!! Form::text('house_number', null, ['class' => 'form-control has-feedback', 'placeholder' => 'House name / number']) !!}
</div>
<div class="form-group">
{!! Form::text('postcode', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Postcode']) !!}
</div>
<div class="form-group">
{!! Form::textarea('message', null, ['class' => 'form-control has-feedback', 'placeholder' => 'Message', 'rows' => '5']) !!}
</div>
<div class="form-group">
<input type="submit" class="button black" value="Register">
</div>
{!! Form::close() !!}
and the function:
public function valuationRequest(ValuationRequest $request)
{
// dd($request->all());
Mail::send('emails.valuation',
['name' => $request['name'],
'email' => $request['email'],
'telephone' => $request['telephone'],
'house_number' => $request['house_number'],
'postcode' => $request['postcode'],
'message' => $request['message'],
],
function ($message) use ($request) {
$message->to('paolo#bigg.co.uk', 'Paolo Resteghini')->subject('Valuation Request - Willow Lettings');
});
Session::flash('flash_message', 'Your request has been sent.');
return redirect(URL::previous());
}
The contents of the DD are perfect. All of the requests are populated as expected, but when trying to go through the rest of the function it fails with the error above.
emails.valuation:
Hello, <br><br>
You have received a new valuation request via the Willow Lettings website. Here they are: <br><br>
<b>Name:</b> {{ $name }}<br>
<b>Email:</b> {{ $email }}<br>
<b>Phone:</b> {{ $telephone }}<br>
<b>House number:</b> {{ $house_number }}<br><br>
<b>Postcode:</b> {{ $postcode }}<br><br>
{{ $message }}
Most likely, this is a problem with your message variable. As you can see from the docs:
Note: A $message variable is always passed to e-mail views, and allows the inline embedding of attachments. So, you should avoid passing a message variable in your view payload.
In other words, you should change message into something else like msg.
'msg' => $request['message'],
Then, in your blade file, reflect that change:
{{ $msg }}

Laravel validation not showing the default error messages

I am working on a laravel project and I am having some issues with the validation.
The default error messages dont appear instead I get to see the validation requirements like this: http://gyazo.com/681e9d8e2e176a29d90db041354f7177
this is my code:
routes.php (I put all the code in here for now)
Route::filter('checkLogin', function()
{
if(Input::GET('email') != ""){ //register
$rules =
array(
'username' => 'required|max:64|min:3|unique:users',
'password' => 'required|max:64|min:6',
'fname' => 'required|max:255|alpha',
'lname' => 'required|max:255|alpha',
'email' => 'required|max:255|email',
'phone' => 'max:24|min:9',
'zip' => 'required',
'street' => 'required|max:255|alpha',
'housenumber' => 'required|max:6|numeric',
'country' => 'required',
'avatar' => 'max:32'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('/')->withInput()->withErrors($rules);
}
}
});
this is how the code from the view:
<div class="fields">
<div class="field">
<i class="fa fa-user"></i>
{{ Form::text('username', null, ['placeholder' => 'Username', 'tabindex' => 1]) }}
{{ $errors->first('username') }}
</div>
<div class="field">
<i class="fa fa-lock"></i>
{{ Form::password('password', ['placeholder' => 'Password', 'tabindex' => 2]) }}
{{ $errors->first('password') }}
</div>
</div>
When validation fails, you are returning your $rules as the errors. Change this line:
return Redirect::to('/')->withInput()->withErrors($rules);
to this:
return Redirect::to('/')->withInput()->withErrors($validator);

Resources