0

I am trying to retrieve a user profile using a unique slug. With the user signed in, I can only get the first profile recorded in the db instead of the signed in user, and I am not sure why? I still wanna be able to see the profiles if I am not signed in.

The address bar should read

.app:8000/profile/signedinsuer , or selected user

instead it always reads

.app:8000/profile/firstuser .

I can manually change the address bar, and the rest works as it should, I get the right view etc. I'm just not getting the right slug through.

Route

Route::group(['prefix' => '/profile'], function () {
    Route::get('/{profile}', 'Profile\ProfileController@index')->name('profile.index');

Controller

class ProfileController extends Controller
{
    public function index(Profile $profile)
    {
        return view('overview.profile.index', [
            'profile' => $profile,
        ]);
    }
}

From Blade

<li>
    <a href="{{ route('profile.index', [$profile]) }}">Profile</a>
</li>

Composer

class ProfileComposer
{
    public function compose(View $view)
    {
        if (!Auth::check()) {
            return;
        }

        $view->with('profile', Auth::user()->profile->first());

    }
}
Dustin
  • 112
  • 4
  • 15
  • Why are u using a `ProfileComposer` for? – Nikola Gavric Feb 10 '18 at 21:37
  • 1
    This `$view->with('profile', Auth::user()->profile->first())` should be `$view->with('profile', Auth::user()->profile)` OR `$view->with('profile', Auth::user()->username)` depending on what column you want to pick their name from. – Stanley Umeanozie Feb 10 '18 at 22:21
  • Stanley, it was that simple, just had to take out the ->first() . Thanks a tone. Maybe reply as an answer? – Dustin Feb 11 '18 at 01:35

1 Answers1

0

You can customize the query logic for route model binding in the RouteServiceProvider:

public function boot()
{
    parent::boot();

    Route::bind('profile', function ($value) {
        return App\User::where('profile', $value)->first() ?? abort(404);
    });
}