U
U
Uglik2016-04-08 11:50:00
Laravel
Uglik, 2016-04-08 11:50:00

How to display general profile information on multiple templates?

Hello.
How in laravel 5.2 to implement the output of general information in several templates?
route

Route::get('/profile/{username}', '[email protected]')->name('profile.show');
Route::get('/profile/{username}/edit', '[email protected]')->name('profile.edit');

controller
/**
     * Show User Profile.
     *
     * @param $username
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showProfile($username)
    {
        $user = User::where('username', '=', $username)->firstOrFail();

        return view('profile.activity', compact('user'));
    }

    /**
     * Edit user information
     *
     * @param $username
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function editProfile($username)
    {

        $user = User::where('username', '=', $username)->firstOrFail();

        return view('profile.settings', compact('user'));
    }

As I understand it, you need to create a composerServiceProvider and compose the template, but it does not work because it cannot determine get username?
How to correctly pass the username parameter to all templates in order to display information about the authorized user in the system?
And another question about the User model. In laravel 5.2 class User extends Authenticatable. I tried to write function getUserName() {return $this->username} and call it from UserController, but nothing happens.
How to generally display logged in user profile information in laravel on multiple profile pages?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Pochepko, 2016-04-08
@DJZT

Everything is correct. Use ComposerView . Have you read the documentation? it 's pretty well laid out here.
You attach a handler to a certain view, which will work and slip data there every time this view is used.

I
IceJOKER, 2016-04-08
@IceJOKER

And look towards the Route model binding, so that each time you do not copy-paste the same code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question