Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iabrar95's avatar

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.";

}

0 likes
15 replies
Sinnbeck's avatar

Can you show your controller for the show method?

iabrar95's avatar

public function show(){

    $students = student::all();

    return view('student')->with('students',$students);
}
iabrar95's avatar

and here where i show the student informations

public function check($student){

    $student = \App\student::find($student);

    return view('showStudent')->with('student',$student);

    
}
Sinnbeck's avatar

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?

iabrar95's avatar

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.";

}

iabrar95's avatar

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.....

Sinnbeck's avatar

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);
});
iabrar95's avatar

there is an error when uploading 404/ not found

Sinnbeck's avatar

Uploading? Do you mean going to the url?

Can you show the route and the code in blade for the link?

iabrar95's avatar

the error occurs because of the parameter of the mail function (Student)

here is the route Route::get('/student/{student}/mail', 'studentController@mail');

Sinnbeck's avatar

Are you sure you are passing the student id to it when using it?

iabrar95's avatar

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

Sinnbeck's avatar

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

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

As you are not using named routes it should be something like this

<a href="/students/{{$student->id}}/mail">Send mail</a>

Please or to participate in this conversation.