ErrorException in helpers.php line 533: - laravel

I'm making a contact form with laravel 5.4 and I want to get an email when someone sends the contact form. I'm using Mailtrap to receive emails.
The problem I'm getting is that I get this error when I submit the form.
ErrorException in helpers.php line 533:
htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs\website\app\Modules\Templates\Resources\Views\emails\contact.blade.php)
My contact function
public function contact()
{
$data = Input::all();
$rules = array(
'name' => '',
'email' => '',
'message' => '',
);
$validator = Validator::make($data, $rules);
if($validator->passes())
{
Mail::send('templates::emails.contact', $data, function($message){
$message->from(Input::get('email'), Input::get('name'));
$message->to('info#site.com', 'Info')->subject('Testing contact form');
});
Session::flash('success', 'Your message has been sent successfully.');
return back();
}else{
return back()->withErrors($validator);
}
}
and my contact.blade.php that is the information that gets sent to me
<h1>We been contacted by.... </h1>
{{ $name }}<br />
{{ $email }}<br />
{{ $subject }}<br />
{{ $message }}<br />

You're passing the $data which holds the array of input. You need to access them like shown below.
Change the data being passed to
Mail::send('templates::emails.contact', compact('data'), function($message)
Change your view code to
{{ $data['name'] }}<br/>
{{ $data['email'] }}<br/>
{{ $data['message'] }}<br/>
You're also trying to access subject in the view when isn't available in the input.

you need to Add $subject in ur cantact function or remove it from cantact.blade.php, try this code :
public function contact()
{
$cdata = Input::all();
$crules = array(
'name' => '',
'email' => '',
'subject' => '',
'message' => '',
);
$validator = Validator::make($cdata, $crules);
if($validator->passes())
{
Mail::send('templates::emails.contact', $cdata, function($message){
$message->from(Input::get('email'), Input::get('name'));
$message->to('info#site.com', 'Info')->subject('Testing contact form');
});
Session::flash('success', 'Your message has been sent successfully.');
return back();
}else{
return back()->withErrors($validator);
}
}
and some times u got an error because ur variables have the same name of stored variables ( like data,timer,for ... etc)

Related

Laravel - How to pass parameter from controller to route and use it in another controller?

I have configured a resource route as below
Route::resource('users', UserController::class);
When a user posts data, it will call the store method in the controller where it will add the data and set a message for success/failure.
public function store(Request $request)
{
// return $request;
$request->validate(
[
"firstName" => 'required',
"lastName" => 'required',
"phoneNo" => 'required',
"email" => 'email:rfc,dns'
]
);
$date = date(now());
$data = [
'firstName' => $request->firstName,
'lastName' => $request->lastName,
'phoneNo' => $request->phoneNo,
'email' => $request->email,
'designation' => $request->designation,
'status' => $request->status,
'createdAt' => $date,
'updatedAt' => $date,
];
$user = Firebase::insertData($data, static::$collection);
if ($user->id() != null) {
$message = "User Created Successfully";
} else {
$message = "Something went wrong. Please contact System Admin with error code USR001";
}
return redirect()->route('users.index', ['message' => $message]);
}
This will redirect to the index method of the same controller. How can I use the $message parameter in the index method and send it to the view? My index method is below
public function index()
{
$userCollection = app('firebase.firestore')->database()->collection('users');
$userData = $userCollection->documents();
$response = [];
$app = app();
foreach ($userData as $data) {
$user = $app->make('stdClass');
$user->firstName = $data["firstName"];
$user->lastName = $data["lastName"];
$user->phoneNo = $data["phoneNo"];
$user->email = $data["email"];
$user->designation = $data["designation"];
$user->status = $data["status"];
$user->createdAt = $data["createdAt"];
$user->updatedAt = $data["updatedAt"];
array_push($response, $user);
}
return view('pages.user.list-user', ['response' => $response]);
}
You can directly pass the message as a flash message using the with() method with the redirect method.
Edit your redirect code as:
return redirect()->route('users.index')->with('message', $message]);
and add the below code in your pages.user.list-user blade file:
#if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
Visit https://laravel.com/docs/8.x/redirects#redirecting-with-flashed-session-data for more info on redirects with a flash message.
Replace your redirect code :
return redirect()->route('users.index', ['message' => $message]);
with
return view('pages.user.list-user', ['message' => $message]);
(1) First of all, pass the message in the parameter of index function:
public function index($message)
{
...
}
(2) This is okay, you wrote correctly:
return redirect()->route('users.index', ['message' => $message]);
(3) Now just access the message in the view (blade) and print it:
{{ $message }}
You can also store message in $response array and simply pass the $response to the desired view:
$response['message'] = $message;
You can have the index method like if the parameter used in the controller.
public function index(Request $request)
{
// Your code
$message = $request['message'];
}
If you want to access the message in view use
return redirect()->route('users.index')->with('message', $message]);
and access from the view using session('message') like in OMi Shah's answer

Get Userdata from database by Login Sessions Codeigniter 3

