Luiz Villalba
Luiz Villalba

Reputation: 149

Send emails with ID laravel

Let me tell you my problem I've an aplication where the users can reply comments, some post have a forum, where the users can let his own opinion about something, but, my real problem is, when an user let a new comment, the admin can receive an email with the information of the answer, but, in the email have to show this information: user email, forum name, and answer's user, when the answer save in database, save the user_id, forum_id, I need pass the email, and the forum name in the email, but, when I try, don't show the information; here is my code:

public function store(Request $request)
{

  try{
       $comment = new Comment;
       DB::beginTransaction();

           if (Auth::user()) {
             $book = $request->book_id;
              $comment->user_id = Auth::user()->id;
              $comment->forum_id = $book;
              $comment->comment = $request->comment;
              $comment->multimedia_type = $request->multimedia_type;
              $comment->comment_id = $request->comment_id;              
              }
              $comment->save();

              Mail::send('emails.forumEmail', array(
                'user_id'     => $request->get('user_id') , -> I supposse here have the information about user.
                'forum_id'    => $request->get('forum_id') , -> I supposse here have the information about forum.
                'comment'     => $request->get('comment') ,
              ),
              function ($message) use($request)
              {
// When I try pass (
$message->from($request->user_id->email);
                      $message->to('[email protected]')->subject('Han registrado una nueva entrada en el foro' . $request->forum_id->forum_title);
) But nothings happen, didn´t work, exactly
                  $message->from('[email protected]');
                  $message->to('[email protected]')->subject('Han registrado una nueva entrada en el foro');
              });


            DB::commit();
            return back()->with('info', 'Tu comentario ha sido guardado exitosamente.');
            }
            else{
                return back()->with('errors', 'Para participar en el foro debes iniciar sesión');
            }
          } catch (\Exception $e) {
            DB::rollback();
            return back()->with(['errors' => $e->getMessage()]);
          }
        }

If someone can help, I'll be really grateful, please

Upvotes: 1

Views: 1050

Answers (2)

user1436631
user1436631

Reputation:

If $request->user_id is the id of the currently authenticated user,

$message->from(Auth::user()->email);

If it is not the id of the currently authenticated user and if you are passing it through a form,

$message->from(User::findOrFail( $request->user_id )->email);

Upvotes: 2

Camilo
Camilo

Reputation: 7184

Try changing this:

$message->from($request->user_id->email);

Into this:

$message->from(Auth::user()->email);

I'm assuming there is an email column your users table.

Upvotes: 2

Related Questions