YaSh Chaudhary YaSh Chaudhary - 5 months ago 13
PHP Question

navigating to other user's profile (laravel 5.2)

I want that a login user can goto other user's profile pages where all his posts and other stuff is displayed.

what i tried is given below:

my route:

Route::get('/myplace/{username}' , [

'uses' => 'PostController@getmyplace' ,

'as' => 'myplace' ,

'middleware' => 'auth:web'



public function getmyplace($username)
$user = User::where('username', $username)->first();



return view('myplace',['user'=>$user,'posts'=>$posts,'accounts'=>$accounts]);

my view:

> @if (!$posts->count())
> <p>{{$user->getNameOrUsername() }} hasn't posted anything, yet.</p>
> @else
> @foreach ($posts as $post)
> @if(Auth::user()== $post->user)
> <div class="panel panel-default">
> <div class="panel-heading">
> <div class="row">
> <section class="col-md-2 col-xs-2"> <img id="imagesize2" src="images/g.jpg" class="img-circle" data-action="zoom"/></img>
> </section> <section class="col-md-5 col-xs-offset-1 col-xs-5">
> <a id="alink13" href=""><h5
> id="alink14">{{$post->user->getNameOrUsername() }}</h5></a>
> <p> on {{$post->created_at}} </p> </section>
> </div>
> <div class="panel-content">
> <div class="row" id="fetch">
> <section class="col-md-12" data-postid="{{ $post->id }}">
> <p id="post">{{$post->body}}</p>
> <div class="interaction" >
> @if(Auth::user()==$post->user)
> <a href="" class="edit">Edit</a>
> <a id="remove2" href="{{route('post.delete',['post_id' => $post->id])}}">Delete</a>
> @endif
> </div>
> </section>
> </div>
> </div>

I passed the username parameter but that didn't gave the desired output.

Anything i need to provide just tell me.


Your problem is here


Your query is selecting data for Auth::user() instead of $user

$posts=Post::where(function($query) use ($user) {

EDIT add use ($user) when declaring the anonymous function

Also you could remove the query if you have setup eloquent relations correctly.

$posts = $user->posts; // example

EDIT 2 remove

@if(Auth::user()== $post->user)

You are selecting posts (in your controller) that DONT belongt to you. So don't check they belong to you in the view.


For details have a look into the discussion. The current state is that actually another reference to a user is needed in the posts table. This reference defines on whoms "profile" the post as made on. Therefore it could be called target_id and references id on users table.

In a next step the route has to be changed so that a user id is passed (could also be passed in the request body ofcourse) e.g.


The controller then takes the passed $id parameter as target_id and the current user (Auth::user()) as user_id (creator of the post)

Finally the query can be adapted to actually select every post of the user himself + every post where he is the target

$posts=Post::where(function($query) use ($user) {

Ofcourse the $user needs to be selected in the different requets and routing and internal redirects need to be adapted with regards to the changes.