如何将相同的数据传递给 laravel 中的多个视图
how to pass same data to multiple views in laravel
通过控制器方法,我将 $notifications
发送到主页视图并在我网站的 header 上显示通知。
个人资料视图扩展了主页视图,我还想在个人资料视图上显示通知。
但是当我请求个人资料视图时,它会生成一个未定义变量 $notifications 的错误。
我认为一种解决方案是在从控制器方法返回配置文件视图时发送 $notifications
,但是在网站上,有很多视图我想显示通知选项卡,所以这是我正确的方法我在想。
我通过以下方式返回首页视图
return view('home')->with(['unseen_notification_counter'=>$unseen_notification_counter,'notifications'=>$notifications]);
这里是 header 部分主页视图中的代码
<ul class="dropdown-menu" id="notificationlist">
@foreach($notifications as $notification)
<li>
<a href="{{route('user.profile',$notification->id)}}" class="dropdown-item">
<img src="http://localhost/webproject/public/user_images/{{$notification->image}}" class="img-thumbnil" width="20px" height="20px">
<strong>{{$notification->username}}</strong>
<span style="white-space: initial;">sent you a friend request.</span>
</a>
</li>
@endforeach
</ul>
创建一个 BaseController
,然后从那里共享数据,如下所示:
<?php
namespace App\Http\Controllers;
use View;
//You can create a BaseController:
class BaseController extends Controller {
public $dataVariable = "some data";
public function __construct() {
$anotherVariable = "more data";
$notifications = Notification::where('is_seen',0)->get(); // assuming this gets unseen notifications
$unseen_notification_counter = count($notifications);
View::share ( 'notifications', $notifications );
View::share ( 'unseen_notification_counter', $unseen_notification_counter );
View::share ( 'data_variable', $this->dataVariable );
View::share ( 'another_variable', $this->anotherVariable );
}
}
扩展 BaseController
的所有控制器都可以访问数据。做这样的事情:
class DashboardController extends BaseController {
public function Index(){
return view('index'); // all the shared data is available in the view ($notifications and $unseen_notification_counter)
}
}
希望有用。
如果您想将相同的数据传递给应用程序中的多个视图,您可以使用 View Composers
例如在你的 AppServiceProvider 的 boot()
方法中你会得到类似的东西:
public function boot()
{
view()->composer(['home', 'profile'], function ($view) {
$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications
$view->with('notifications', $notifications);
});
}
然后您只需将不同的 blade 文件名(就像您使用路由一样)添加到数组中。
或者,您可以share所有视图的通知:
public function boot()
{
$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications
view()->share('notifications', $notifications);
}
通过控制器方法,我将 $notifications
发送到主页视图并在我网站的 header 上显示通知。
个人资料视图扩展了主页视图,我还想在个人资料视图上显示通知。
但是当我请求个人资料视图时,它会生成一个未定义变量 $notifications 的错误。
我认为一种解决方案是在从控制器方法返回配置文件视图时发送 $notifications
,但是在网站上,有很多视图我想显示通知选项卡,所以这是我正确的方法我在想。
我通过以下方式返回首页视图
return view('home')->with(['unseen_notification_counter'=>$unseen_notification_counter,'notifications'=>$notifications]);
这里是 header 部分主页视图中的代码
<ul class="dropdown-menu" id="notificationlist">
@foreach($notifications as $notification)
<li>
<a href="{{route('user.profile',$notification->id)}}" class="dropdown-item">
<img src="http://localhost/webproject/public/user_images/{{$notification->image}}" class="img-thumbnil" width="20px" height="20px">
<strong>{{$notification->username}}</strong>
<span style="white-space: initial;">sent you a friend request.</span>
</a>
</li>
@endforeach
</ul>
创建一个 BaseController
,然后从那里共享数据,如下所示:
<?php
namespace App\Http\Controllers;
use View;
//You can create a BaseController:
class BaseController extends Controller {
public $dataVariable = "some data";
public function __construct() {
$anotherVariable = "more data";
$notifications = Notification::where('is_seen',0)->get(); // assuming this gets unseen notifications
$unseen_notification_counter = count($notifications);
View::share ( 'notifications', $notifications );
View::share ( 'unseen_notification_counter', $unseen_notification_counter );
View::share ( 'data_variable', $this->dataVariable );
View::share ( 'another_variable', $this->anotherVariable );
}
}
扩展 BaseController
的所有控制器都可以访问数据。做这样的事情:
class DashboardController extends BaseController {
public function Index(){
return view('index'); // all the shared data is available in the view ($notifications and $unseen_notification_counter)
}
}
希望有用。
如果您想将相同的数据传递给应用程序中的多个视图,您可以使用 View Composers
例如在你的 AppServiceProvider 的 boot()
方法中你会得到类似的东西:
public function boot()
{
view()->composer(['home', 'profile'], function ($view) {
$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications
$view->with('notifications', $notifications);
});
}
然后您只需将不同的 blade 文件名(就像您使用路由一样)添加到数组中。
或者,您可以share所有视图的通知:
public function boot()
{
$notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications
view()->share('notifications', $notifications);
}