如何在控制器方法之间传递参数

时间:2016-05-27 01:50:23

标签: laravel laravel-5 laravel-5.1 laravel-5.2

我在获取自定义创建方法的价值方面遇到了问题。我想要的是获取变量$ student_id并将其放在我的findOrFail()中,如下所示:

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

这是我的路线:

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

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

我有这个form.blade.php,当用户点击学生时,它将被重定向到ReservationController中的自定义创建方法,如下所示:

<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>
            </h4>
            <h5>
               ID No.: {{ $s->id_no }}</i>
            </h5>

           <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">
              Enroll
            </a> 

        </div>
    @endforeach

</div>

现在在ReservationController的index方法中,我想只获取与该$ student_id相关的值。但是,我无法想象实现这一目标。任何人都可以建议解决这个问题的方法吗?

1 个答案:

答案 0 :(得分:1)

实际上,您不会遇到任何问题Logic问题。

Controller未按正确方式设计。

你需要有这样的东西

//List all subjects
public function index() {
    return view('reservation.form')->with('subjects', Subject::with('sections')->get());
}

//List a single subject
//If i didn't misunderstood you, you can remove this one according to the source code you're using.
public function show($id) {
    return view('reservation.oneSubject')->with('subject', Subject::find($id));
}

//Enroll a student to subject
public function enroll($id, $student_id) {
    //Find the section for $id
    //Add student_id to that section
}

您需要在此示例中定义一条额外的路线GETPOST,我可以使用GET

Route::get('/reservation/{id}/enroll/{student_id}', 'ReservationsController@enroll');

我应遵循什么逻辑?

  1. 列出所有科目(将致电index()
  2. 选择一个主题(将调用show($id)
  3. 向学生展示。
  4. 点击添加按钮(将调用enroll($id, $student_id)
  5. 如何传递$ id,$ student_id才能注册?

    您的预订资源将包含这些路线。

    /reservations
    /reservations/{id}/store
    etc..
    
    示例中的

    id参数,指向Subject而不是学生。

    让我们说你有一个show($id)功能,它会显示单个主题和学生列表,

    return view(...)->with('subject', Subject::find($id)->with('students', YOUR_STUDENTS);
    

    在视图中,假设您已经$students

    ,请在学生中进行迭代
    @foreach($students as $student)
    <a href="{{ action('ReservationController@enroll', [$subject->id, $student->id']) }}">Enroll student</a>
    @endforeach
    

    我没有show()功能!

    由于您没有显示单个subject的show函数,因此此处将应用相同的逻辑,

    1. 迭代主题
    2. 显示主题
    3. 迭代学生
    4. 如上例所示生成anchor tags
    5. 所以你会有这样的事情,

      @foreach($subjects as $subject)
      <h1>{{ $subject->title }}</h1>
          @foreach($students as $student)
           <div class="students">
              <h2> {{ $student->name }}</h2>
              <a href="{{ action('ReservationController@enroll', [$subject->id, $student->id]) }}">Enroll</a>
           </div>
          @endforeach
      @endforeach