doulong6761 2014-10-19 20:36
浏览 15

Laravel数据库内容未显示

I have 2 variables pulled from my DB $month and $id. When i do $if(isset($slug)) if queries the db and displays the results. When i do the same for $month it doesn't output anything but will load the page. I can't see anything wrong with the code as it all matches??

routes.php

Route::get('blog/{id}', ['as' => 'blog.id', 'uses' => 'BlogController@ShowbyId']); 
Route::get('blog/{month}', ['as' => 'blog.month', 'uses' => 'BlogController@ShowbyMonth']); 

<?php class BlogController extends BaseController {

public function ShowAll() {
$blogs = Blog::all();
$blogs = DB::table('blogs')->paginate(3);
return View::make('pages.blog')
           ->with('blogs', $blogs);
}
public function ShowById($id) {
$ids = Blog::where('id', $id)
             ->get();
return View::make('pages.blog')
           ->with('ids', $ids);
}
public function ShowByMonth($month) {
$months = Blog::where('month', $month);
$months = DB::table('blogs')->paginate(3);
return View::make('pages.blog')
           ->with('months', $months);

}
} 

blog.blade.php

@if(isset($blogs)) 
@foreach ($blogs as $blog)
    <div class="blog-outer-wrap">
    <img src="images/blog/{{ $blog->img}}"> 
     <div class="blog-header">{{ $blog->header }}</div>
    <div class="blog-text">{{ $blog->content }}</div>
    <a href="{{ URL::route('blog.id', [$blog->id]) }}"><div class="blog-readmore">Read More</div>    
    </div>
@endforeach
@elseif(isset($ids)) 
@foreach ($ids as $id)
    <div class="blog-outer-wrap">
    <img src="../images/blog/{{ $id->img}}"> 
    <div class="blog-header">{{ $id->header }}</div>
    <div class="blog-text">{{ $id->content }}</div>
    </div>
@endforeach
@elseif(isset($months)) 
@foreach ($months as $month)
    <div class="blog-outer-wrap">
    <img src="images/blog/{{ $month->img}}"> 
    <div class="blog-header">{{ $month->header }}</div>
    <div class="blog-text">{{ $month->content }}</div>
    <a href="{{ URL::route('blog.month', [$month->slug]) }}"><div class="blog-readmore">Read 
    </div>
@endforeach
@endif
  • 写回答

1条回答 默认 最新

  • douguiyan9164 2014-10-19 21:25
    关注

    Since I can't comment I'll post as an answer.

    Your two routes:

    Route::get('blog/{id}', . . . );
    Route::get('blog/{month}', . . . );
    

    Are the same, any uri blog/whatever will hit the first Route no matter what, the second one will never get hit because the first wildcard {id} will take any argument. So you are always hitting BlogController@ShowById

    So i'm guessing your passing in month to try and hit the ShowByMonth controller. Instead it's routing you to:

    public function ShowById($id) {
        $ids = Blog::where('id', $id)
             ->get();
        return View::make('pages.blog')
             ->with('ids', $ids);
    }
    

    Where your query becomes Blog::where('id', 'January'); or whatever month you put into your uri.

    Fix:

    Add constraints

    Route::get('blog/{id}', [
        'as' => 'blog.id',
        'uses' => 'BlogController@ShowbyId']
    )->where('id', '[0-9]+'); // Only if {id} is any integer
    
    
    Route::get('blog/{month}', [
        'as' => 'blog.month',
        'uses' => 'BlogController@ShowbyMonth']
    )->where('month', '[A-Za-z]+'); // Only if {month} is any alpha character
    
    评论

报告相同问题?