A
A
Alexander2022-04-08 14:30:32
PHP
Alexander, 2022-04-08 14:30:32

How to properly implement view return in Laravel?

I make a list of users, and their profiles, and here the question arose.
There is a page /users/{id}/ configured a route + controller, I give a view. Then the question arose to display another view if the page belongs to a logged in user.
Something tells me what to block in the controller in each method like this:

if(Auth::id() == $user->id) {
            return view('profile.my.show', compact('user', 'departments', 'users'));
        } else {
            return view('profile.show', compact('user', 'departments', 'users'));
        }

Not the best solution, how do they solve such a problem? Maybe there is some system function that I don't know about? I'm new and just learning.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lil_koi, 2022-04-08
@lil_koi

you can inside

return view('profile.my.show', compact('user', 'departments', 'users'));

Insert the name of the view using the ternary operator.
Drop down to paragraph
Ternary operator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question