How To send PDF to a specific user from database via email? - mysql

This is my code for generate PDF and send it to admins. The problem it is sending all the bills stored in database to all. But I want to send specific bill to specific user. I have connected two tables using foreign key named property_id. I want to send bill to an admin whose property_id is similar to bill property_id. How to do that?
code::
$users = User::where('user_type', 'admin')->get();
foreach($users as $user) {
$data["email"] = $user->email;
$data["title"] = "From Admin";
$data["body"] = "Bill To Pay";
$pdf = PDF::loadView('emails.myTestMail', $data);
$mail = Mail::send('emails.myTestMail', $data, function($message) use($data, $pdf) {
$message->to($data["email"], $data["email"])
->subject($data["title"])
->attachData($pdf->output(), "Bill.PDF");
});
}
Everything is working fine. But it is sending all the information stored in bill table to all user. I want to send specific data to specific user.
Thank You.

Related

Data is not updating in mysql through laravel

I have made a table of users and during the time of registration, i am inserting the necessary data like email password and name and marked others columns null. Everything was working fine until i tried to update the null columns as per requirement. I have tried many methods to update the data but it always says 200 OK in postman but not update the Mysql table.Here is my code
$data = $request->validate([
'dob' => 'required',
'gender'=>'required|in:male,female',
'image'=>'required|mimes:jpg,png,jped|max:5048'
]);
$newImage = time().'-'.$request->name.'.'.$request->image->extension();
$request->image->move(public_path('images'), $newImage);
$user = User::find($id);
$user->update($request->all());
return response($user);
And this my output the name id and email field were added on the time of registeration what i am trying to add is 'dob','gender' and ''profile picture
After trying the method provided i got this
The answer to this question was don't use 'PUT' request to update the data. Some times it works well and some times it will not work. Just use post method just like to create data. It solved my problem.
$user = User::find($id);
$user ->dob = $request->dob;
$user ->gender = $request->gender;
$user ->image= $newImage;
$user ->save();
try this will 100% update your users details .
I hope you got your solution .

inserting data to multiple tables from a single imported CSV in laravel

I'm trying to import CSV file in laravel that contains data to be stored in multiple tables in mysql database. Actually I've Contacts with multiple emails, addresses, phones etc so i want them to store in their respective table along with the contactId of the contact newly created from the CSV.
For now insertion in single table is working but when i introduce columns like email, address etc that are to be inserted in other tables, it gives me column not found in Contact table as email address etc column is not there.
Controller Method for Insertion
public function processImport(Request $request){
$data = CsvData::find($request->csv_data_file_id);
$csv_data = json_decode($data->csv_data, true);
foreach ($csv_data as $row) {
$contact = new Contact(); //Insertion in Contact table
foreach (config('app.contact_fields') as $index => $field) {
if ($data->csv_header) {
$contact->$field = $row[$request->fields[$field]];
} else {
$contact->$field = $row[$request->fields[$index]];
}
}
$contact->save(); //Insertion in Contact table
$id = $contact->id;
}
return 'Successfully imported!';
}
Now i want to know is there any way we can separate columns to be inserted into separate tables from the single CSV imported ?
Any help would be highly appreciated.
Thanks
You have to execute multiple queries inside the foreach() loop, because you have multiple tables. Without knowing the both structure of the CSV file and the tables in question it is difficult to specify any further.

Using Dancer2::Plugin::DBIC to pull values from database

I have a webapp where a user can log in and see a dashboard with some data. I'm using APIary for mock data and in my Postgres Database each of my users have an ID. These ID's are also used in the APIary JSON file with relevant information.
I'm using REST::Client and JSON to connect so for example the url for the user's dashboard is: "/user/dashboard/12345" (in Apiary)
and in the database there is a user with the ID "12345".
How can I make it so when the user logs in, their ID is used to pull the data that is relevant to them? (/user/dashboard/{id})? Any documentation or advice would be much appreciated!
The docs of Dancer2::Plugin::Auth::Extensible are showing one part of what you need to do already. In short, save the user ID in the session. I took part of code in the doc and added the session.
post '/login' => sub {
my ($success, $realm) = authenticate_user(
params->{username}, params->{password}
);
if ($success) {
# we are saving your user ID to the session here
session logged_in_user => params->{username};
session logged_in_user_realm => $realm;
} else {
# authentication failed
}
};
get '/dashboard' => sub {
my $client = REST::Client->new();
# ... and now we use the user ID from the session to get the
# from the webservice
$client->GET( $apiary . '/user/dashboard/' . session('logged_in_user') );
my $data = $client->responseContent();
# do stuff with $data
};
For those who want to know what I ended up doing:
Dancer2::Plugin::Auth::Extensible has
$user = logged_in_user();
When I printed this it showed me a hash of all the values that user had in the database including the additional ID I had. So I accessed the id with
my $user_id = $user->{user_id};
And appended $user_id to the end of the url!

