2016-08-18 88 views
0

我希望登錄用戶可以轉到其他用戶的個人資料頁面,其中顯示他的所有帖子和其他內容。導航到其他用戶的個人資料(laravel 5.2)

是我的嘗試下面給出:

我的路線:

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

     'uses' => '[email protected]' , 

     'as' => 'myplace' , 

     'middleware' => 'auth:web' 

]); 

PostController中:

public function getmyplace($username) 
{ 
    $user = User::where('username', $username)->first(); 
    if(!$user) 
     abort(404); 

    $posts=Post::where(function($query){ 
     $query->where('user_id',Auth::user()->id) 
      ->orWhereIn('user_id',Auth::user()->friends()->lists('id') 
       ); 
    }) 
    ->orderBy('created_at','desc')->get(); 

    $accounts=Account::orderBy('updated_at','desc')->limit(1)->get(); 

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

我的觀點:

> @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> 

我通過用戶名參數,但那沒有沒有給出所需的輸出。

任何我需要提供的只是告訴我。

+0

你得到了什麼錯誤?你想得到什麼,你得到了什麼? –

+0

@AmirBar我希望當我轉到某人的個人資料時,顯示該用戶的內容(這裏是帖子)。我得到的是 - >登錄用戶 –

回答

1

你的問題是在這裏

$query->where('user_id',Auth::user()->id) 
     ->orWhereIn('user_id',Auth::user()->friends()->lists('id') 
      ); 

您的查詢的身份驗證選擇數據::用戶()而不是$用戶

$posts=Post::where(function($query) use ($user) { 
    $query->where('user_id',$user->id) 
     ->orWhereIn('user_id',$user->friends()->lists('id') 
      ); 
}) 
->orderBy('created_at','desc')->get(); 

編輯添加使用($用戶)時聲明匿名函數

如果您已經建立了雄辯的關係,您也可以刪除該查詢離子正確。

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

EDIT 2刪除

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

您選擇的職位(在你的控制器)DONT belongt給你。所以不要在視圖中檢查它們屬於你。

編輯3

詳細看看納入討論。目前的狀態實際上是在posts表中需要另一個用戶的引用。該參考文獻定義了關於作者所發佈的帖子的「個人資料」。因此可以將其稱爲target_id並在用戶表上引用id。

在接下來的步驟中,必須改變路線,以便傳遞用戶ID(也可以在課程的請求體中傳遞),例如,

/user/{id}/profile 

然後控制器需要傳遞的$ id參數爲target_id和當前用戶(AUTH ::用戶())作爲USER_ID(帖子的作者)

最後,查詢可適應實際選擇自己的用戶的每一個崗位+每一個崗位,他是目標

$posts=Post::where(function($query) use ($user) { 
    $query->where('user_id',$user->id) 
     ->orWhere('target_id',$user->id); 
}) 
->orderBy('created_at','desc')->get(); 

Ofcourse在$用戶需要在不同requets和路由和內部重定向需要與問候的變化而適於選擇。

+0

的內容現在出現錯誤:未定義的變量:用戶 –

+0

哦,這是由於匿名函數只是添加「使用($用戶)」的功能,我會更新我的答案在第二個|現在添加 –

+0

sry,但這並沒有解決我的問題..... –

相關問題