4

From a controller method, I send $notifications to the home view and display notifications on a header of my website.

The Profile views extend the home view and I also wanted to display the notifications on the profile view.

But it generates an error that undefined variable $notifications when I request for the profile view.

I think one solution is that to send $notifications when returning profile view from controller method, but in the website, there are many views on which I wanted to show notification tab so it's the right way that I am thinking.

I returned the home view by following way

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

here is the code in the home view in the header section

<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>
3
  • How are you passing the notifications to the views? Commented Aug 29, 2018 at 16:57
  • can you show where are you extending your view? Commented Aug 29, 2018 at 17:19
  • 1
    Can you show how your getting the notifications as well (sorry) ? Commented Aug 29, 2018 at 17:23

2 Answers 2

5

If you're wanting to pass the same data to multiple views within your application you could use View Composers

E.g. in the boot() method of your AppServiceProvider you would have something like:

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

Then you would just add the different blade file names (like you would with a route) to the array.


Alternatively, you could share the notifications with all views:

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

    view()->share('notifications', $notifications);
}
Sign up to request clarification or add additional context in comments.

Comments

4

Create a BaseController, and share data from there like so:

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

}

All controllers that extend BaseController will have access to the data. Do something like this:

class DashboardController extends BaseController {

    public function Index(){
      return view('index'); // all the shared data is available in the view ($notifications and $unseen_notification_counter)
    }
}

Hope it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.