gpt4 book ai didi

php - 从不同的 Controller 输出到相同的 View

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:21 27 4
gpt4 key购买 nike

是否可以从多个 Controller 输出到同一个 View 。我有一个名为“仪表板”的 View 。我有两个变量:$users$friends。我想从不同的 Controller 向这些变量发送数据。我需要将两个 Controller 添加到同一条路线吗?

View :

<div class="panel friendlist" id="friendlist">
<div class="panel-heading"><h3 class="panel-title">Result List</h3>
</div>
<div class="panel-body">
<ul class="list-group">
@foreach($friends as $friend)
<li class="list-group-item"><a href="">{{ $friend->username }}</a>
</li>
@endforeach
</ul>
</div>
</div>
</section>
<section class="row posts">
<div class="col-md-6 col-md-3-offset">
<header><h3>other posts</h3></header>
@foreach($posts as $post)
<article class="post">
<p>{{ $post->content }}</p>
<div class="info">Posted by {{ $post->user->username }} on {{ $post->created_at }}</div>

帖子 Controller :

public function getDashboard()
{
$posts = Post::orderBy('created_at','desc')->get();
return view('dashboard',['posts' => $posts]);
}

好友 Controller :

public function getFriends()
{
$friends = Auth::user()->friends()->where('status','accepted')->get();

return view('dashboard',['friends' => $friends]);
}

当前路线:

Route::get('/dashboard',[
'uses' => 'PostController@getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);

最佳答案

@Amartya Barua,您可以使用 View 编辑器将一些变量共享到多个 View https://laravel.com/docs/5.8/views , 或者你可以创建 BaseService, 并编写可重用的 getters 并将 BaseService 注入(inject)你的 Controller , 这样你就可以访问所需的 getters 到你的 Controller , 如果有任何问题评论我的答案

关于php - 从不同的 Controller 输出到相同的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56086929/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com