2016-09-05 42 views
1

我有一些麻煩顯示特定用戶所做的結果。Laravel渴望加載|查詢特定用戶

下面我收到的所有條目($ restaurantmenue)的代碼,而不是我只需要由特定用戶輸入的內容。我怎樣才能查詢創建條目的用戶?

//Restaurant Profile Page 
public function showrestaurants($id) { 
    $restaurant = User::findOrFail($id); 
    //$restaurant->addclick(); 

    $restaurantmenue = User::with(['articles' => function ($q){ 
    $q->nowpublished(); 
    }])->get(); 

    return view('pages.profile')->withRestaurant($restaurant)->withRestaurantmenue($restaurantmenue); 
} 

nowpublished()範圍在Article.php:

public function scopeNowPublished($query) { 
    $zero = Carbon::today()->addHours(23)->addMinutes(59)->addSeconds(59); 
    $query->whereBetween('published_at',[Carbon::today(),$zero])->orderBy('published_at','desc'); 
} 

筆者認爲:

@foreach($restaurantmenue as $daily) 
        @foreach($daily->articles as $menue) 
        <div class="card card-horizontal"> 
         <div class="row"> 
         <div class="col-md-5"> 
          <div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;"> 
          <img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;"> 
          <div class="filter filter-azure"> 
           <button type="button" class="btn btn-neutral btn-round"> 
            <i class="fa fa-heart"></i> SMÄCKT MIR 
           </button> 
          </div> 
          </div> 
         </div> 
         <div class="col-md-7"> 
          <div class="content"> 
           <p class="category text-info"> 
            <i class="fa fa-trophy"></i> Best of 
           </p> 

           <a class="card-link" href="#"> 
            <h4 class="title">{{$menue->title}} </h4> 
           </a> 
           <a class="card-link" href="#"> 
            <p class="description">{{$menue->body}}</p> 
            <br /> 
            {{$menue->price}} € 
           </a> 

          </div> 
         </div> 
         </div> 
        </div> 
        @endforeach 
       @endforeach 

回答

2

如果我理解正確的話,你應該做這樣的事情:

$restaurantmenue = User::where('id', $id)->with(['articles' => function ($q) { 
    $q->nowpublished(); 
}])->get(); 
+1

完美!這工作 – Mamulasa

+0

很高興幫助。 ) –