1

I have a controller where I get the unread comments on the projects of the logged in user. Now my problem is I need this data in my nav partial to show it on all my views. But unfortunately this does not work.

Here's my route:

Route::get('/', 'KlantController@getUnreads');

Here's my function in my KlantController

public function getUnreads(){
        $uid = Auth::user()->id;
        $projects = User::find($uid)->projects;

        //comments
        if (!empty($projects)) {
            foreach ($projects as $project) {     
                $comments_collection[] = $project->comments;
            }
        }

        if (!empty($comments_collection)) {
            $comments = array_collapse($comments_collection);
            $unreads = collect($comments)->where('unread', "1")->count();
        }
        return view('partials.nav', $unreads);
    }

And here's where I use the data in my partial.

@if($unreads > 0)
  <span class="label label-danger">{{ $unreads }}</span>
@endif

Annoying enough this gives me multiple times the following error:

Undefined variable: unreads (View: /var/www/resources/views/partials/nav.blade.php)

Anyone has any idea how I can fix this? Any help is appreciated!

Many thanks in advance :)

4 Answers 4

1

The data you are passing should be an array with key/value pairs.

From the Laravel documentation:

When passing information in this manner, $data should be an array with key/value pairs. Inside your view, you can then access each value using its corresponding key

In your case, you can just use the compact function.

return view('partials.nav', compact('unreads'));
Sign up to request clarification or add additional context in comments.

1 Comment

define your function as public function getUnreads($unreads=0). Because if there are not any comments, unreads variable is undefined.
1

I am working currently for one client in Laravel 5.1 (YES, I know please don't ask).

Anyways to help somebody perhaps.

In the controller:

public function blogIndex()
{
    $posts = Blog::orderBy('date')
        ->where('status', 'Published')
        ->whereDate('date', '<', Carbon::now())
        ->paginate(10);

    return view('pages.blog.index', compact('posts'));
}

In one of the views, we have partial.

@include('partials.blog-categories-nav', ['posts' => $posts]);

In that partial resources/views/partials/blog-categories-nav.blade.php

{{ dd($posts) }}

The output clearly you get:

Did and Dump

Perhaps, just perhaps you guys forgot to include {{ }}, curly brackets or perhaps $ in $var.

If for some reason it is NOT still working, you can always use {{ dd(get_defined_vars()) }} in views.

Which will output all of your variables in for us well known die and dump dd format.

Cheers hope this will help someone;

I wish you happy coding.

Have a nice day my fellow developers.

Comments

0
return view('partials.nav')->with(['unreads' => $unreads]);

And in your controller, you can start it this way:

$uid = Auth::user()->id;
$projects = User::find($uid)->projects;
$unreads = 0

So, if theres no comments, the $unreads vars will exists in your view anyway.

Comments

0

Try this:

In your function:

return view('partials.nav', ['unreads' => $unreads]);

In your view:

@if(count($unreads) > 0)
  @foreach ($unreads as $unread)
     <span class="label label-danger">{{ $unread }}</span>
  @endforeach
@endif

5 Comments

This doesn't work unfortunately, I still get the same error :(
Yep, I do. I get it 3 times actually. I think it is because I have 3 comments in my database
Hum.... In your if, use the count function @if(count($unreads) > 0). And I think that maybe you should use a @foreach because your $unreads variable is an array.
can't be an array it's just a ->count()
Nope still don't work :'( same error. I think something is wrong with my route maybe?

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.