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
    ]);
}
0 likes
3 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

1 like
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.

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']);
1 like

Please or to participate in this conversation.