【问题标题】:How to pass parameters between controller methods如何在控制器方法之间传递参数
【发布时间】:2016-05-27 01:50:23
【问题描述】:

我在获取自定义创建方法的价值时遇到问题。我想要的是获取变量 $student_id 并将其放在 index 方法中的 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 相关的值。但是,我无法弄清楚如何实现这一目标。任何人都可以提出解决此问题的方法吗?

【问题讨论】:

  • 我真的不明白。你能简化你的问题吗?
  • @xdevnull 我想要的是获取这条路由Route::get('reservation/{id}/create' 的id参数,并且可以在reservationcontroller中的索引方法中访问
  • 索引应该是从其他地方访问的,您需要手动调用索引或放置一些私有成员。但是,这适用于single request 的多个请求,您可以将其存储在会话或其他地方。
  • @xdevnull 好的,所以在我的预订控制器中的索引方法中,我正在获取学生。问题在于它是硬编码的。我想要的是 $id 值是 Route::get('reservation/{id}/create 中 id 的值。问题是,id 只能用于 create 方法。我想要的是得到id(在这种情况下为 $student_id)。这就是我想要实现的目标。

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


【解决方案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)

我如何通过 $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 的显示功能,因此此处将适用相同的逻辑,

  1. 遍历主题
  2. 显示主题
  3. 遍历学生
  4. 如上例生成anchor tags

所以你会有这样的东西,

@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

【讨论】:

  • 好的,我在 irc 在线
猜你喜欢
  • 1970-01-01
  • 2016-02-05
  • 2015-12-21
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多