3 years ago

Get id parameter of one method and accessed by another method in controllers

Posted 3 years ago by jay_gorio

I am having problems getting the value of custom create method. What I want is to get the variable $student_id and place it on my findOrFail() in the index method as shown below:


public function index()

    $student = Student::with(['sectionSubjects','sectionSubjects.section', 
                'sectionSubjects.subject'])->findOrFail(1); //$student_id

    return $student->sectionSubjects;    

public function create($id)
    $student_id = $id;

    $subjects = Subject::with('sections')->get();
    return view('reservation.form',compact('subjects','student_id'));

Here's my route:

Route::resource('student', 'StudentController');
Route::resource('reservation', 'ReservationController', ['except' => ['create','show'] ]);

    ['as' => 'reservation.create', 'uses' => '[email protected]'

I have this form.blade.php that when a user click on a student it will be redirected to the custom create method in the ReservationController as seen below:

<div class="list-group ">
@inject('student', 'App\Http\Controllers\StudentController')
    @foreach($student->index() as $s)
        <div class="list-group-item">
            <h4 class="list-group-item-heading">
                {{ $s->first_name }} {{ $s->middle_name }} {{ $s->last_name }}</i>
               ID No.: {{ $s->id_no }}</i>
           <a href="{{ route('student.edit', $s->id) }}" class="btn btn-xs btn-primary">Edit Info</a>
           <a href="{{ route('reservation.create', $s->id ) }}" 
              class="btn btn-xs btn-danger">

Now in the index method in the ReservationController, I want to fetch only values that are related to that $student_id. However, I cannot figure out to achieve this. Can anyone suggest ways to solve this?

Please sign in or create an account to participate in this conversation.