user3386779 user3386779 - 1 month ago 6
PHP Question

passing the query value to mail template in laravel

I want to pass the query value and get the value in mail template.I want to pass data as ['users'=>$users] .I caught internal server error

controller

$users = DB::table('travel_request')
->join('department', 'travel_request.department_id', '=', 'department.id')
->join('users', 'travel_request.approver_id', '=', 'users.id')
->join('travel_purpose', 'travel_request.travel_purpose_id', '=', 'travel_purpose.id')
->join('traveling_details', 'travel_request.id','=','traveling_details.travel_request_id' )
->join('country','country.id', '=', 'traveling_details.country_id')
->join('travel_type','traveling_details.travel_type_id', '=', 'travel_type.id')
->join('travel_request_status','travel_request.status_id', '=', 'travel_request_status.id')
->select('travel_request.*', 'travel_request.id as travel_id','department.name as dept_name','users.firstname as approver_name','travel_purpose.purpose','country.name as country_name','traveling_details.from_date','traveling_details.to_date','travel_request_status.status','travel_type.type as travelType')->where('travel_request.id',$id)->get();

Mail::Send('email.welcome', $users, function($message) use($users) {
$message->to('sathurka.palpandi@aspiresys.com', 'Travel Request')->subject
('Testing Mail');
$message->from('ganesh.veilsamy@aspiresys.com','Ganesh Veilsamy');
});


welcome.blade.php
@foreach ($users as $user)
{{$user->traveler_name}}
@endforeach

Answer

You can use like below

$data['users'] =$users;
Mail::Send('email.welcome', $data, function($message) use ($data) {
   $message->to('sathurka.palpandi@aspiresys.com', 'Travel Request')->subject
        ('Testing Mail');
   $message->from('ganesh.veilsamy@aspiresys.com','Ganesh Veilsamy');
});


welcome.blade.php
 @foreach ($users as $user)
    {{$user->traveler_name}}
 @endforeach

This is just for notification.