Laravel Eager Loading |查询特定用户

时间:2016-09-05 11:34:54

标签: php 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

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你应该这样做:

function getData(query, sheetName) {
  //jdbc:sqlserver://localhost;user=MyUserName;password=*****;
  var conn = Jdbc.getConnection("jdbc:sqlserver://URL;user=USERNAME;password=PASSWORD;databaseName=DBNAME");
  var stmt = conn.createStatement();
  stmt.setMaxRows(MAXROWS);
  var rs = stmt.executeQuery(query);
  Logger.log(rs);

  var doc = SpreadsheetApp.openById("SPREADSHEETID");
  var sheet = doc.getSheetByName(sheetName);
  var results = [];
  var cell = doc.getRange('a1');
  var row = 0;

  cols = rs.getMetaData();
  colNames = [];
  for (i = 1; i <= cols.getColumnCount(); i++ ) {
    Logger.log(cols.getColumnName(i));
    colNames.push(cols.getColumnName(i));
  }
  results.push(colNames);

  var rowCount = 1;
  while(rs.next()) {
    curRow = rs.getMetaData();
    rowData = [];
    for (i = 1; i <= curRow.getColumnCount(); i++ ) {
      rowData.push(rs.getString(i));
    }
    results.push(rowData);
    rowCount++;
  }
  sheet.getRange(1, 1, MAXROWS, cols.getColumnCount()).clearContent();
  sheet.getRange(1, 1, rowCount, cols.getColumnCount()).setValues(results);

  Logger.log(results);
  rs.close();
  stmt.close();
  conn.close();
}