Team-Tea-Time/laravel-forum

Question: How to pass some variables to all views in the forum

samialtaher opened this issue · 3 comments

Hello, This is a great package. I just implemented it and initially used the default master layout blade. Then I tried to extend my app layout blade to get maintain my app's look and feel. This layout blade needs some variables such us $user and a couple more so i got an error -
I made a change in the CategoryController and added
$user = $request->user();
$workspace = \App\Models\Workspace::where('id', $user->workspace_id)->first();
and passed those to the view as:
return ViewFactory::make('forum::category.index', compact('categories', 'user', 'workspace'));

This solved the problem for the main page of the forum, but it seems i need to do the same for every controller method/function that generates a view.

My question is: is there a way to make my variables available to all forum views or pages that need them in the forum by adding them in some file?

I know this may be a simple or stupid question to most people, but I am not even a developer, but a 57 year old who have been experimenting and building a site with Laravel 8 by following other people's code, and learning on my own. So I would appreciate any pointers or assistance.

Thanks very much to all.

Riari commented

The best way to solve this problem is by using a view composer: https://laravel.com/docs/9.x/views#view-composers

For example, in your case you could do this in the boot method of your app's service provider:

View::composer('forum::master', function ($view) {
  $view->user = \Illuminate\Support\Facades\Auth::user();
  $workspace = \App\Models\Workspace::where('id', $view->user->workspace_id)->first();
});

Thanks very much. Followed your pointers and all is working fine now. Really appreciate it.
Thanks again for the great package.
One last quick question, is there a way to make the input to accept links, images, videos etc... or make it use TinyMCE editor?

Riari commented

One last quick question, is there a way to make the input to accept links, images, videos etc... or make it use TinyMCE editor?

Sure - there's no support for this out of the box, but you can find some guidance in my reply to #275.