K
K
Karpkarp2020-02-05 16:17:14
Laravel
Karpkarp, 2020-02-05 16:17:14

How to write a route in Laravel?

The site opens along the route The header and footer of the site are placed in a single template, where I already connect the rest of the site. And then I was impatient to display 3 entries from the blog in the footer. I made a controller with a method:Route::get('','[email protected]');

public function index()
    {
        $blogs = Blog::join('users', 'author_id','=', 'users.id')
            ->orderBy('blogs.created_at', 'desc')
            ->limit(3)
            ->get();

        return view('layouts.layout', compact('blogs'));
    }


layouts.layout is the same template with a header and a footer, I set the route and it clearly overlaps the first route. I tried to transfer the method to the PostController, and everything worked, but only on the main page. When you try to walk through the pages, an error about an unknown variable takes off. In principle, it's also clear ... How can I correctly register the route for displaying posts in the footer? Route::get('','[email protected]');


Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2020-02-05
@Karpkarp

And what about the routes? We read the documentation in general and about the view composer in particular.

A
Alexander, 2020-02-06
@QcfgAlexandr

If I'm not mistaken, it should help:
We take this out in __construct

$blogs = Blog::join('users', 'author_id','=', 'users.id')
            ->orderBy('blogs.created_at', 'desc')
            ->limit(3)
            ->get();

and addview()->share(['blogs ' => $blogs]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question