Laravel 5.4 - send mail - laravel

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!!!

Related

how to Checked if value of static checkboxes are equal on what you have in your database in Laravel

I have this column in my database named "source_income"
Which was Imploded from my EDIT page.
Problem is after I save the record, I see all the checkboxes are checked. I understand that the cause of the problem is that I do not have something that will check if the value of the checkbox should be equal to what I have in the database.
The checkboxes on my form are not dynamic.
<div class="col-md-6 ">
<div class="form-group {{ $errors->has('source_income') ? 'has-error' : ''}}">
<strong>Check all that apply to you:</strong>
<br>
<br>
{!! Form::checkbox('source_income[]', 'employed', null, ['id' => 'employed']) !!}
{!! Form::label('employed', 'Employed') !!}
<br>
{!! Form::checkbox('source_income[]', 'online-seller', null, ['id' => 'online-seller']) !!}
{!! Form::label('online-seller', 'Online Seller') !!}
<br>
{!! Form::checkbox('source_income[]', 'rider', null, ['id' => 'rider']) !!}
{!! Form::label('rider', 'Rider (Grab,Lazada,Etc.)') !!}
<br>
{!! Form::checkbox('source_income[]', 'small-business-owner', null, ['id' => 'small-business-owner']) !!}
{!! Form::label('small-business-owner', 'Small Business Owner') !!}
<br>
{!! Form::checkbox('source_income[]', 'no-income', null, ['id' => 'no-income']) !!}
{!! Form::label('no-income', 'No income') !!}
<br>
{!! Form::checkbox('source_income[]', 'remittances-allotment', null, ['id' => 'remittances-allotment']) !!}
{!! Form::label('remittances-allotment', 'Remittances / Allotment') !!}
{!! $errors->first('source_income', '<p class="help-block">:message</p>') !!}
</div>
</div>
EDIT,BLADE.PHP
public function edit($id, Profile $model)
{
$user_id = Auth::user()->id;
$user = User::findOrFail($user_id);
$profile = Profile::findOrFail($id);
$profileSourceIncome = explode(",", $profile->source_income);
return view('dashboard.profile.edit', compact('model','profile'))
->with('user', $user)
->with('profileSourceIncome', $profileSourceIncome);
}
I literally stopped in this part $profileSourceIncome = explode(",", $profile->source_income);
My question is how can I able to display the checkboxes checked if the name of the checkbox is equal to any value from $profileSourceIncome[]?
Thank you so much in advance!
According to the documentation, you only need to pass true as the third parameter in your Form::checkbox(...) calls to return a checkbox that is already checked.
public function edit($id, Profile $model)
{
$user_id = Auth::user()->id;
$user = User::findOrFail($user_id);
$profile = Profile::findOrFail($id);
// Turn this array into a Collection
$profileSourceIncome = collect(explode(',', $profile->source_income));
return view('dashboard.profile.edit', compact('model','profile'))
->with('user', $user)
->with('profileSourceIncome', $profileSourceIncome);
}
And then, in your blade file, you could use the Collection's contains() method to do the following:
Form::checkbox('source_income[]', 'employed', $profileSourceIncome->contains('employed'), ['id' => 'employed'])
Form::checkbox('source_income[]', 'online-seller', $profileSourceIncome->contains('online-seller'), ['id' => 'online-seller'])
Form::checkbox('source_income[]', 'rider', $profileSourceIncome->contains('rider'), ['id' => 'rider'])
Form::checkbox('source_income[]', 'small-business-owner', $profileSourceIncome->contains('small-business-owner'), ['id' => 'business-owner'])
Form::checkbox('source_income[]', 'no-income', $profileSourceIncome->contains('no-income'), ['id' => 'no-income'])
Form::checkbox('source_income[]', 'remittances-allotment', $profileSourceIncome->contains('remittances-allotment'), ['id' => 'remittances-allotment'])

Why update picture doesn't detect file uploaded?

