Am trying to execute a send mail function when a record is stored in the database but am getting unreachable statement
public function store(Request $request)
{
$visit = Visit::create($request->all());
return response()->json($visit);
$this->sendEmail($request);
}
This is the send email function
public function sendEmail(Request $request){
$visit = Visit::create($request->all());
$host_email = Db::table('users')
->where('name', '=', $visit->visitor_host)
->value('email');
$to_name = $request->input('visitor_name');
$data = array('name'=> $to_name, "body" => "Test mail");
Mail::send('mails.mail', $data, function($message) {
$message->from('cytonnvisitor#gmail.com','cytonn');
$message->to('mimikiduchu#gmail.com');
$message->subject('Visitor coming notification');
});
}
From the manual:
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call.
Change your code to
$this->sendEmail($request);
return response()->json($visit);
to execute the mail send before your return.
Enter return response()->json($visit); at the end of the store function.
Related
I want check if my form value equals in database value in laravel
Here is my controller class
public function code_post(Request $request, $id)
{
$sms_token_in = $request->sms_token_in;
$sms_token=Auth::user()->sms_token;
DB::table('users')->where('id',$id , 'sms_token_in' ,$sms_token)->update([
'sms_verify'=>'1'
]);
return redirect('/panel')->with('edit','pending');
}
What i do wrong?
update to
DB::table('users')->where(['id',$id ])->where([ 'sms_token_in'
,$sms_token])->update(['sms_verify'=>'1' ]);
The most simple way to use where is
two parameters, 1: database field name, 2: value
three parameters (field name, operator, value) ('1', '>=', 3)
DB::table('users')->where('id',$id)->where('sms_token_in', $sms_token)->update([
'sms_verify'=>'1'
]);
Try
DB::table('users')->where([
['id',$id],
['sms_token_in', $sms_token]
])->update([
'sms_verify'=>'1'
]);
First you must accept two parameter from your function. Look like only you are passing only one.
change function parameter to
public function code_post(Request $request, $id = 0)
then
if(!empty($id)){
User::where(['id' => $id,'sms_token_in' => $sms_token])->update([
'sms_verify'=>'1'
]);
}
// you can do this in two way
public function code_post(Request $request, $id)
{
$sms_token_in = $request->sms_token_in;
$sms_token=Auth::user()->sms_token;
DB::table('users')->where('id',$id)->where('sms_token_in' ,$sms_token)->update([
'sms_verify'=>'1'
]);
return redirect('/panel')->with('edit','pending');
}
//2nd way
public function code_post(Request $request, $id)
{
$sms_token_in = $request->sms_token_in;
$sms_token=Auth::user()->sms_token;
DB::table('users')->where(['id',$id , 'sms_token_in' ,$sms_token])->update([
'sms_verify'=>'1'
]);
return redirect('/panel')->with('edit','pending');
}
The best whey to do it is on Validators in laravel so, if it don't exist create a classe validator that extends "LaravelValidator" and do it:
protected $rules = [
ValidatorInterface::RULE_CREATE => [
'sms_token_in' => 'required|unique:your_table_name',
],
ValidatorInterface::RULE_UPDATE => [],
]; protected $messages = [
'sms_token_in.unique' => 'Your duplicate message!'
];
In your controller instantiate your validator in construct like it:
public function __construct(MyValidatorClass $validator)
{
$this->validator = $validator;
}
And in your controller function store do it, before your persist on database.
$this->validator->with($data)->passesOrFail(ValidatorInterface::RULE_CREATE);
In this way, you can use validator from Laravel to check and return anything you want to your user.
function check(Request $request){
//validate
$request->validate([
'email'=>'required|email',
'user_pass'=>'required|min:5|max:12'
]);
$userInfo=Employee::where('email','=',$request->email)->first();
if(!$userInfo){
return back()->with('fail','We do not recognize your email address');
}else{
//check password
if($request->user_pass=$userInfo->user_pass){
$request->session()->put('LoggedUser', $userInfo->emp_id);
return redirect('/employee/dashboard');
}else{
return back()->with('fail','Incorrect password');
}
}
}
In this method im check if the user_pass from request is equal to $userInfo->user_pass that query from database.
if($request->user_pass=$userInfo->user_pass)
I will try to insert & also update data using session in Codeigniter, but data not inserted into the database even its print save successfully.
Here is my controller:
public function save($user_id)
{
$this->load->model('Users');
$code=$this->input->post('code');
$name=$this->input->post('name');
$address=$this->input->post('address');
$user_data= array(
'code' =>$code,
'name'=>$name,
'address'=>$address,
'active'=>1
);
if($this->Users->save($user_data,$user_id))
{
$this->session->set_flashdata('msg',"save sucesss");
}else {
$this->session->set_flashdata('msg',"not save");
}
redirect('home');
}
& this is my model:
public function save($data,$id)
{
if (id=='') {
// code...
$this->db->insert('user',$data);
return true;
}else
{
$this->db->where('id',$id)
->update('user',$data);
return true;
}
return false;
}
Data insert if I removed if in model!
You have the model always returning true no matter the outcome of the database operation. You should use the return value from insert() or update() so the "message" reports what actually happens.
Note that the argument to save has a default value. Now you can call the save URL without an argument and it will automatically do an insert.
public function save($user_id = NULL)
{
$this->load->model('users');
$user_data = array(
'code' => $this->input->post('code'),
'name' => $this->input->post('name'),
'address' => $this->input->post('address'),
'active' => 1
);
if($this->Users->save($user_data, $user_id))
{
$msg = "save sucesss";
}
else
{
$msg = "not save";
}
$this->session->set_flashdata('msg', $msg);
redirect('home');
}
public function save($data, $id)
{
if(empty($id))
{
// code...
// insert returns TRUE on success, FALSE on failure
return $this->db->insert('user', $data);
}
// update() accepts a third argument, a "where" array
// and returns TRUE on success, FALSE on failure
return $this->db->update('user', $data, array('id' => $id));
}
Now have an accurate report on the database operations.
the first check is data is coming in save controller or not if it's not getting the data then fix it. If coming then pass it in a model in the correct format and it will definitely be inserted in the database.
use following printing data
echo $data;
var_dump($data);
print($data);
print_r($data);
First thing is to rename your model calling eg:
$this->load->model('users');
and use this to call your method:
$this->users->save($user_data,$user_id)
your model should look like this then:
public function save($data, $id) {
if ($id) {
$this->db->where('id', $id)
->update('user', $data);
return true;
}
$this->db->insert('user', $data);
return true;
}
if you want to use your flashdata on the next request, use this:
$this->session->keep_flashdata('item');
$this->session->keep_flashdata(array('item1', 'item2', 'item3'));
because flashdata is only for the next request:
CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared.
I have created a Laravel method in my PageController to send emails when form data is sent like the following:
public function sendMessage(Request $request)
{
$name = $request->input('name');
$email = $request->input('email');
$message_content = $request->input('message');
// email message
Mail::raw($message_content, function ($message)
{
$message->from($email, $name);
// $message->to(env('APP_ADMIN_EMAIL'));
$message->to("myemail#mail.com");
$message->subject('Website Message');
});
return "message sent";
}
But I get this error when the method is called:
ErrorException Undefined variable: email
Can someone explain what I'm doing wrong?
You need to pass the variables to the closure:
Mail::raw($message_content, function ($message) use ($email, $name)
{
$message->from($email, $name);
$message->to("myemail#mail.com");
$message->subject('Website Message');
});
See docs: https://secure.php.net/manual/en/functions.anonymous.php
GetEstimateController.php
public function fill_dropbox(){
$data = ProcedureTreat::get(['pro_name']);
$data1 = HospitalPackage::get(['address']);
// return $this->get_estimate();
// dd($data);
return View::make('get_quote')->with('address',$data1)->with('procedureName',$data);
}
public function get_estimate($name,$addr){
// dd($name);
$HospitalPack = HospitalPackage::where(['pro' => $name, 'address' => $addr])->orderBy('treat_price', 'desc')->get()->first();
$Dropbox_fill = $this->fill_dropbox();
// dd($HospitalPack);
return View::make('get_quote')->with(compact('Dropbox_fill','HospitalPack'));
}
if I'm using
return View::make('get_quote')->with(compact('Dropbox_fill','HospitalPack'));
this line it is showing error while using
return View::make('get_quote')->with(compact('HospitalPack'));
this code it is not showing error.
Turns out the problem was indeed in the return statement inside the fill_dropbox() method, so if you did it like this:
public function get_estimate($name,$addr){
$HospitalPack = HospitalPackage::where(['pro' => $name, 'address' => $addr])->orderBy('treat_price', 'desc')->get()->first();
$data = ProcedureTreat::get(['pro_name']);
$data1 = HospitalPackage::get(['address']);
return View::make('get_quote')->with(compact('data', 'HospitalPack', 'data1'));
}
Everything should be working now :)
I have this store method inside OfferController:
public function store(Requests\OfferRequest $request)
{
$offer = new Offer($request->all());
Auth::user()->offer()->save($offer);
$maxoffer = Maxoffer::where('article_id', $request->input('article_id'))
->where('start', Carbon::createFromFormat('m/d/Y h:i a', $request->input('start')))
->first();
//dd($maxoffer->article()->first()->user->name);
if($maxoffer == null)
{
Auth::user()->maxoffer()->create($request->all());
}
else
{
if($maxoffer->price < $request->input('price'))
{
$user = Auth::user();
Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user) {
$m->from($maxoffer->article()->first()->user->email, $maxoffer->article()->first()->user->name);
$m->to($maxoffer->user()->first()->email, $maxoffer->user()->first()->name)->subject('Someone have the bigger offer than you');
$key = '';
$newOffer = Maxoffer::where('id', $maxoffer->id)
->update(['price'=>$request->input('price'),'user_id'=>Auth::user()->id, 'key'=>$key, 'provera'=>$request->input('provera')]);
});
}
}
Alert::success('Keep looking for best rates. Good luck...', 'Thanks for bidding!')->persistent("Close");
return Redirect::back();
}
so if maxoffer is not null and if maxoffer<request->input('price') then I need to update an row and that work nice, but also I need to send MAIL to previous user which was posted maxoffer before new maxoffer but inside MAIL function I get just:
undefined variable: maxoffer
what is problem here? Why maxoffer is undefined?
Pass $maxoffer to the function closure. use($user, $maxoffer)
Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user, $maxoffer) {
$m->from($maxoffer->article()->first()->user->email, $maxoffer->article()->first()->user->name);
$m->to($maxoffer->user()->first()->email, $maxoffer->user()->first()->name)->subject('Someone have the bigger offer than you');
$key = '';
$newOffer = Maxoffer::where('id', $maxoffer->id)
->update(['price'=>$request->input('price'),'user_id'=>Auth::user()->id, 'key'=>$key, 'provera'=>$request->input('provera')]);
});