2016-04-15 86 views
1

Im在我的刀片文件中显示@foreach语句中的数据库结果时遇到问题。我根本没有得到任何错误。Laravel 5:@foreach未显示结果

控制器的文件:

public function index() 
    { 
     /** 
     * Retrieve all articles 
     * where status is published [1] 
     * and order them by published date decending 
     * 
     * @var articles 
     */ 

     $articles = Article::where(function($query) { 
      return $query 
       ->where('eHArt_Status', '1') 
       ->orderBy('published_at', 'desc') 
       ->paginate(10); 
     }); 

     // return articles feed 
     return view('build.4-.feeds.articles')->with('articles', $articles); 
    } 

我的DB型号:

class Article extends Authenticatable 
{ 
    /** 
    * The database table used by the model 
    * 
    * @var string 
    */ 
    protected $table = 'eHArticle'; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     // fillable article fields 
     'eHArt_OldArtID', 
     'eHArt_Title', 
     'eHArt_Content', 
     'eHArt_Author', 
     'eHArt_Status', 
     'eHArt_Img', 
     'published_at' 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     // no hidden fields 
    ]; 
} 

我的视图文件: “”

@section('feed') 
    <!-- check if there is articles --> 
    @if (!$articles->count()) 
     <p>No articles</p> 
    @else 
     @foreach ($articles as $article) 
      <p>Article</p> 
     @endforeach 
    @endif 
@endsection 

如果我走远离$articles->count()我可以看到字符串“没有文章”,但我的foreach没有显示任何内容。

做一个DD($文章)

Builder {#159 ▼ 
    #query: Builder {#158 ▼ 
    #connection: MySqlConnection {#154 ▶} 
    #grammar: MySqlGrammar {#155 ▶} 
    #processor: MySqlProcessor {#156} 
    #bindings: array:6 [▶] 
    +aggregate: null 
    +columns: null 
    +distinct: false 
    +from: "eHArticle" 
    +joins: null 
    +wheres: array:1 [▶] 
    +groups: null 
    +havings: null 
    +orders: null 
    +limit: null 
    +offset: null 
    +unions: null 
    +unionLimit: null 
    +unionOffset: null 
    +unionOrders: null 
    +lock: null 
    #backups: [] 
    #bindingBackups: [] 
    #operators: array:26 [▶] 
    #useWritePdo: false 
    } 
    #model: Article {#152 ▼ 
    #table: "eHArticle" 
    #fillable: array:7 [▶] 
    #hidden: [] 
    #connection: null 
    #primaryKey: "id" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: [] 
    #original: [] 
    #relations: [] 
    #visible: [] 
    #appends: [] 
    #guarded: array:1 [▶] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    #morphClass: null 
    +exists: false 
    +wasRecentlyCreated: false 
    } 
    #eagerLoad: [] 
    #macros: [] 
    #onDelete: null 
    #passthru: array:11 [▶] 
    #scopes: [] 
} 
+0

你可以做DD内($ articles)在索引方法内? – Digitlimit

回答

1

使用get()方法:

此外,您应该使用此查询得到正确的结果。

$articles = Article::where(function($query) { 
      return $query 
       ->where('eHArt_Status', '1') 
       ->orderBy('published_at', 'desc') 
       ->paginate(10); 
     })->get(); 

否则你应该查询更改为

$articles = Article::where('eHArt_Status', '1')->orderBy('published_at', 'desc')->paginate(10); 

看到这个https://laravel.com/docs/5.1/pagination#paginating-eloquent-resultshttps://laravel.com/docs/5.1/pagination#displaying-results-in-a-view

您需要回声它<p>标签{{$article}}

@section('feed') 
    @if(count($articles)) 
     @foreach ($articles as $article) 
      <h1>{{$article->eHArt_Title}}</h1> 
      <p>{{$article->eHArt_Content}}</p> 
     @endforeach 
    @else 
     <p>No articles</p> 
    @endif 
@endsection 
{!! $articles->render() !!} 
+1

谢谢。这工作。 –

+0

这真的是一个糟糕的建议。首先,查询太冗长了:'Article :: where('eHArt_Status','1') - > orderBy('published_at','desc') - > paginate(10);'是正确的。另外,使用'count()'而不进行比较(例如> 0)是非常糟糕的做法。我为将来会阅读这个的人写了这篇文章,所以他们可以做正确的事情。 –

0

使用if(count($articles) > 0)

@section('feed') 
    <!-- check if there is articles --> 
    @if(count($articles) > 0) 
     @foreach ($articles as $article) 
      <p>Article</p> 
     @endforeach 
    @else 
     <p>No articles</p> 
    @endif 
@endsection 

您可以在the documentation阅读更多关于count()方法。如果您正在使用生成器查询后

Article::where('eHArt_Status', '1')->orderBy('published_at', 'desc')->paginate(10); 
+0

还是什么都没有。它只是在刷新页面时显示一个空白页面。然而,如果我将if语句更改为@if(!count($ articles)),它将显示文本“No articles” –

+0

请参阅我的答案.. @WesMurray它会工作..非常确定 – aimme

+0

您是否使用了@if(count ($ articles))'或'if(count($ articles)> 0)'? –