A
A
Artur Galyaev2020-06-10 20:58:31
Laravel
Artur Galyaev, 2020-06-10 20:58:31

What is the best way to pass data to the view?

I have 2 models: articles and categories. I display articles on the page, categories in the header. What is the best way to pass data to a view?

use App\Posts;
use App\Categories;
class PostController extends Controller
{
    function __invoke()
    {
        $articles = Posts::orderBy('id', 'desc')
            ->paginate(5);
        return view('articles', ['articles' => $articles, 'categories' => Categories::all()]);
    }
}

Should I do this or is there a better way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-06-10
@art5455

I understand that the categories in the header are not only on this page?
If so, then it is better to pass categories using View Composers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question