S
S
SleepGame2017-04-27 16:02:06
Laravel
SleepGame, 2017-04-27 16:02:06

How to implement code in laravel to display information in one block?

The point is, I'll try to get it right.
There is a main admin file:

<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
    <head>
 @include('admin.inc.head')
    </head>
<body>
@include('admin.inc.authpanel')
@include('admin.inc.subnavbar')
<!-- /subnavbar -->
         @yield('content')
@include('admin.inc.footer')
<!-- /footer -->

Where the data arrives, through a directive @yieldfor a specific action.
If you need to create an article, then the file data is loaded create.blade.phpinto @yield.
In the create file (and so on for all the others), through include, a code is inserted with a column where a list of all articles is displayed.
This code has variables that are passed from the PostController and the create method to the create.blade.php file.
but if we load, for example, the update method, and do not pass the variables for the list there, then an error will appear (that is, you need to specify these variables in all methods).
Variables are passed through an array
$data = [
        'posts'=>Post::where('id', '<>', $id)->get(), 
        'article'=>Article::find($id),//выбираем статью для редактирования
        'categories'=>Category::all(),
        ];

Now to the point, how to make sure that you don’t constantly write in each method that loads a file with a column, an array with variables and they can be edited later from one place?
I'm a beginner, I'm starting to work with php framework. Do not judge strictly

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Barmunk, 2017-04-28
@Barmunk

I use https://laravel.com/docs/5.4/views#view-composers for this . In a separate class, which is loaded through the service provider, we fumble any data directly into the necessary views. The only negative is that the composer starts after the controller, before the view, which can sometimes cause discomfort

F
Flasher, 2017-05-02
@Flasher

This is probably what you are looking for On all pages, @yield does not display anything. How to share data between pages in a blade?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question