Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jalaf11201's avatar

How do we handle this repetition?

Hi, I'm having a hard time to handle little pages. And I have to take user data in all the pages. I will take different data in other pages just like in communities. And also should I put everything community related to community service and use it in community controllers and profile community pages or etc? or Can I just do it like this.

Where can I see some examples for this cases? I always did basic crud, so don't know how to handle this cases.

public function show(Request $request) { $user = User::where('username', $request['username'])->firstOrFail();

    return view('pages.profile.index', [
        'user' => $user
    ]);
}

public function comments(Request $request) {
    $user = User::where('username', $request['username'])->firstOrFail();

    return view('pages.profile.comments', [
        'user' => $user
    ]);
}

public function communities(Request $request) {
    $user = User::where('username', $request['username'])->firstOrFail();

    $communities = Community::query()
        ->where('owner_user_id', $user->id)
        ->when(auth()->id() !== $user->id, function ($query) {
            $query->where('visibility', 'public')
                ->where('status', 'active');
        })->get();

    return view('pages.profile.communities', [
        'user' => $user,
        'communities' => $communities
    ]);
}
public function about(Request $request) {
    $user = User::where('username', $request['username'])->firstOrFail();

    return view('pages.profile.about', [
        'user' => $user
    ]);
}

public function saved(Request $request) {
    $user = User::where('username', $request['username'])->firstOrFail();

    if($user->id !== auth()->user()->id) {
        return back()->with($this->errorMessage('Anauthorized to access.'));
    }

    return view('pages.profile.saved', [
        'user' => $user
    ]);
}

public function hidden(Request $request) {
    $user = User::where('username', $request['username'])->firstOrFail();

    if($user->id !== auth()->user()->id) {
        return back()->with($this->errorMessage('Anauthorized to access.'));
    }

    return view('pages.profile.hidden', [
        'user' => $user
    ]);
}

public function history(Request $request) {
    $user = User::where('username', $request['username'])->firstOrFail();

    if($user->id !== auth()->user()->id) {
        return back()->with($this->errorMessage('Anauthorized to access.'));
    }

    return view('pages.profile.history', [
        'user' => $user
    ]);
}

public function reacted(Request $request) {
    $user = User::where('username', $request['username'])->firstOrFail();

    if($user->id !== auth()->user()->id) {
        return back()->with($this->errorMessage('Anauthorized to access.'));
    }

    return view('pages.profile.reacted', [
        'user' => $user
    ]);
}
1 like
5 replies
ghabriel25's avatar

Just use #[CurrentUser] attribute as dependency injection, it will automatically resolve current authenticated user. For example

use App\Models\User;
use Illuminate\Container\Attributes\CurrentUser;

public function profile(#[CurrentUser] User $user)
{
    return view('pages.profile.index', [
        'user' => $user
    ]);
}

If its to show another user using route model binding, then just type hinting the User class

use App\Http\Controllers\UserController;
use App\Models\User;

// Route definition...
Route::get('/users/{user}', [UserController::class, 'show']);

// Controller method definition...
public function show(User $user)
{
    return view('user.profile', ['user' => $user]);
}

Lastly, I recommend watching https://laracasts.com/series/laravel-from-scratch-2026

2 likes
imrandevbd's avatar

Ghabriel is definitely on the right track, but since you're querying by username instead of the default id, you should use explicit Route Model Binding. You don't need to manually query the database in every single method.

1 like
JussiMannisto's avatar

As mentioned, route model binding is the solution.

If you want to use usernames in the URLs instead of IDs, you can do it like this:

Route::get('/users/{user:username}/comments', [UserController::class, 'comments']);
2 likes
vincent15000's avatar

This is according to me the best approach :

  • use services to have one unique source of code, then you just have to call this service to execute the code when you need it

  • if you need to share the same data with several pages, use a view composer, so you won't need to execute the code in each controller, just once in the view composer and you will access the share data automatically in each view (or only in a selection of views if you prefer)

Please or to participate in this conversation.