【问题标题】:how to pass same data to multiple views in laravel如何将相同的数据传递给laravel中的多个视图
【发布时间】:2020-02-13 19:07:52
【问题描述】:

通过控制器方法,我将$notifications 发送到主视图并在我的网站标题上显示通知。

个人资料视图扩展了主页视图,我还想在个人资料视图上显示通知。

但是当我请求配置文件视图时,它会生成一个未定义变量 $notifications 的错误。

我认为一种解决方案是在从控制器方法返回配置文件视图时发送$notifications,但是在网站上,我想在很多视图上显示通知选项卡,所以这是我想的正确方式。

我通过以下方式返回主页视图

return view('home')->with(['unseen_notification_counter'=>$unseen_notification_counter,'notifications'=>$notifications]);

这是标题部分的主页视图中的代码

<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>

【问题讨论】:

  • 你如何将通知传递给视图?
  • 你能说明你在哪里扩展你的视野吗?
  • 你能说明你是如何获得通知的吗(抱歉)?

标签: php laravel laravel-5 laravel-5.2


【解决方案1】:

如果您想将相同的数据传递给应用程序中的多个视图,您可以使用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);
    });
}

然后,您只需将不同的刀片文件名(就像使用路由一样)添加到数组中。


或者,您可以share 包含所有视图的通知:

public function boot()
{
    $notifications = \App\Notification::all(); //Change this to the code you would use to get the notifications

    view()->share('notifications', $notifications);
}

【讨论】:

    【解决方案2】:

    创建一个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)
        }
    }
    

    希望它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2019-01-21
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      相关资源
      最近更新 更多