how to pluck email from array of users
pluck
is useful to do a minimalist db query.
When you have an array, just use map
:
arr.map(&:email)
Get value of email using pluck - php laravel
Try the below code :
Assuming your data look likes below array i.e $arrays.
$arrays=[
["name" => "Khv","emails" =>
[0 => ["email" => "demo1@yahoo.com"]
]
],
["name" => "asy", "emails" =>
[0 => ["email" => "demo2@yahoo.com"]
]
]
];
$emails=collect();
foreach ($arrays as $array) {
$emails->push($array['emails'][0]['email']);
}
dd($emails);
Note : You can't use pluck on array, it can be only used on collection.
Don't include current user in Pluck array
You can do this by using where
and passing conditions to exclude the current_user
<%= f.select :assignee, User.where("id <> ?", current_user.id).pluck(:email), :prompt => "Select one" %>
Rails - given an array of Users - how to get a output of just emails?
(by popular demand, posting as a real answer)
What I don't like about fl00r's solution is that it instantiates a new User object per record in the DB; which just doesn't scale. It's great for a table with just 10 emails in it, but once you start getting into the thousands you're going to run into problems, mostly with the memory consumption of Ruby.
One can get around this little problem by using connection.select_values on a model, and a little bit of ARel goodness:
User.connection.select_values(User.select("email").to_sql)
This will give you the straight strings of the email addresses from the database. No faffing about with user objects and will scale better than a straight User.select("email")
query, but I wouldn't say it's the "best scale". There's probably better ways to do this that I am not aware of yet.
The point is: a String
object will use way less memory than a User
object and so you can have more of them. It's also a quicker query and doesn't go the long way about it (running the query, then mapping the values). Oh, and map
would also take longer too.
If you're using Rails 2.3...
Then you'll have to construct the SQL manually, I'm sorry to say.
User.connection.select_values("SELECT email FROM users")
Just provides another example of the helpers that Rails 3 provides.
How to pass array values into the mail class one by one
Why not use foreach?
$email = Auth::user()->email;
$messageData = [
'email' => $email,
'name' => Auth::user()->name,
'order_id' => $order_id,
'orderDetails' => $orderDetails,
];
foreach ($vendorEmails as $vemail) {
Mail::send('emails.order', $messageData, function ($message) use ($email, $vemail)
{
$message->to($vemail)->subject('Order Placed - stylooworld.info');
});
}
How to pluck key value from object of array
You should use map function to pluck specific key value from object array.
var new_arr= arr.map(function (value, index, array) {
return {"id": value.id};
});
pass array of emails to Job class Mail::to
From the doc
To send a message, use the to method on the Mail facade. The to method accepts an email address, a user instance, or a collection of users.
So do this.
$email = new Report($this->user);
$admins = User::select('email')->where('role', 2)->get();
Mail::to($admins)->queue($email);
This is what happens under the hood. In case you want to use different ways to load the email list.
public function to($address, $name = null)
{
return $this->setAddress($address, $name, 'to');
}
protected function setAddress($address, $name = null, $property = 'to')
{
foreach ($this->addressesToArray($address, $name) as $recipient) {
$recipient = $this->normalizeRecipient($recipient);
$this->{$property}[] = [
'name' => isset($recipient->name) ? $recipient->name : null,
'address' => $recipient->email,
];
}
return $this;
}
protected function addressesToArray($address, $name)
{
if (! is_array($address) && ! $address instanceof Collection) {
$address = is_string($name) ? [['name' => $name, 'email' => $address]] : [$address];
}
return $address;
}
protected function normalizeRecipient($recipient)
{
if (is_array($recipient)) {
return (object) $recipient;
} elseif (is_string($recipient)) {
return (object) ['email' => $recipient];
}
return $recipient;
}
Related Topics
Trying to Understand Use of Self.Method_Name VS. Classname.Method_Name in Ruby
Error: While Executing Gem ... (Typeerror) Incompatible Marshal File Format (Can't Be Read)
How to Pass <Arguments> to Irb If I Don't Specify <Programfile>
How to Fix Libv8 Error from Gemfile on Mavericks
Ruby Mechanize Post with Header
How to Connect to Postgresql Database After Upgrading to Yosemite 10.10
Is the Unix Philosophy Falling Out of Favor in the Ruby Community
How to Do Sti and Still Use Polymorphic Path Helpers
How to Sort a Ruby Array of Strings by Length
List Dynamic Attributes in a Mongoid Model
Best Way to Handle Data Attributes in Slim
How to Pass an Argument When Calling a View File
How to Stop God from Leaving Stale Resque Worker Processes
Actiondispatch::Http::Uploadedfile.Content_Type Not Being Initialized in Rspec Test
Retrieving Image Height with Carrierwave
How to Do Case-Insensitive Order in Rails with Postgresql