I have a form
{{ Form::open(array('route' => 'submit.contactAct', 'class' => 'form-horizontal')) }}
{{ Form::label('fremail', trans('people.email')) }} <span class="req">*</span>
{{ Form::text('fremail', Input::old('fremail'), array('class' => 'form-control')) }}
{{ $errors->first('fremail', '<span class="help-block alert alert-danger">:message</span>') }}
{{ Form::label('comment', trans('people.contact messages')) }} <span class="req">*</span>
{{ Form::textarea('comment', Input::old('comment'), array('class' => 'form-control', 'rows' => 5)) }}
{{ $errors->first('comment', '<span class="help-block alert alert-danger">:message</span>') }}
{{ Form::hidden('email', $actor['email']) }}
{{ Form::submit('Submit', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
It has three fields: a from email field ('fremail'), a 'comment' field and a 'email' field which grabs the email from the 'actor' database table.
My submit function looks like this. It validates the data and sends it to a send function. It also creates a $star variable and stores the 'email' field. For this example the email stored in $star=actorname#gmail.com
public function submitContactAct()
{
//prepare input
$input = Input::except('_token');
$star = Input::get('email');
if ( ! $this->validator->with($input)->passes())
{
return Redirect::back()->withErrors($this->validator->errors())->withInput($input);
}
$this->mailer->sendContactUsAct($input, $star);
return Redirect::back()->withSuccess( trans('main.contact success') );
}
This is my send email function, I have set the setReplyTo to the $star variable, but I keep receiving
Class actorname#gmail.com does not exist
public function sendContactUsAct(array $input, $star)
{
//get contact us email for db
$options = App::make('Options');
$email = $options->getContactEmail();
if ($email)
{
Mail::send('Emails.ContactAct', $input, $star, function($message) use($email)
{
$message->setReplyTo($star);
$message->to($email)->subject( trans('main.contact email subject') );
});
}
}
I don't know why it is thinking a the string in $star is a class.
Try this
Mail::send('Emails.ContactAct', $input, function($message) use ($email, $star)
{
$message->setReplyTo($star);
message->to($email)->subject( trans('main.contact email subject') );
});
Variable $star has to be passed using use because it's not in scope of your closure.
Related
I'm trying to build contact form in Laravel 5.4. I'm almost succeeded but besides actual message, on my mail i'm getting the structure of the page (look screenshot). Can you help me with that?
enter image description here
my View Form:
<div class="row">
{{ Form:: open(array('action' => 'ContactController#getContactUsForm')) }}
<ul class="errors">
#foreach($errors->all('<li>:message</li>') as $message)
{{ $message }}
#endforeach
</ul>
<div class="form-group">
{{ Form:: textarea ('message', '', array('placeholder' => 'Message', 'class' => 'form-control', 'id' => 'message', 'rows' => '7' )) }}
</div>
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form:: close() }}
</div>
</div>
And my Controller:
namespace App\Http\Controllers;
use Input;
use Illuminate\Http\Request;
use Validator;
use Mail;
use Redirect;
class ContactController extends Controller
{
public function getContactUsForm(Request $request){
//Get all the data and store it inside Store Varible
$data = \Input::all();
//$data = $request->message;
//$data = $request->input('message');
//Validation rules
$rules = array (
//'first_name' => 'required', uncomment if you want to grab this field
//'email' => 'required|email', uncomment if you want to grab this field
'message' => 'required|min:5'
);
//Validate data
$validator = Validator::make ($data, $rules);
//If everything is correct than run passes.
if ($validator -> passes()){
Mail::send('support/contact', $data, function($message) use ($data)
{
//$message->from($data['email'] , $data['first_name']); uncomment if using first name and email fields
$message->from('masha#mail.com', 'contact form');
//email 'To' field: cahnge this to emails that you want to be notified.
$message->to('masha#mail.com', 'Masha')->subject('Contact Form');
});
// Redirect to page
return Redirect::route('contact')
->with('message', 'Your message has been sent. Thank You!');
//return View::make('contact');
}else{
//return contact form with errors
return Redirect::route('contact')
->with('error', 'Feedback must contain more than 5 characters. Try Again.');
}
}
}
change this:
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form:: close() }}
</div>
To this:
<div class="modal-footer">
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
</div>
{{ Form:: close() }}
I think ordering break the structure.
sorry if this is a very newbie Q..
but please help me to solve this problem. plus give me the reason about why this error happened..
this is my edit view
new.blade.php
#section('content')
#include('common.show_error')
{{Form::open(array('url'=>'author/update', 'method'=>'PUT'))}}
<p>
{{ Form::label('name', 'Name: ') }}</br>
{{ Form::text('name', $author->name) }}
</p>
<p>
{{ Form::label('bio', 'Biography: ') }}</br>
{{ Form::textarea('bio', $author->bio) }}
</p>
{{ Form::hidden('id', $author->id) }}
<p>{{ Form::submit('Edit Data') }}</p>
#stop
this is my show view
show.blade.php
#extends('layouts.default')
#section('content')
<h1>{{ $author->name }}</h1>
<p>{{ $author->bio }}</p>
<p>{{ $author->updated_at }}</p>
<span>
{{ HTML::linkRoute('authors', 'Home') }} |
{{ HTML::linkRoute('edit_author', 'Edit', array($author->id)) }} |
{{ Form::open(array('url'=>'author/destroy', 'method'=>'DELETE', 'style'=>'display: inline;')) }}
{{ Form::hidden('id', $author->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
</span>
#stop
this is my controller
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
Member::where('id','=',$id)->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated');
}
}
the case: when I try to edit data using edit button. it said:
"Trying to get property of non-object laravel"
and when I check at the error log. it refers to
<h1>{{ $author->name }}</h1>
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
$author = Member::find($id);
$author->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated')
->with('author', $author);
}
}
Little changes in your controller, try it :) In your code, you are not send variable "author" into your view.
I am in the progress of making a form to edit an existing entry in the database. I am using the Form::model approach to do this, however it doesn't seem to work. The fields just stay empty.
ServerController.php
/**
* Editing servers
*/
public function edit($name)
{
$server = Server::find($name);
$keywords = ($server->getKeywords()) ? $server->getKeywords() : array();
$countries = $this->getCountries();
return View::make('server/edit', array('server' => $server, 'countries' => $countries));
}
public function update($name)
{
$server = Server::find($name);
// Did it succeed?
if($server->save()) {
Session::flash('success', 'You server was edited!');
return Redirect::route('server.view', array($name));
}
// Did not validate
if(Input::get('keywords')) {
$keywords = Input::get('keywords');
Session::flash('keywords', $keywords);
}
Session::flash('danger', "<b>Oops! There were some problems processing your update</b><br/>" . implode("<br/>", $server->errors()->all()));
return Redirect::route('server.edit', array($name))->withInput()->withErrors($server->errors());
}
The Form
{{ Form::model($server, array('route' => array('server.update', $server->name), 'class' => 'form-horizontal', 'role' => 'form', 'files' => true)) }}
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{{ Form::label('email', 'Email', array('class' => 'control-label col-md-4')) }}
<div class="col-md-4">
{{ Form::text('email', '', array('class' => 'form-control')) }}
{{ $errors->first('email', '<br/><div class="alert alert-danger">:message</div>') }}
</div>
</div>
(some more fields)
{{ Form::close() }}
The problem here is that you're passing in an empty string as the default field value. As the documentation states here, any explicitly passed values will overrule the model attribute data. Try using null instead of '':
{{ Form::text('email', null, array('class' => 'form-control')) }}
Earlier today I had the exact same problem with Auth::attempt always retuning false. I realized that Auth checks for a hashed password, so by doing so I was able to get it to return true, but now it always does. Even if I type asdadfasdfaf in my form, the if statement loads the page. Any suggestions?
Controller:
class userController extends \BaseController
{
public function login()
{
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($user))
{
return Redirect::route('home');
}
else
{
return View::make('login');
}
}
}
Form
{{ Form::open(array('url' => 'home' )) }}
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
The Routes file:
Route::post('home', 'userController#login');
No matter what I enter it always directs me to my "home" page?
The action url should be login
{{ Form::open(array('url' => 'login' )) }}
^^^^ as you used Auth::attempt to this URL
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
I've been reading about this feature: http://laravel.com/docs/html#form-model-binding
And it looks really neat, but there are couple of things that I'm not certain about.
Do I need to put any code in the controller action to process this form? What does that look like?
The model (User) I want to bind in my form has a separate table for addresses. So I want to be able to fill out the User model's fields, but also the fields for the related Address model. Can I do that with form-model-binding, or do I have to handle the form manually?
Or, failing that, can I use form model binding for the user fields, but manually handle the address fields?
You don't need any different code in your controller to process this form. All your (named) form variables will be in Input::all().
The model ($user) you pass in
Form::model($user, array('route' => array('user.update', $user->id)))
Is just any record you need to, if you have more than one table involved, you'll have to do something like
$user = User::where('id',$userID)
->leftJoin('users_addresses', 'users_addresses.user_id', '=', 'users.id')
->first();
And pass this composed model to your Form::model().
How you name your inputs is entirely up to you, because you'll have to write the logic to process your form. But, in my opinion users_address[street] for the address inputs is good, because you'll end up with an array of addresses columns that you can pass right away to your UserAddress model.
<html>
<head>
<title></title>
</head>
<body>
{{ Form::model($user, array('route' => array('user.update', $user->id))) }}
{{ Form::label('first_name', 'First Name:', array('class' => 'address')) }}
{{ Form::text('first_name') }}
{{ Form::label('last_name', 'Last Name:', array('class' => 'address')) }}
{{ Form::text('last_name') }}
{{ Form::label('email', 'E-Mail Address', array('class' => 'address')) }}
{{ Form::text('email') }}
{{ Form::label('address[street1]', 'Address (Street 1)', array('class' => 'address')) }}
{{ Form::text('address[street1]') }}
{{ Form::label('address[street2]', 'Address (Street 2)', array('class' => 'address')) }}
{{ Form::text('address[street2]') }}
{{ Form::label('ddress[city]', 'City', array('class' => 'address')) }}
{{ Form::text('address[city]') }}
{{ Form::label('address[state]', 'State', array('class' => 'address')) }}
{{ Form::text('address[state]') }}
{{ Form::label('address[zip]', 'Zip Code', array('class' => 'address')) }}
{{ Form::text('address[zip]') }}
{{ Form::submit('Send this form!') }}
{{ Form::close() }}
</body>
</html>
And if you do dd( Input::all() ) in your controller, you'll get something like this:
This result is provided by Kint's dd(): https://github.com/raveren/kint. Really helpful.
If your form just have fields from a single Model, your update method can be very simple and look something like:
public function update($id)
{
$user = User::find($id);
if (!$user->update(Input::all())) {
return Redirect::back()
->with('message', 'Something wrong happened while saving your model')
->withInput();
}
return Redirect::route('user.saved')
->with('message', 'User updated.');
}
On forms a little bit more complex, coders will have to add more logic to their controllers, in you case with a little bit more of research I think you can make this happen:
public function update($id)
{
$user = User::find($id);
$inputs = Input::all();
if (!$user->update($inputs)) {
$address = new UserAddress($inputs['address']);
$user->address()->save($address);
...
}
...
}
In Laravel 5.1 for relation model binding you just need to eager load relation table(s):
$user = User::with(['address'])->find($id);
And in view set fields names as array:
{!! Form::model($user, ['route' => ['user.update', $user->id]]) !!}
{!! Form::text('address[street]') !!}
{!! Form::text('address[number]') !!}
{!! Form::close() !!}