I am using laravel 5 to create an edit form for a profile and can update picture in the form.
I want to store a new image in edit form. I use this code in edit.blade to get the image by user.
View:
{!! Form::model($dataItemregistration,['method' => 'PATCH', 'action' => ['Modul\ProfilController#update', $dataItemregistration->ItemRegistrationID, 'files' => true] ]) !!}
<div class="form-group">
<div class="row">
<div class="col-lg-3">
{{ Form::label('pic', 'Gambar (Saiz gambar, 250x300px)') }}
</div>
<div class="col-lg-7">
{!! Form::file('gambar', array('class' => 'form-control')) !!}
</div>
</div>
</div>
<br>
<div class="col-lg-10 text-center">
{!! link_to(URL::previous(),'Back', ['class' => 'btn btn-warning btn-md']) !!}
{{ Form::submit('Update', ['class' => 'btn btn-primary']) }}
</div>
{!! Form::close() !!}
Controller:
public function update(Request $request, $id)
{
$valueitemregistrations = Itemregistration::find($id);
$this->validate($request,['gambar' => 'max:100000',]);
if ($request->hasFile('gambar')) {
// Get the file from the request
$file = $request->file('gambar');
// Get the contents of the file
$content = $file->openFile()->fread($file->getSize());
$valueitemregistrations->Picture = $content;
$valueitemregistrations->update();
if($valueitemregistrations) {
return redirect('profil');
} else {
return redirect()->back()->withInput();
}
} else {
echo "testing";
}
}
When I try to upload and update, it goes to echo "testing". It doesn't detected any files uploaded..
I had been using the same code for add.blade and it works.
Is it related to route path or else?
This happens when your HTML form doesn't have enctype="multipart/form-data".
In this case the cause is 'files' => true being part of the wrong array inside Form::model(); it's inside the 'action' array when it should be outside. Try this:
Form::model($dataItemregistration, [
'method' => 'PATCH',
'action' => ['Modul\ProfilController#update', $dataItemregistration->ItemRegistrationID],
'files' => true,
]);

storing data with name of author - laravel 5.2

I have hasMany relation to my model user and reports.
I want to set author name for the reports. (Like a blog-post author)
my model User:
public function reports() {
return $this->hasMany('App\Report', 'author_id');
}
model Report
public function user() {
return $this->belongsTo('App\User', 'author_id');
}
and my controller:
public function create()
{
$category = Category::lists('title','id');
return view('dash.reports.create')->with('category', $category);
}
/**
* Store a newly created resource in storage.
*
* #return void
*/
public function store(Request $request)
{
$this->validate($request, ['title' => 'required', ]);
Report::create($request->all());
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}
I'm able to set in in phpmyadmin, but how can i set it with my controller?
edit: my view:
{!! Form::open(['url' => '/dash/reports', 'class' => 'form-horizontal']) !!}
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('title', 'Servizio', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::text('title', null, ['class' => 'form-control', 'required' => 'required']) !!}
{!! $errors->first('title', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('date', 'Data lavorativa', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-2">
{!! Form::selectRange('day', 1, 31, null, ['class' => 'form-control']) !!}
{!! $errors->first('day', '<p class="help-block">:message</p>') !!}
</div>
<div class="col-sm-2">
{!! Form::selectMonth('month', null, ['class' => 'form-control']) !!}
{!! $errors->first('month', '<p class="help-block">:message</p>') !!}
</div>
<div class="col-sm-2">
{!! Form::select('year', array('2016' => '2016', '2015' => '2015'), null, ['class' => 'form-control']) !!}
{!! $errors->first('year', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : ''}}">
{!! Form::label('category_id', 'Cliente', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
{!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
{!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}
Very easy. Replace Report::create... with this.
$user = Auth::user();
$report = new Report($request->all());
$report->author()->associate($user);
$report->save();
Make sure you use Auth; up at the top.
This uses the Auth object to get the current user,
Builds a new Report using the $request data without saving,
Tells the report we're associating $user as the author for the model,
Saves the report with the authorship information.
solution:
public function store(Request $request)
{
$this->validate($request, ['title' => 'required', ]);
$user = Auth::user()->id;
$report = new Report($request->all());
$report->author_id = $user;
$report->save();
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}

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 }}

Update data with Laravel Collective forms

I have an edit form with Laravel Collective but when clicking the button, the data do not update. Below are my codes.
Form:
{!! Form::model($post, ['route' => ['/post/update/', $post->id]]) !!}
{{ method_field('PATCH') }}
<div class="form-group">
<div class="row">
<div class="col-lg-6">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="col-lg-6">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'rows' => 10]) !!}
</div>
<hr/>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
</div>
{!! Form::close() !!}
Controller:
public function edit($id)
{
return \View::make('admin/post/edit')->with([
'post' => \DB::table('posts')->find($id),
'categories' => \App\Category::lists('category', 'id')
]);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return \Redirect::to('/admin/posts');
}
Routes:
Route::get('/admin/post/edit/{id}', 'Admin\PostController#edit');
Route::patch('/post/update/', [
'as' => '/post/update/',
'uses' => 'Admin\PostController#update'
]);
It's a bit different from the Laracast, and it's confusing me. Framework is new to me and the lack of code to do something is confusing.
I solved it. Mass Assignment. explains what to do if using update or create
So, the update method is:
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->category_id = $request->category_id;
$post->content = $request->content;
$post->save();
return \Redirect::to('/admin/posts');
}

Resources