Laravel Route parameter I have list of students I retrieve them from the database and listed them in a view
then when i click on any student it show their information in another view
i made that by using the Route parameter /student/{student}
now i want to send an email to this student how to get the id of the route?
and then send this student an email?
here is the email code i just want to get the id from the find method
public function mail()
{
$user = auth()->user();
$email = $user->email;
$name = $user->name;
$student = student::find(1,['id','Email']);
$email1 = $student->Email;
$data = array('name'=>$name);
Mail::send('name', $data, function($message) use ($email,$name,$email1) {
$message->to($email1, 'Tutorials Point')->subject
('Laravel HTML Testing Mail');
$message->from($email,$name);
});
echo "HTML Email Sent. Check your inbox.";
}
Can you show your controller for the show method?
public function show(){
$students = student::all();
return view('student')->with('students',$students);
}
and here where i show the student informations
public function check($student){
$student = \App\student::find($student);
return view('showStudent')->with('student',$student);
}
Your show method would normally show a single student. Index would show all.
Anyways, where are you calling the mail method? Is that just another route or is it from the check method?
here is the mail method ... the mail method is for another route .. the route of the check method is >>'/student/{student}' >> in that route there is a button to send email to this student
public function mail()
{
$user = auth()->user();
$email = $user->email;
$name = $user->name;
$student = student::find(1,['id','Email']);
$email1 = $student->Email;
$data = array('name'=>$name);
Mail::send('name', $data, function($message) use ($email,$name,$email1) {
$message->to($email1, 'Tutorials Point')->subject
('Laravel HTML Testing Mail');
$message->from($email,$name);
});
echo "HTML Email Sent. Check your inbox.";
}
here in the find method at the id parameter i just want to get the id url of the '/student/{student}' instead of writing 1,2,3.....
Ok that route then needs to have the student id just as check has
/student/{student}/mail
And update the mail method (I typehint the student to make it autoload from the database).
public function mail(Student $student)
{
$user = auth()->user();
$email = $user->email;
$name = $user->name;
$email1 = $student->Email;
$data = array('name'=>$name);
Mail::send('name', $data, function($message) use ($email,$name,$email1) {
$message->to($email1, 'Tutorials Point')->subject
('Laravel HTML Testing Mail');
$message->from($email,$name);
});
there is an error when uploading 404/ not found
Uploading? Do you mean going to the url?
Can you show the route and the code in blade for the link?
the error occurs because of the parameter of the mail function (Student)
here is the route
Route::get('/student/{student}/mail', 'studentController@mail');
Are you sure you are passing the student id to it when using it?
i copied your code it did not worked .. but when i used the old method is working but without getting the id from URL i should type the number of the id
Can you please show your blade code. Without it, it is really hard to help. My guess is that you dont pass in the id, which will lead to 404
As you are not using named routes it should be something like this
<a href="/students/{{$student->id}}/mail">Send mail</a>
it worked thank you so much
Please sign in or create an account to participate in this conversation.