I want to know how can I put $user->email in the following code snippet, where the email address is. Everything works well when hard coded in but not when I put $user->email where the email address is - I get error then. Please help. thanks.
public function runCommand(Request $request){
$user = User::all();
$signature = $request->input('signature');
$command = Artisan::call($signature, ['user' => 'myemail#email.co.za']);
return response($command);
}
$user = User::all();
returns a Collection
The following illustrates how you iterate over all user emails:
$users = User::all();
foreach ($users as $user) {
echo $user->email;
}
( In case you want to send an email to the currently authenticated user (?) from within a Controller, not a Command see here. )
Related
I create the CRUD App. In this App, the user can add a contact, and include their birthday in that contact.
I have two tables in PHP MySQL: students and users. When their contact at the students' table has a birthday, the user will receive an email.
To test the email work, I'm using MailHog. It's was work.
I test that there are two users with a birthday in that contact.
One user has a notification email. Another one has two notification emails because that contact has two data that are two birthdays.
The problem is, that I want to send one notification to every user. So if one user has many contacts on that birthday, it will give the user one email notification, instead of many emails.
this is the notification in MailHog:
user john has two notifications, instead of one.
this is my code:
public function handle()
{
info('mail first');
sleep(1);
info('mail second');
$student = Student::whereMonth('birth', date('m-d'))
->whereDay('birth', date('d') )
->get();
foreach ($student as $mystudent) {
$user = User::find($mystudent->user_id);
Mail::to($user)->send(new WelcomeMail($mystudent));
}
// dd($userid);
echo "check email";
}
this code in class WelcomeMail
public function __construct($students)
{
$this->data = $students;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.welcome')->with('students', $this->data);
}
this is in view:
{{ $students}}
I do this modiv in mail command:
$userid = [];
foreach ($student as $mystudent) {
$user = User::find($mystudent->user_id);
array_push($userid, $user);
Mail::to($user)->send(new WelcomeMail($mystudent));
}
At this point, it's still given me the result that the user has many emails for each contact.
hopefully, you help me train logic to solve this one.
+
this the database looks like:
You can start your query from the user, so you're sure that every user is present only once.
I'm assuming you have students() relation present on the User::class model
public function handle()
{
$users = User::wherehas('students', function($studentQuery) {
$studentQuery
->whereMonth('birth', date('m-d'))
->whereDay('birth', date('d'));
})
->with('students', function($studentQuery) {
$studentQuery
->whereMonth('birth', date('m-d'))
->whereDay('birth', date('d'));
})
->get();
foreach ($users as $user) {
Mail::to($user)->send(new WelcomeMail($user->students));
}
echo "check email";
}
$student = Student::whereMonth('birth', date('m-d'))
->whereDay('birth', date('d'))
->groupBy('user_id')
->get();
I get this error when running this code
public function update(StoreAccountsRequest $request, $id)
{
$user = User::findOrFail($id);
$user->Accounts()->update($request->except('_method', '_token'))->where('accounts.user_id', '$user');
return redirect()->route('accounts.index')->with(['message' => 'Accounts updated successfully']);
}
please I need help understanding how to update a single user account in the accounts Table
Code must e like this
$user->Accounts()->update($request->except('_method', '_token'));
Little question: Currently I've a route
Route::get('update/{id?}', 'SessionController#onClick');
That loads with this function:
public function onClick($id, Request $request)
{
$data = $request->all();
$user = User::where('userRooms', $id)->first();
return view('sessions.cards')->with('user', $user);
}
I want that my blade view displays all users that have access to this room, currently I hard coded everything. My question is now, what do I have to write down in my code so that users have access to multiple rooms and not just one(userRooms row in my db for User::Class) & how I can display all users that have this room?
I hope I understood your question correctly. Try this way:
public function onClick($id, Request $request)
{
$data = $request->all();
$users = User::where('userRooms', $id)->get();
return view('sessions.cards', [
'users' => $users,
]);
}
$email_ids= DB::table('users')->pluck('email_id');
$name = DB::table('users')-> pluck('name');
foreach ($email_ids as $email_id ) {
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($email_id)->subject("Welcome");
});
}
I want to send a mail to respective users with their names in email. I get $email_ids and $name in array. but when I run this code , I get error as
htmlentities() expects parameter 1 to be string, array given
When I replace 'user' => $name with 'user' => $email_id . I runs successfully.
When I send mail it should be like
Hello , $name(name of user)
Thank You
Your code should be:-
$users = DB::table('users')->get(); // get all users
foreach ($users as $user ) {
$name = $user->name; // get user's name
$email_id = $user->email_id; // get user's email
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($email_id)->subject("Welcome");
});
}
Note:- You can get specific columns from DB table via below query,
$users = DB::table('users')->select('name', 'email_id')->get();
Laravel shows htmlentities() error when there is an error in view file.
In your case you are getting name and email in different-different arrays but you only foreach $email_ids not $name In this line
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
in this $name is array of names return by query and you are printing it as string that's why it is giving that error.
To achieve what you want change your code like this
$users = DB::table('users')->select('name')->addSelect('email_id')->get()->toArray();
foreach ($users _ids as $user ) {
Mail::send('mail', ['user' => $user->name ] , function ($message) use ($user->email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($user->email_id)->subject("Welcome");
});
}
Hi I have the following code in my PasswordController.php but its not working?
// get the user info..
$user = DB::table('users')
->where('email', '=' , Input::get('email'))
->first();
if (empty($user))
return Redirect::back()->with('message', "email doesn't exsist!");
// set email template
Config::set('auth.reminder.email', 'emails.auth.admin-reminder');
$response = Password::remind($credentials, function($message) use ($user){
$message->with('name'=> $user->first_name);
$message->subject(trans('assword Reset'));
});
I am trying to pass $user->first_name to my blade email template? How do I do that?
Thanks
Figured it out ,
Before
Password::remind
we add this :
View::composer('emails.auth.admin-reminder', function($view) use ($user) {
$view->with(['name' => $user->first_name]);
});
You can pass to the view as many variables as you want