【问题标题】:Handling multiple Get/Post method to the same view in laravel 5.2在 laravel 5.2 中处理多个 Get/Post 方法到同一个视图
【发布时间】:2017-01-16 13:09:09
【问题描述】:

我基本上有一个像这样的三列视图

| 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 方法来显示第一列(用户)和 GET 方法以及用户的 id 来获取他们的所有个人资料。我不知道如何将所有三组数据(用户、用户配置文件和配置文件的数据)传递到同一个视图。这是我尝试过的,但很困惑如何从这一点开始。

这是我的路线

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

第二条路线运行良好Route::get(/candidates{id},..)。我该怎么办?

【问题讨论】:

  • 实际上有点混乱。您在同一视图上拥有用户、用户配置文件和用户数据。单击用户名时,您正在填充配置文件列。你是用ajax做的吗?还是加载整个页面?
  • 我没有使用 ajax。我正在重新加载页面。我想ajax应该是做到这一点的方法。有什么想法吗?

标签: laravel laravel-5.2


【解决方案1】:

路由文件

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

现在,在视图中,检查个人资料详细信息是否为空,如果是,那么您有 显示所有客户页面,否则显示个人资料页面。

【讨论】:

  • 不,它不起作用,但我对你的答案做了一些小改动。看我的回答
【解决方案2】:

路由文件

Route::get('/candidates', [
    'uses' => 'candidateController@showProfile',
    'middleware' => 'auth']);

Route::get('/candidates/{id}', [
    'uses' => 'candidateController@showProfile',
    'middleware' => 'auth']); 

这里是控制器

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

【讨论】:

  • 我的答案没有任何变化。通过减少一个路由调用,我展示了一种更简单的处理方式。
  • {?id} 没有让它工作可选,这就是为什么我把它作为两条不同的路线
  • 你应该把问号放在最后,比如 {id?}。你的意思是说它有用吗?
猜你喜欢
  • 2017-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多