Laravel 5.1 passing data to mail view doesn't work - php

So, I'm trying to make an e-mail view, using data the user posted. The problem is, that specific data is unreachable. I don't know how I'm supposed to get that data.
Here is my controller:
public function PostSignupForm(Request $request)
{
// Make's messages of faults
$messages = [
//removed them to save space
];
//Validation rules
$rules = [
//removed them to save space
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return Redirect::back()->withInput()->withErrors($validator);
}
DB::table('rittensport')->insert([
'note' => $request->get('note'),
//standard instert
]);
/**
* Sending the e-mails to the pilot and co-pilot
*
* #return none
*/
Mail::send('emails.rittensport_signup', $request->all(), function ($message) {
$message->from(env('APP_MAIL'), 'RallyPodium & Reporting');
$message->sender(env('APP_MAIL'), 'RallyPodium & Reporting');
$message->to($request->get('piloot_email'), strtoupper($request->get('piloot_lastname')).' '.$request->get('piloot_firstname'));
$message->to($request->get('navigator_email'), strtoupper($request->get('navigator_lastname')).' '.$request->get('navigator_firstname'));
$message->subject('Uw inschrijving voor de RPR Gapersrit '. date('Y'));
$message->priority(1);//Highest priority (5 is lowest).
});
return Redirect::back();
Well, the view exists and the error I'm facing to is:
Undefined variable: request.
This is how I try to get the data in the e-mail view: {{ $request->get('note') }} I already tried things like {{ $message->note }}, $message['note'] And so on.

Try this:
Mail::send('emails.rittensport_signup', array("request" => $request), function (...

Related

How to change error message Title is required in Laravel [duplicate]

This question already has answers here:
How to return custom error message from controller method validation
(5 answers)
Closed 4 years ago.
Good day to all, I want to change default error message as "Title is required" to "Please enter title" The code I use:
Controller
$this->validate($request, [
'Title'=>'required',
]);
Also, how can I ensure that a user cannot save the same data into database, for example, if there is already a Title as Movie 43 we do not have to let user save that Title again in the database.
The signature of the validate function is:
public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
You can pass in custom messages as the third parameter. The key of the custom message can be either field_name for all errors relating to that field, or you can be more specific and use field_name.rule. In this case you should use:
$this->validate(
$request,
['Title' => 'required'],
['Title.required' => 'Please enter title']
);
use Validator;
if you have much more validations this could be better
$validator = Validator::make($request->all(), $rules, $messages);
Try this
$rules = [
'Title'=>'required|unique'
];
$messages = [
'Title.required' => 'Please Enter Title',
'Title.unique' => 'Please Enter Unique Title'
];
$validator = Validator::make(Input::all(), $rules, $messages);
And above declaration of controller class
use Validator;
use Illuminate\Support\Facades\Input;
Hope it helps you!

Laravel 5 make a custom register function

Laravel 5 has it's default register function which is in
public function postRegister(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}
I know that I can copy this code and paste it in my AuthController but there's a change that I need to make which I don't know where to start and find. What I want is change the code for the insertion of data in my users table. I want to change this because I add another column in my users table which is company_name and I have a table which is named companies so basically when the user enter a company_name for registration it will check the companies table if it is existing then return error message if it is. So think there is something like:
$rules = array(
'company_name' => 'unqiue:companies',
);
But i don't know where to put this thing in my registration code. Thanks
You can use a custom validation, in this case. Make sure, you a are calling $this->validate(), and not $this->validator. This validate will automatically redirect back with errors if it fails, so you can skip the check statement.
public function postRegister(Request $request)
{
$this->validate($request->all(), [
'company_name' => 'unique:companies',
// And the other rules, like email unqiue, etc..
]);
Auth::login($this->create($request->all()));
return redirect($this->redirectPath());
}

Laravel "Serialization of 'Closure' is not allowed"

When I go to store a dataset in Laravel, I sometimes get this error and haven't found a solution to it.
Serialization of 'Closure' is not allowed
Open: ./vendor/laravel/framework/src/Illuminate/Session/Store.php
*/
public function save()
{
$this->addBagDataToSession();
$this->ageFlashData();
$this->handler->write($this->getId(), serialize($this->attributes));
$this->started = false;
Here is the function being called when the error occurs:
public function store()
{
$data = Input::all();
$validator = array('first_name' =>'required', 'last_name' => 'required', 'email' => 'email|required_without:phone', 'phone' => 'numeric|size:10|required_without:email', 'address' => 'required');
$validate = Validator::make($data, $validator);
if($validate->fails()){
return Redirect::back()->with('message', $validate);
} else {
$customer = new Customer;
foreach (Input::all() as $field => $value) {
if($field == '_token') continue;
$customer->$field = $value;
}
$customer->save();
return View::make('admin/customers/show')->withcustomer($customer);
}
}
What is causing this serialization error?
Just replace following line:
return Redirect::back()->with('message', $validate);
with this:
return Redirect::back()->withErrors($validate);
Also, you may use something like this (To repopulate the form with old values):
return Redirect::back()->withErrors($validate)->withInput();
In the view you can use $errors variable to get the error messages, so if you use $errors->all() then you'll get an array of error messages and to get a specific error you may try something like this:
{{ $errors->first('email') }} // Print (echo) the first error message for email field
Also, in the following line:
return View::make('admin/customers/show')->withcustomer($customer);
You need to change the dynamic method to withCustomer not withcustomer, so you'll be able to access $customer variable in your view.
return Redirect::back()->with('message', $validate);
You are telling Laravel to serialize the entire validator object into session. To redirect with errors, use the withErrors method:
return Redirect::back()->withErrors($validate);
This will take the error messages out of the validator and flash those to session prior to redirecting. The way you're doing it now you're trying to store the entire class in Session which is causing your error.
Another issue I see is that I don't think there's a withcustomer method on the View class:
return View::make('admin/customers/show')->withcustomer($customer);
Try changing that to either just with:
return View::make('admin/customers/show')->with('customer', $customer);
or make sure to capitalize the Customer portion:
return View::make('admin/customers/show')->withCustomer($customer);
See also this question.

Laravel Message Bag Error

Am working on form validations for newsletter for a project am on, the news letter form appears on every page so it will also appear on the longin and registration page so i decided to make use of Laravel Message Bags to store the news letter errors but it keeps giving me an undefined property error on the actual page i check and output echo the errors, i don't know if am doing something wrong here are the details though!
The Error:
Undefined property: Illuminate\Support\MessageBag::$newsletter
My code In the Controller:
return Redirect::back()->withInput()->withErrors($inputs, "newsletter");
My code in the View:
#if($errors->newsletter->any())
<p>
{{$errors->newsletter->any()}}
</p>
Code in controller:
$post_data = Input::all();
$validator = Validator::make(Input::all(),
array(
'email' => 'required',
'password' => 'required'
));
if ($validator->fails()) {
return Redirect::back()
->withInput()
->withErrors(['auth-validation' => 'ERROR: in validation!']);
}
Code in vew:
#if($errors->any())
#foreach($errors->getMessages() as $this_error)
<p style="color: red;">{{$this_error[0]}}</p>
#endforeach
#endif
The RedirectResponse class function withErrors() doesn't have a second parameter..
The function vendor\laravel\framework\src\Illuminate\Http\RedirectResponse.php -> withErrors():
/**
* Flash a container of errors to the session.
*
* #param \Illuminate\Support\Contracts\MessageProviderInterface|array $provider
* #return \Illuminate\Http\RedirectResponse
*/
public function withErrors($provider)
{
if ($provider instanceof MessageProviderInterface)
{
$this->with('errors', $provider->getMessageBag());
}
else
{
$this->with('errors', new MessageBag((array) $provider));
}
return $this;
}
So, if you really want to use the MessageBag then this should work (didn't test it):
$your_message_bag = new Illuminate\Support\MessageBag;
$your_message_bag->add('foo', 'bar');
return Redirect::back()->withInput()->withErrors($your_message_bag->all());
withErrors should receive the messages from validator object. After your validation process something like:
$validation = Validator::make(Input::all(), $validation_rules);
if (!$validation->passes()){
return Redirect::back()->withInput()->withErrors($validation->messages());
}
I hope it works fine for you.
You can write a function like this
if(!function_exists('errors_for')) {
function errors_for($attribute = null, $errors = null) {
if($errors && $errors->any()) {
return '<p class="text-danger">'.$errors->first($attribute).'</p>';
}
}
}
then in your View
<div class="form-group">
<label for="bio">Bio</label>
<textarea class="form-control" name="bio"></textarea>
</div>
{!! errors_for('bio',$errors) !!}
learnt from Jeffrey Way on Laracasts

Laravel 3 Validation of a single input form

I am using spectacular Laravel Framework but i have a validation issue that i cannot solve in any way trying to validate a single email field.
My form input is:
{{ Form::email('email', Input::old('email'), array('id' => 'email', 'class' => 'span4', 'placeholder' => Lang::line('newsletter.email')->get($lang), 'novalidate' => 'novalidate')) }}
My controller
public function post_newsletter() {
$email = Input::get('email');
$v = Newsletter::validator($email, Newsletter::$rules);
if($v !== true)
{ ... }
else
{ ... }
}
and my model
class Newsletter extends Eloquent {
/**
* Newsletter validation rules
*/
public static $rules = array(
'email' => 'required|email|unique:newsletters'
);
/**
* Input validation
*/
public static function validator($attributes, $rules) {
$v = Validator::make($attributes, $rules);
return $v->fails() ? $v : true;
}
}
I ve done this so many times with success with much complicated forms but now even if i enter a valid email i get an error message about required fied etc Am I missing something? Maybe is the fact that i try to validate just one single field? I really don't get it why this happens.
I believe this might be because you're not passing in an array of attributes (or an array of data, basically).
Try using the compact function to generate an array like this.
array('email' => $email);
Here is how you should do it.
$v = Newsletter::validator(compact('email'), Newsletter::$rules);

Categories