Reputation: 637
i want to display the counter in my app.blade which being called in all pages like the screenshot below
I only have this function in my controller
class ReportsController extends Controller
{
public function invoiceTransaction()
{
$salespayments = Salespayments::where('type','=','check')->get();
$countUnread = Salespayments::select(DB::raw("SUM(unread) as unread"))->get();
return view('reports.invoiceTransactions')
->with('salespayments', $salespayments)
->with('countUnread', $countUnread);
}
}
And I am calling the counter in my blade by this {{$countUnread[0]->unread}}
How can I make that function be readable in my app.blade.php? thanks a lot!
Upvotes: 1
Views: 576
Reputation: 224
In your AppServiceProvider
you can share the sum
result across all views by using view()->share();
Like this:
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot() {
$countUnread = Salespayments::sum('unread');
view()->share('countUnread', $countUnread);
}
Upvotes: 2
Reputation: 3065
Make a service provider first,
php artisan make:provider CounterServiceProvider
Then in your CounterServiceProvider
file.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Views\Composers;
class CounterServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// here define your master layout
$this->app['view']->composer(['master'], Composers\Counter::class);
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
Now in your App\Views\Composers
folder add Counter
class.
<?php
namespace App\Views\Composers;
use Illuminate\View\View;
class Counter {
public function compose(View $view)
{
$view->with('countUnread', session('countUnread'));
}
}
Make sure you add your CounterServiceProvider
in config/app.php file's providers array.
Upvotes: 0