i'm trying to make a login system. When user already login it will show user personal data like name, email or something. I'm try to modify the login code but doesn't work. Maybe someone can help.
Here is my code.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Model_user');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('user/user_login');
}
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => $password,
'status' => 1
);
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
}else{
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
}
function logout(){
$this->session->sess_destroy();
redirect('user/user_login');
}
}?>
Model
function cek_login($table,$where){
return $this->db->get_where($table,$where);
}
I'm can make username session appear,
<?php echo $this->session->userdata('username') ?> it's working.
But try to show name it doesnt appear. Appreciate any kind help. Thank you
You can try something like this.
$query = $this->Model_user->cek_login('tb_user',$where);
if($query->num_rows() > 0){
$user = $query->row();
$data_session = array(
'username' => $username,
'status' => "userlogin",
'name' => $user->name,
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
} {
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
For this to work, there must be a name column in the table tb_user
You need to store the full name in the session too.
Try editing this
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
to this
$cek = $this->Model_user->cek_login('tb_user',$where);
if($cek->num_rows() == 1){
$row=$cek->result_array()[0]; //returns an array of results, pick the first
$data_session = array(
'fullname' => $row['fullname'], //assuming column name is 'fullname'
'username' => $username,
'status' => "userlogin",
);

Redirect after data insertion in laravel with success message

Hello guyzz I want to redirect back to form for new data entry which is actually form.blade.php view. I can see the data inserted successfully but how I can redirect with success message. my code is given.
public function store(Request $request)
{
$sname = $request->input('sname');
$fname = $request->input('fname');
$gradyear = $request->input('gradyear');
$phone = $request->input('phone');
$email = $request->input('email');
$paddress = $request->input('paddress');
$prog = $request->input('prog');
$job = $request->input('job');
$org = $request->input('org');
$position = $request->input('position');
$data = array(
'sname' => $sname,
"fname" => $fname,
"gradyear" => $gradyear,
"phone" => $phone,
"email" => $email,
"paddress" => $paddress,
"prog" => $prog,
"job" => $job,
"org" => $org,
"position" => $position
);
DB::table('tests')->insert($data);
echo "Data inserted Successfully";
}
On the controller after the code
return back()->with('status', 'successfully inserted');
On the form view
#if(session('status')
{{ session('status') }}
#endif
You can format your message in the CSS you choose
In Controller:
public function store(Request $request)
{
//
return back()->with('success', 'Data inserted Successfully');
}
In Blade:
#if(session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
#endif
Assuming the above code is working the way you want, you can redirect with flashed session data:
public function store(Request $request)
{
$sname = $request->input('sname');
$fname = $request->input('fname');
$gradyear = $request->input('gradyear');
$phone = $request->input('phone');
$email = $request->input('email');
$paddress = $request->input('paddress');
$prog = $request->input('prog');
$job = $request->input('job');
$org = $request->input('org');
$position = $request->input('position');
$data = array(
'sname' => $sname,
"fname" => $fname,
"gradyear" => $gradyear,
"phone" => $phone,
"email" => $email,
"paddress" => $paddress,
"prog" => $prog,
"job" => $job,
"org" => $org,
"position" => $position);
DB::table('tests')->insert($data);
return back()->with('status', 'Data inserted Successfully!');
}
And inside your blade file you can render with something like:
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

How to return success sending an email laravel

I am trying to give a mail success response in laravel.
Here is my route:
Route::get('/contatti/','ItemController#contatti');
Route::post('/contatti/mail','ItemController#mail');
Here is my method
public function mail()
{
$data = Input::all();
$rules = array(
'nome' => 'required',
'email' => 'required',
);
$validator = Validator::make($data, $rules);
if($validator->fails()) return Redirect::to('contatti')->withErrors($validator)->withInput();
$emailcontent = array (
'nome' => $data['nome'],
'email' => $data['email'],
);
Mail::send('emails.contactmail', $emailcontent, function($message){
$message->to('xxxx #mail.com','')->subject('Contatti');
});
$success='ok';
return Redirect::back()->with('success',$success);
}
HTML
#if(isset($success))
<div>
Email Inviata con successo!
</div>
#endif
I can't do that because I am using a route method post?
I need to use something like :
Session::flash('success', 'Successfully sent');
to set this? And then get the session in the Contact page?
According to documentation:
Note: Since the with method flashes data to the session, you may
retrieve the data using the typical Session::get method.
So, your HTML template should be:
#if(Session::get('success'))
<div>
Email Inviata con successo!
</div>
#endif

Laravel 4 Displaying user friendly validation errors

I have a controller that validates some input and when validation fails it passes the errors to my view:
public function updateAccount(){
$user = Auth::user();
$validation = User::validateAccount(Input::all());
if( $validation->passes() ){
$user->fill(Input::all());
$user->save();
return Redirect::back();
} else {
return Redirect::back()
->withErrors($validation)
->withInput();
}
}
The code for User::validateAccount(); looks like this:
public static function validateAccount($input){
$rules = [
'website' => 'url'
];
$validation = Validator::make($input, $rules);
return $validation;
}
In my view I display the errors like this:
#if($errors->any())
<div class="errors">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
However, instead of the default, user friendly error output I get this. For a URL validation error it displays:
validation.url
How do I get Laravel to display the default, user friendly error messages that are configured in app/lang/en/validation.php?
So for a URL error this should be:
"The :attribute format is invalid."
You need to define the custom message.
Change
public static function validateAccount($input){
$rules = [
'website' => 'url'
];
$validation = Validator::make($input, $rules);
return $validation;
}
to
public static function validateAccount($input){
$messages = [ 'url' => 'You must give a valid url'];
$rules = [ 'website' => 'url' ];
$validation = Validator::make($input, $rules, $messages);
return $validation;
}
OR
You can add this to your app/lang/en/validation.php file:
'custom' => array(
'website' => array(
'url' => 'You must give a valid url',
),
),

Resources