在laravel 5.2中处理同一视图的多个Get/Post方法
我基本上有一个视图,它有三个这样的列在laravel 5.2中处理同一视图的多个Get/Post方法,laravel,laravel-5.2,Laravel,Laravel 5.2,我基本上有一个视图,它有三个这样的列 | Users | User's Profile | Profile's Data 第一列列出所有用户,用户的配置文件类似于用户的子列,其中将显示用户创建的配置文件。单击用户1时,视图中应该会出现类似的内容。单击配置文件,相应的配置文件数据应显示在第3列中 | user 1 | User 1- Profile 1 | User 1- Profile 1's data | user 2 | User 1- Profile 2 | Us
| Users | User's Profile | Profile's Data
第一列列出所有用户,用户的配置文件类似于用户的子列,其中将显示用户创建的配置文件。单击用户1时,视图中应该会出现类似的内容。单击配置文件,相应的配置文件数据应显示在第3列中
| user 1 | User 1- Profile 1 | User 1- Profile 1's data
| user 2 | User 1- Profile 2 | User 1- Profile 1's data
| user 3 | User 1- Profile 3 | User 1- Profile 1's data
所有这些事情都应该站在同一个角度。我已经在视图中显示了第一批用户。但在单击用户(用户1或用户2等)时,无法显示相应用户的配置文件。我使用GET方法来显示第1列(用户),并使用id为用户的GET方法来获取他们的所有配置文件。我不知道如何将所有三组数据(用户、用户配置文件和配置文件的数据)传递到同一个视图。以下是我尝试过的,但很困惑如何从这一点上继续下去
这是我的路线
Route::get('/candidates', [
'uses' => 'candidateController@showClient',
'middleware' => 'auth']);
Route::get('/candidates/{id}', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth']);
这是我的控制器
public function showClient(){
$client = new User();
$client_details = $client
->where('role','client')
->get();
return view('admin')->with('client_details',$client_details);
}
public function showProfile($id){
$client = new User();
$client_details = $client
->where('role','client')
->get();
$profile = new Profile();
$user_profiles = $profile
->where('user_id',$id)
->get();
return view('admin')->with('client_details',$client_details)->with('user_profiles',$user_profiles);
}
public function showProfile($id = null){
$client = new User();
$client_details = $client
->where('role','client')
->get();
if (!is_null($id)) {
$profile = new Profile();
$user_profiles = $profile
->where('user_id',$id)
->get();
}
else{
$user_profiles = [];
}
return view('admin')->with('client_details',$client_details)->with('user_profiles',$user_profiles);
}
第二个路由工作正常route::get(/candidates{id},…)
。如何处理此问题?路由文件
Route::get('/candidates/{id?}', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth'
]);
在这里,通过在id参数上添加问号,使其成为可选参数
public function showProfile($id = null) {
$client_details = User::where('role', 'client')->get();
if (!is_null($id)) {
$profile_details = Profile::where('user_id', $id)->get();
} else {
$profile_details = [];
}
return view('admin', compact('client_details', 'profile_details'));
}
现在,在查看时,检查配置文件详细信息是否为空,如果为空,则您有
显示所有客户端页面,否则显示其配置文件页面。路由文件
Route::get('/candidates/{id?}', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth'
]);
在这里,通过在id参数上添加问号,使其成为可选参数
public function showProfile($id = null) {
$client_details = User::where('role', 'client')->get();
if (!is_null($id)) {
$profile_details = Profile::where('user_id', $id)->get();
} else {
$profile_details = [];
}
return view('admin', compact('client_details', 'profile_details'));
}
现在,在查看时,检查配置文件详细信息是否为空,如果为空,则您有
显示所有客户端页面,否则显示其配置文件页面。路由文件
Route::get('/candidates/{id?}', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth'
]);
Route::get('/candidates', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth']);
Route::get('/candidates/{id}', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth']);
这是控制器
public function showClient(){
$client = new User();
$client_details = $client
->where('role','client')
->get();
return view('admin')->with('client_details',$client_details);
}
public function showProfile($id){
$client = new User();
$client_details = $client
->where('role','client')
->get();
$profile = new Profile();
$user_profiles = $profile
->where('user_id',$id)
->get();
return view('admin')->with('client_details',$client_details)->with('user_profiles',$user_profiles);
}
public function showProfile($id = null){
$client = new User();
$client_details = $client
->where('role','client')
->get();
if (!is_null($id)) {
$profile = new Profile();
$user_profiles = $profile
->where('user_id',$id)
->get();
}
else{
$user_profiles = [];
}
return view('admin')->with('client_details',$client_details)->with('user_profiles',$user_profiles);
}
路由文件
Route::get('/candidates/{id?}', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth'
]);
Route::get('/candidates', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth']);
Route::get('/candidates/{id}', [
'uses' => 'candidateController@showProfile',
'middleware' => 'auth']);
这是控制器
public function showClient(){
$client = new User();
$client_details = $client
->where('role','client')
->get();
return view('admin')->with('client_details',$client_details);
}
public function showProfile($id){
$client = new User();
$client_details = $client
->where('role','client')
->get();
$profile = new Profile();
$user_profiles = $profile
->where('user_id',$id)
->get();
return view('admin')->with('client_details',$client_details)->with('user_profiles',$user_profiles);
}
public function showProfile($id = null){
$client = new User();
$client_details = $client
->where('role','client')
->get();
if (!is_null($id)) {
$profile = new Profile();
$user_profiles = $profile
->where('user_id',$id)
->get();
}
else{
$user_profiles = [];
}
return view('admin')->with('client_details',$client_details)->with('user_profiles',$user_profiles);
}
事实上,这有点令人困惑。在同一视图上有用户、用户配置文件和用户数据。单击用户名时,您正在填充配置文件列。你是在用ajax做这件事吗?还是整个页面加载?我没有使用ajax。我正在重新加载页面。我想ajax应该是实现这一点的方法。有什么想法吗?其实有点让人困惑。在同一视图上有用户、用户配置文件和用户数据。单击用户名时,您正在填充配置文件列。你是在用ajax做这件事吗?还是整个页面加载?我没有使用ajax。我正在重新加载页面。我想ajax应该是实现这一点的方法。有什么想法吗?没有,没用,但我对你的答案做了一些小改动。看到我的答案了吗?没用,但我对你的答案做了一些小改动。我的答案没有任何变化。我通过减少一个路由呼叫来展示轻松的处理方式。
{id}
没有将其设置为可选,这就是为什么我将其设置为两个不同的路由。你应该在末尾打问号,比如{id?}。你的意思是说它不起作用?我的答案没有任何变化。我通过减少一个路由呼叫来展示轻松的处理方式。{id}
没有将其设置为可选,这就是为什么我将其设置为两个不同的路由。你应该在末尾打问号,比如{id?}。你的意思是说它不起作用?