C
C
chelkaz2016-03-08 23:16:24
Laravel
chelkaz, 2016-03-08 23:16:24

What options for the logic of end-to-end information in the section can you recommend?

There is a sidebar. How to transfer information to it? Which is visible on any page. In it, for example, the last comment, or the last announcement, or something else (from different database tables)
How is it better to do this? At the moment the logic is not clear. For a separate page, it’s clear, through a router and a controller. But how to implement for a pass-through block?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Ernest Faizullin, 2016-03-08
@chelkaz

I can suggest using the so-called composers. Create a View/Composers folder in the app folder, create a class/file in the Composers folder, let's call it AddStatistic.php:

namespace App\View\Composers;

use App\Comment;
use App\Blog;
use Illuminate\View\View;

class AddStatistic
{
    public function compose(View $view)
    {
        $statistic = [
            'last_comments' => Comment::getLast(),
            'top_rated' => Blog::topRated(),
        ];
        $view->with('statistic', $statistic);
    }
}

then it's better to move the block with statistics to a separate view, let's say 'partials.statistic' , then add use App\View\Composers to Providers/AppServiceProvider.php and add composer to the view of the block with statistics in the boot method, which will pass $statistic there:
namespace App\Providers;

use App\View\Composers;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        ...
        $this->app['view']->composer(['partials.statistic'], Composers\AddStatistic::class);
    }
    
    ...
}

S
Stanislav Pochepko, 2016-03-09
@DJZT

view composer. Read about him. Make it like a widget.
Oops... Didn't notice you already answered

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question