Laravel 如何在控制器方法之间传递参数
我在获取自定义创建方法的值时遇到问题。我想要的是获取变量$student_id并将其放置在index方法中的findOrFail()上,如下所示: ReservationController.phpLaravel 如何在控制器方法之间传递参数,laravel,laravel-5,laravel-5.1,laravel-5.2,Laravel,Laravel 5,Laravel 5.1,Laravel 5.2,我在获取自定义创建方法的值时遇到问题。我想要的是获取变量$student_id并将其放置在index方法中的findOrFail()上,如下所示: ReservationController.php public function index() { $student = Student::with(['sectionSubjects','sectionSubjects.section', 'sectionSubjects.subject'])->
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>
@注入('student'、'App\Http\Controllers\StudentController')
@foreach($student->index()作为$s)
{{$s->first_name}{{$s->middle_name}{{{$s->last_name}
ID号:{{$s->ID号}
@endforeach
现在在ReservationController中的index方法中,我只想获取与$student_id相关的值。但是,我不知道如何实现这一点。有人能提出解决这个问题的方法吗?事实上,除了逻辑问题之外,你没有任何问题 您的
控制器设计不正确
你需要像这样的东西
//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
}
您需要定义一个额外的路由,可以是GET
或POST
,在本例中,我使用的是GET
Route::get('/reservation/{id}/enroll/{student_id}', 'ReservationsController@enroll');
我应该遵循什么逻辑?
列出所有主题(将调用index()
)
选择一个主题(将调用show($id)
)
向学生展示
单击添加按钮(将调用注册($id,$student\u id)
如何通过$id、$student\u id注册?
您的预订资源将拥有这些路线
/reservations
/reservations/{id}/store
etc..
示例中的id
参数,指向Subject
而不是Student
假设您有一个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
我真的不明白。你能简化你的问题吗?@xdevnull我想要的是获取这个路由的id参数route::get('reservation/{id}/在reservationcontrollerindex的my index方法中创建并可访问应该是从其他地方访问的,您需要手动调用index或放置一些私有成员。但是这将适用于单个请求
,对于多个请求,您可以将其存储在会话或其他地方。可能存在@xdevnull的重复项OK我在reservationcontroller中的索引方法获取学生。问题是它是硬编码的。我想要的是$id value是Route::get('reservation/{id}/create)中id的值。问题是,id只能由create方法访问。我想要的是获取id(在这种情况下是$student\u id).这就是我想要实现的.好的,我在irc在线
@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