Laravel我如何从多个控制器获取信息到一个视图中?

Laravel我如何从多个控制器获取信息到一个视图中?,laravel,model-view-controller,Laravel,Model View Controller,我正在使用Laravel构建一个应用程序。其结构如下: 一个用户->haOne->Profile->hasMany->讨论->hasMany->回复hasMany->喜欢 像往常一样,每个模型都有一个控制器,具有典型的CRUD功能:DiscussionController、PaymentController、ImageController、ProfileController、RoleControl、PostsController、MessageController 在管理区域,我管理并查看每个用

我正在使用Laravel构建一个应用程序。其结构如下:

一个用户->haOne->Profile->hasMany->讨论->hasMany->回复hasMany->喜欢

像往常一样,每个模型都有一个控制器,具有典型的CRUD功能:DiscussionController、PaymentController、ImageController、ProfileController、RoleControl、PostsController、MessageController

在管理区域,我管理并查看每个用户的所有相关信息

web.php中的路径是:

Route::resource('admin-profiles', 'AdminProfileController');
我的问题出现在管理配置文件.show视图中,该视图是通过此方法提供的AdminProfileController@show

public function show($slug)
{
    $profile = Profile::where('slug', $slug)->first();
    $page_name = $profile->name;

    return view('admin.profiles.show', compact('profile', 'page_name'));
}
这里我需要所有关于用户的信息(个人资料、讨论、帖子回复、图片、喜好、付款、渠道等)

我可以在AdminController中构建一个巨大的show方法,并将一大堆变量传递给该视图,如下所示:

    $discussions = Discussion::where('profile_id', $profile->id)get);
    $replies= Reply::where('profile_id', $profile->id)->paginate(4);
...
    and so on until 27 queries
    <span class="mt-3 small pull-right">
Accumulated Likes: {{ $user->profile->all_likes($user->id) }}
</span>
但在我看来,这是一个糟糕的解决方案,因为我已经为每个模型配备了一个控制器

我确实这样调用了UserController:

    $discussions = Discussion::where('profile_id', $profile->id)get);
    $replies= Reply::where('profile_id', $profile->id)->paginate(4);
...
    and so on until 27 queries
    <span class="mt-3 small pull-right">
Accumulated Likes: {{ $user->profile->all_likes($user->id) }}
</span>
但它没有起作用


如何从HTML视图调用不同控制器中的方法?

如果所有内容都建立在关系之上,那么就使用它们

一个用户->hasOne->Profile->hasMany->讨论->hasMany->回复->hasMany->喜欢

一个用户->hasOne->Profile

在UserModel中:

public function profile()
{
  return $this->hasOne(ProfileModel::class);
}
在控制器中:

$user->profile;
在ProfileModel中:

public function discussions()
{
  return $this->belongsToMany(DiscussionsModel::class, ... youre fields);
}
//类似于以下关系回复

在控制器中:

$profile->discussions;

$user->profile; //it's Collections relationships one by one
$user->profile->discussions; //it's Collections relationships hasMany
$user->profile->discussions->first()->replies; //replies to first user discussions. you can go foreach. and so on
鉴于:

控制器

public function show(int $id)
{
    $user = UserModel::find($id);

    return view('admin.profiles.show', [
      'user' => $user
    ]);
}
查看

<body>
    {{ $user->profile; }} 
    {{ $user->profile->discussions; }}
    {{ $user->profile->discussions->first()->replies; }}
</body>

{{$user->profile;}}
{{$user->profile->discussions;}
{{$user->profile->discussions->first()->回复;}

{{WhateverController::public_staticFunction()}}@Amarnasan,谢谢你的回答,但是我如何从HTML视图中称之为“WhateverController”?因为我得到了错误:未定义变量:all_likes(视图:…\views\admin\users\show.blade.php)。另一方面,这意味着我必须使所有控制器中的所有方法都是静态的?{{\App\Http\Controllers\WhateverController::public\u staticFunction()}}。控制器只是另一个类。如果你想访问它的方法,你需要重启它或者调用静态方法。如何将来自不同控制器的所有这些变量放到一个视图中。因为我需要所有东西都在一个视图中。我已经建立了全面的关系。我没有问题获得控制器中的所有信息。我的问题是如何从HTMLPerhaps调用这些信息,我无法解释自己。问题是如何调用该函数(在您的示例中)。因为我需要在不同的控制器(DiscussionController、PaymentController、ImageController、ProfileController、RoleControl、PostsController、MessageController等)中调用多个自定义函数,以向我的管理仪表板提供信息。最终,我将在不同的控制器中使用多个同名函数。如果我只是以{{$variable}}为例,我会得到一个错误消息:Undefined variable:all_likes(视图:…)