jeudi 26 mai 2016

How to get parameter of create method and acces it to the index method using laravel controllers

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:

ReservationController.php

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'] ]);

Route::get('reservation/{id}/create', 
    ['as' => 'reservation.create', 'uses' => 'ReservationController@create'
]);

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">
                  </i>
            </h4>
            <h5>
               ID No.: </i>
            </h5>

           <a href="" class="btn btn-xs btn-primary">Edit Info</a>

           <a href="" 
              class="btn btn-xs btn-danger">
              Enroll
            </a> 

        </div>
    @endforeach

</div>

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire