I've been trying to add the id of the current logged-in user into a field in my DB.
But, nothing is stored. I have tried the following methods:
public function store(StoreChildRequest $request)
{
$user = Auth::user();
$child = $request->all();
$child->merge(['user_id' => $user->id]);
$child = new Patient($child);
$child->save();
}
But the StoreChildRequest always returns the user_id is not passed.
Also this didn't work:
public function store(StoreChildRequest $request)
{
$user = Auth::user();
Input::merge(['user_id' => $user->id]);
$child = new Patient($request->all());
$child->save();
}
The same error.
Even if I try to do so with fixed data, the merge won't happen:
public function store(StoreChildRequest $request)
{
$user = Auth::user();
Input::merge(['user_id' => "01"]);
if($child = new Patient($request->all()))
{
$child->save();
return response()->json(['succes' => 'Child saved.']);
}
else
{
return response()
->json($request->getMessageBag()->toArray());
}
}
EDIT
I have noticed that when I remove the StoreChildRequest (the validation) the code works just fine. So I'm going to add the rules:
StoreChildRequest:
public function rules()
{
return [
'name' => 'required|max:255',
'givenname' => 'required|max:255',
'street' => 'required|max:255',
'streetnumber' => 'required|max:255',
'city' => 'required|max:255',
'postalcode' => 'required|digits:4',
'email' => 'required|email',
'birthdate' => 'required|date',
'sex' => 'required|max:1',
'user_id' => 'required'
];
}
The error has to be in here then?
Thank you for your help.
You could do something like this:
$user = Auth::user();
$child = $request->all();
$child['user_id'] = $user->id;
$child = new Patient($child);
$child->save();
Also, you could try:
$user = Auth::user();
$request->request->add(['user_id' => $user->id]);
$child = $request->all();
$child = new Patient($child);
$child->save();
Related
i want get data from database and calculate the average Rating but when i get data by author id i have message find() is null
this my function
public function teacher_average_rating_in_school()
{
$this->autoRender = false;
if($this->request->is('get'))
{
$author_id = $this->request->query('author_id');//
$rate = $this->UserRating->find('all',array('conditions'=>array('author_id'=> $author_id), 'fields'=> array('AVG(UserRating.rstings) as averageRating'), 'recursive' =>-1));
$average = $rate[0][0]['averageRating'];
$result = array('success'=>'1' , 'average' => $average );
}
else{
$result = array('success'=>'0','message'=>'request type is not GET');
}
echo json_encode($result);
}
and this my model
class UserRating extends AppModel {
public $validate = array(
'user_id' => array('rule' => 'notBlank'), 'author_id' => array( 'rule' => 'notBlank') , array('rstings' => 'notBlank' ));
}
why i get find()is null , and how can i solve that ?
Have you load the Modal??
public function teacher_average_rating_in_school()
{
$this->autoRender = false;
if($this->request->is('get'))
{
$this->loadModel('UserRating');
$author_id = $this->request->query('author_id');//
$rate = $this->UserRating->find('all',array('conditions'=>array('author_id'=> $author_id), 'fields'=> array('AVG(UserRating.rstings) as averageRating'), 'recursive' =>-1));
$average = $rate[0][0]['averageRating'];
$result = array('success'=>'1' , 'average' => $average );
}
else{
$result = array('success'=>'0','message'=>'request type is not GET');
}
echo json_encode($result);
}
If you have already loadModel using then check sql_dump.
You can use as follows
//inside controller action
debug($this->UserRating->lastQuery());
//Or inside layout call this
<?php echo $this->element('sql_dump'); ?>
I'm trying to update the status on my tickets table to the value : 2.
Once I can create the comment... (is working.. :) ), I wanted to change the status to 2.
This is my ticket model and the following function:
public function addComment($id,$body,$solved)
{
$this->find($id)->status = 2;
$this->save();
$this->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
You need to get your object first then you can Update it:
public function addComment($id,$body,$solved)
{
$ticket = $this->find($id);
$ticket->status = 2;
$ticket->save();
$ticket->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
Try changing your code like this. Maybe this will fix the problem you're having:
public function addComment($id,$body,$solved)
{
$ticket = Ticket::find($id);
$ticket->status = 2;
$ticket->save();
$ticket->comments()->create([
'ticket_id' => $id,
'body' => $body,
'user_id' => auth()->id()
]);
}
I am novice to laravel.I need to send email to all the records in the table using where condition(where exam_id=1). Each record will get the email message is name and email of its own.Already stored in the table.Can any own suggest for this?
Advance thanks
public function sendmail(Request $request) {
$email = DB::table('student')->select('email','exam_id')->where('exam_id','=','1')->get();
$email= mysql_query("SELECT email FROM student WHERE exam_id='1' ;");
$title = $request->input('title');
$content = $request->input('content');
if(mysql_num_rows($email))
{
while($elist_result = mysql_fetch_array($email))
{
Mail::send('email', ['title' => $title, 'content' => $content],function ($message)
{
$message->from('dhivya#authorselvi.com', 'dhivya');
$message->to('dhivya#authorselvi.com');
$message->cc($elist_result);
$message->subject("Hello");
} );
}
}
return response()->json(['message' => 'message send successfully']);
}
public function sendmail(Request $request) {
$email = DB::table('student')->select('email','exam_id')->where('exam_id','=','1')->get();
$title = $request['title'];
$content = $request['content'];
foreach($email as $email) {
Mail::send('email', ['title' => $title, 'content' => $content],function ($message)
{
$message->from('dhivya#authorselvi.com', 'dhivya');
$message->to($email->email);
$message->subject("Hello");
});
}
return response()->json(['message' => 'message send successfully']);
}
I'm seeing that you are sticked to clear PHP and it's wrong while you are working on Laravel framework.
public function sendmail(Request $request) {
$emails = DB::table('student')->select('email','exam_id')->where('exam_id','=','1')->get();
$title = $request['title'];
$content = $request['content'];
foreach($emails as $email) {
Mail::send('email', ['title' => $title, 'content' => $content],function ($message)
{
$message->from('dhivya#authorselvi.com', 'dhivya');
$message->to($email->email);
$message->subject("Hello");
});
}
return response()->json(['message' => 'message send successfully']);
}
Take a look at your code edited by me and try it.
Here I am writing the test cases for changes password in cakephp 3.x, for that I have set the session for authentication check in test cases, but it is not working.
My code is
public function setUserSession() {
$auth = [
'Auth' => [
'user' => [
'id' => 2,
'email' => 'testemail1#test.ie',
'firstname' => 'testfastname',
'lastname' => 'testlastname',
'gender' => 'male',
'active' => 1,
'created' => '2016-02-19 11:54:47',
'modified' => '2016-02-19 11:54:47'
]
]
];
return $auth;
}
/**
* Test register method
*
* #return void
*/
public function testpassword()
{
$data = [
'current_password' => '123456',
'new_password'=>'12345678',
'repeat_password'=>'12345678',
];
$this->session($this->setUserSession());
$this->post('/Customers/password', $data);
$this->assertResponseSuccess();
//echo "Ser"; exit;
// $users = TableRegistry::get('customers');
//$query = $users->find()->where(['email' => $data['email']]);
//$this->assertEquals(1, $query->count());
}
And checking in customer controller like this but is not working.
$id = $this->Auth->user('id');
if (!$this->Customers->exists(['id' => $id])) {
$this->Flash->error('You have been logged out');
return $this->redirect(array('action' => 'login'));
}
I am using a modelless form to send a contact email. I am using the example in the cookbook.
With the change to data in objects form arrays I am unsure what form the data is in being passed to the ContactForm.php
I have tried $data['name'] and $data[0]['name']
View for input name without the data[]
<input type="text" class="form-control" name="name" placeholder="Your Name" required="" aria-required="true" aria-invalid="false">
PagesController.php
public function contact(){
$this->request->allowMethod('ajax');
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
$data = array(
'status' => 'successful'
);
} else {
$data = array(
'status' => 'error'
);
}
$this->set(compact('data')); // Pass $data to the view
$this->set('_serialize', 'data');
}
}
ContactForm.php
protected function _execute(array $data)
{
// Send an email.
$email = new Email('default');
$email->emailFormat('html');
$email->template('demoTemplate')->viewVars( array('userName' => $data[0]['name'],
'userCompany' => '$data',
'userEmail' => '$data',
'userPhone' => '$data'));
$email->from('info#domain.com');
$email->to('$data');
$email->bcc('$data');
$email->subject('Contact Form Submission');
if ($email->send()) {
return true;
}
else {
return false;
}
}
Well for do this I use first this tool advanced-rest-client, and inside the code of cakephp you can use try and catch, you can user debug() too, example debug($data); in combination with advanced-rest-client you can see the result.
and if debug dont work serialice the data
$this->set('data', $data);
$this->set('_serialize', ['data']);
but even when serialized data use advanced-rest-client, just select the post option and type the url of where you want to get the post.
so:
public function contact(){
$this->request->allowMethod('ajax');
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
debug($this->request->data());//first
$datos = $this->request->data();//second
$data = [
'content' => $datos,
'status' => 'successful',
];
} else {
$datos = $this->request->data();
$data = [
'content' => $datos,
'status' => 'error,
];
}
$this->set(compact('data')); // Pass $data to the view
$this->set('_serialize', 'data');
}
}