Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

php - Guidance on laravel view composer

I have a header in the app layouts with a profile link. When the user clicks it, he will be redirected to his profile. I managed to do something earlier on the ads displayed on the main page, but there I used route model binding like this inside a controller:

public function index(User $user){
      
    $about = $user->profile()->get();
    return view ('profile.index',[
            'user' => $user,
            'about' => $about
        ]);

And then the route:

Route::get('/index/{user}', [ProfileController::class, 'index'])->name('index');

If I've understood correctly, I should be using a view composer or repeat that code in many of my controllers. But do I leave the {user} in the route? And what Am I suppose to write in the composer?

public function compose(View $view)
{
    $view->with('app', $about = $user->profile()->get());
}

And how will the link take the $about variable

<li><a href=" {{route('index', [$about]) }}">Profil</a></li>

Trying this returns an error, which is obvious because I'm still scratching my head to all of this

question from:https://stackoverflow.com/questions/66045335/guidance-on-laravel-view-composer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...