2017-07-11 100 views
-1

所以我是一個編碼訓練營的學生,剛發佈了一個問題,但意識到我給了錯誤代碼片段,並決定刪除它並在此處重新發布。
我正在製作一個crossfit web應用程序,並且在通過創建將用戶/配置文件關聯在一起的模式循環時遇到問題。
這裏是我的資源SearchController:PHP laravel控制器

public function index(Request $request) 
{ 
    // 
    //dump($request->user());die(); 
     return view('user.search') 
      ->withUsers(User::all()) 
      ->withProfile(Profile::where('user_id', $request->user()->id)); 
} 

,這裏是我的blade.php文件:

@foreach($users as $user) 
    <li class = "list-group-item"> 
     {{$user->id}}: 
     <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal{{$user->id}}"> 
      {{$user->name}} 
     </button> 
     <!-- Modal --> 
     <div class="modal fade" id="myModal{{$user->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
      <div class="modal-dialog" role="document"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <h4 class="modal-title" id="myModalLabel"></h4> 
       </div> 
       <div class="modal-body"> 
       <ul> 
        <p>{{$profile->name}}</p> 
        <img src = "{{$profile->img_link}}" alt = "profile_pic" height="200" width="200"> 
        <li><p><strong>Location: </strong>{{$profile->location}}</p></li> 
        <li><p><strong>Age: </strong>{{$profile->age}}</p></li> 
        <li><p><strong>Height: </strong>{{$profile->height}}</p></li> 
        <li><p><strong>Affiliate: </strong>{{$profile->affiliate}}</p></li> 
        <li><p><strong>Front Squat: </strong>{{$profile->front_squat}} lbs</p></li> 
        <li><p><strong>Back Squat: </strong>{{$profile->back_squat}} lbs</p></li> 
        <li><p><strong>Clean and Jerk: </strong>{{$profile->clean_and_jerk}} lbs</p></li> 
        <li><p><strong>Snatch: </strong>{{$profile->snatch}} lbs</p></li> 
        <li><p><strong>Deadlift: </strong>{{$profile->deadlift}} lbs</p></li> 
        <li><p><strong>Bio: </strong>{{$profile->bio}}</p></li> 
       </ul> 
       </div> 
       <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="button" class="btn btn-default"><a href="/message">Message</a></button> 
       </div> 
      </div> 
      </div> 
     </div> 
    </li> 
@endforeach 

所以,當你點擊一個按鈕,模式只帶來了從最後一個配置文件中的信息已經進入我的MySQL數據庫。任何幫助將不勝感激。

+1

你的模式是使用'$ profile'而不是'$ user',它總是鏈接到當前用戶? –

+0

不要刪除舊帖子並重新發布。 –

+0

Mangus我發佈了ProfileController部分,而不是SearchController,並打算編輯,但點擊刪除 –

回答

0

如果你只是想獲取所有用戶的配置文件,你可以做這樣的:

SearchController.php:

public function index(Request $request) 
{ 
     return view('user.search') 
      ->withUsers(User::all()); 
} 

而在你blade.php文件你可以做到這一點:

@foreach($users as $user) 
      <li class = "list-group-item"> 
       {{$user->id}}: 
       <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal{{$user->id}}"> 
        {{$user->name}} 
       </button> 
       <!-- Modal --> 
       <div class="modal fade" id="myModal{{$user->id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
        <div class="modal-dialog" role="document"> 
        <div class="modal-content"> 
         <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
         <h4 class="modal-title" id="myModalLabel"></h4> 
         </div> 
         <div class="modal-body"> 
         <ul> 
          <p>{{$user->profile->name}}</p> 
          <img src = "{{$user->profile->img_link}}" alt = "profile_pic" height="200" width="200"> 
          <li><p><strong>Location: </strong>{{$user->profile->location}}</p></li> 
          <li><p><strong>Age: </strong>{{$user->profile->age}}</p></li> 
          <li><p><strong>Height: </strong>{{$user->profile->height}}</p></li> 
          <li><p><strong>Affiliate: </strong>{{$user->profile->affiliate}}</p></li> 
          <li><p><strong>Front Squat: </strong>{{$user->profile->front_squat}} lbs</p></li> 
          <li><p><strong>Back Squat: </strong>{{$user->profile->back_squat}} lbs</p></li> 
          <li><p><strong>Clean and Jerk: </strong>{{$user->profile->clean_and_jerk}} lbs</p></li> 
          <li><p><strong>Snatch: </strong>{{$user->profile->snatch}} lbs</p></li> 
          <li><p><strong>Deadlift: </strong>{{$user->profile->deadlift}} lbs</p></li> 
          <li><p><strong>Bio: </strong>{{$user->profile->bio}}</p></li> 
         </ul> 
         </div> 
         <div class="modal-footer"> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
         <button type="button" class="btn btn-default"><a href="/message">Message</a></button> 
         </div> 
        </div> 
        </div> 
       </div> 
      </li> 
     @endforeach 

現在你會顯示所有用戶,併爲每個你將展示他們的個人資料,以及這些用戶的,只是確保你在你的user.php的文件中有這樣的:

public function profile() 
{ 
    return $this->hasOne('App\Profile'); 
} 

希望這是你要找的,如果不是,那麼告訴我什麼是失蹤,我會盡我所能幫助,歡呼!