updating a line in cakephp

I'm trying to create a simple request/accept function. A user adds a person - both users usernames are added to the table along with an autogenerated id. Now when the other user checks their requests they click approve and add an expiry date to the the record. Currently whats happening is that when a user clicks accept and sets an expiry date it creates a new record in the Relationship table.
the structure of my relationship table is
id
partyone
partytwo
active
expirydate
public function add(){
if($this->request->is('post')){
$this->Relationship->create();
if ($this->Relationship->save($this->request->data))
{
$id=$this->Relationship->id;
$this->Session->setFlash('The relationship has been saved');
}
else { $this->Session->setFlash('The relationship could not be saved. Please, try again.'); }
}
}
public function approve(){
if($this->request->is('post')){
$this->Relationship->id;
$this->Relationship->getLastInsertID();
$this->Relationship->save($this->request->data);
$this->Session->setFlash('The information has been saved');}
else{
$this->Session->setFlash('The information couldnt be saved');}
}
}
here is my approve view
<?php
echo $this->Form->create('Relationship', array('action'=>'approve'));
echo $this->Form->input('expirydate',array('label'=>'Expiry Date: ', 'class' => 'dateclass'));
echo $this->Form->end('Submit');
?>
here is my add view
<?php
echo $this->Form->create('Relationship', array('action'=>'add'));
echo $this->Form->input('partyone',array('label'=>'Enter your username: '));
echo $this->Form->input('partytwo',array('label'=>'Username of user: '));
echo "<br />";
echo $this->Form->end('Click here to add relationship');
?>
how can i code this so it updates and not creates a new line with expiry date, please help I am losing my mind over this.
This is not making anysense. Is this a typo :
$this->Relationship->id;
$this->Relationship->getLastInsertID();
It should obviously read :
$this->Relationship->id = $this->Relationship->getLastInsertID();
You have to add a hidden field to your "approve" view with the id of the relationship you are going to edit. If the submitted data contains an id, the save() method will update the specified record instead of creating a new one.
You can find an example in the cookbook: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html#editing-posts
You need to provide more information, but the basic idea is:
On your approve view you need to have a field that stores the id of the relationship to be approved.
You can store the ID as a param for the approve method, or as a hidden field.
function approve($id=null) {
/*Form action = Relationship/Approve/ID*/
$this->Relationship->id = $id;
/*or - hidden field/an input*/
$this->Relationship->id = $this->request->data['Relationship']['id'];
$this->Relationship->save($this->request->data);
}
providing id is set before you save, Cake will update, instead of adding.

Retrieve session data Codeigniter

I'm working on a messaging system and want the user's userid to be posted to the database along with the message. Right now, the message is posting to the database, but with a user ID of 0.
How can I get the user ID from the session data to post to the database along with the message? Sidenote: I'm using Tank Auth for authentication. (From the mysql side, user_id in the message table is a foreign key referencing id in the users table).
Controller
function index() {
if ($this->input->post('submit')) {
$id = $this->input->post('user_id');
$message = $this->input->post('message');
$this->load->model('message_model');
$this->message_model->addPost($id, $message);
}
}
Model
function addMessage($id, $message) {
$data = array(
'user_id' => $id,
'message' => $message
);
$this->db->insert('message', $data);
}
For tank_auth, get the user_id using the following, and then assign that to your sessions
$user_id = $this->tank_auth->get_user_id();
Taken directly from CI's documentation:
Retrieving Session Data
Any piece of information from the session array is available using the
following function:
$this->session->userdata('item');
Where item is the array index
corresponding to the item you wish to fetch. For example, to fetch the
session ID you will do this:
$session_id = $this->session->userdata('session_id');
Note: The
function returns FALSE (boolean) if the item you are trying to access
does not exist.
So, if you have a piece of session data named user_id, you would access it like this:
$user_id = $this->session->userdata('user_id');