如何从控制器传递多个值到视图
How to pass multiple values from controller to view
我有一个表单,其中有许多下拉列表,在创建和编辑记录时从不同的表中填充这些下拉列表
这是代码
<?php
class ParticipantController extends Controller
{
/**
* Store a newly created resource in storage.
*/
public function create(Participant $participant)
{
$this->authorize('create',$participant);
$events = Event::get_name_and_id();
$wards =Ward::get_name_and_id();
$individuals = Individual::get_name_and_id();
$organizations = Organization::get_name_and_id();
$roles = ParticipantRole::get_name_and_id();
$groups = Group::get_name_and_id();
return view('participants.create', compact('events','wards','individuals','organizations','participant','roles','groups'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Participant $participant)
{
$events = Event::get_name_and_id();
$wards =Ward::get_name_and_id();
$individuals = Individual::get_name_and_id();
$organizations = Organization::get_name_and_id();
$roles = ParticipantRole::get_name_and_id();
$groups = Group::get_name_and_id();
return view('participants.edit', compact('events','wards','individuals','organizations','participant','roles','groups'));
}
}
你从代码中可以看出,表单正在从 6 个表中获取数据作为下拉列表,下面是如何做到这一点的。它工作正常,但阅读和理解太多
您可以在单独的方法中重构冗余代码,例如
class ParticipantController extends Controller
{
public function populate($function_name, $participant) {
$events = Event::get_name_and_id();
$wards =Ward::get_name_and_id();
$individuals = Individual::get_name_and_id();
$organizations = Organization::get_name_and_id();
$roles = ParticipantRole::get_name_and_id();
$groups = Group::get_name_and_id();
$data = compact('events','wards','individuals','organizations','roles' ,'groups', 'participant');
return view('participants.' . $function_name , $data);
}
/**
* Store a newly created resource in storage.
*/
public function create(Participant $participant) {
$this->authorize('create',$participant);
return $this->populate(__FUNCTION__, $participant);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Participant $participant) {
return $this->populate(__FUNCTION__, $participant);
}
}
我有一个表单,其中有许多下拉列表,在创建和编辑记录时从不同的表中填充这些下拉列表 这是代码
<?php
class ParticipantController extends Controller
{
/**
* Store a newly created resource in storage.
*/
public function create(Participant $participant)
{
$this->authorize('create',$participant);
$events = Event::get_name_and_id();
$wards =Ward::get_name_and_id();
$individuals = Individual::get_name_and_id();
$organizations = Organization::get_name_and_id();
$roles = ParticipantRole::get_name_and_id();
$groups = Group::get_name_and_id();
return view('participants.create', compact('events','wards','individuals','organizations','participant','roles','groups'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Participant $participant)
{
$events = Event::get_name_and_id();
$wards =Ward::get_name_and_id();
$individuals = Individual::get_name_and_id();
$organizations = Organization::get_name_and_id();
$roles = ParticipantRole::get_name_and_id();
$groups = Group::get_name_and_id();
return view('participants.edit', compact('events','wards','individuals','organizations','participant','roles','groups'));
}
}
你从代码中可以看出,表单正在从 6 个表中获取数据作为下拉列表,下面是如何做到这一点的。它工作正常,但阅读和理解太多
您可以在单独的方法中重构冗余代码,例如
class ParticipantController extends Controller
{
public function populate($function_name, $participant) {
$events = Event::get_name_and_id();
$wards =Ward::get_name_and_id();
$individuals = Individual::get_name_and_id();
$organizations = Organization::get_name_and_id();
$roles = ParticipantRole::get_name_and_id();
$groups = Group::get_name_and_id();
$data = compact('events','wards','individuals','organizations','roles' ,'groups', 'participant');
return view('participants.' . $function_name , $data);
}
/**
* Store a newly created resource in storage.
*/
public function create(Participant $participant) {
$this->authorize('create',$participant);
return $this->populate(__FUNCTION__, $participant);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Participant $participant) {
return $this->populate(__FUNCTION__, $participant);
}
}