A
A
Aziz Karimov2017-10-05 15:54:11
Laravel
Aziz Karimov, 2017-10-05 15:54:11

How to display two models in one url?

there is 1 layout (welcome.blade.php) and sidebar and main in it, created two models with migrations: Sidebar and Main and, accordingly, two route tables
: got content for main:Route::get('/', '[email protected]');

public function main(Request $request) {
    $mains = Main::all();
    return view('index_hub', ['mains'=>$mains]);

and inserted into the main layout
Question: how to insert a sidebar from the Sidebar model (table) into the layout, because the route is '/' (ie the same)?
ps tried creating a controller Route::get('/', '[email protected]'); but it throws an error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kuznetsov, 2017-10-05
@Aziztwelve

public function main(Request $request) {
    $mains = Main::all();
    $sidebar = Sidebar::all();

    return view('index_hub', ['mains' => $mains, 'sidebar' => $sidebar]);
}

PS: If I understood correctly, then you need to give some data from the model to the view. If this is the case, then it is better to get the model in the view itself, i.e. directly take the model and display it in view.
Let's say view sidebar:
<sidebar>
// тут мб таблица, или просто вывод какой-то определённой инфы.
// тогда просто делаем запрос к модели и выводим
// пример для div таблицы
@foreach(Sidevar::all() as $sidevar)
    <div>{{ $sidebar->name }}</div>
@endforeach
</sidebar>

To be honest, I did not really understand your task. If you don't mind, please describe in more detail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question