我试图通过路线传递getMonth视图,但似乎正在工作,因为它在指定$ month时显示我的数据库的所有结果,而不是按月过滤结果。但是$ id确实有用,并且会转到匹配db中的$ id的单个帖子。
Route::get('blog', ['as' => 'blog', 'uses' => 'BlogController@BlogIndex']);
Route::get('blog/{Id}', ['as' => 'blog.id', 'uses' => 'BlogController@ShowbyId']);
Route::get('blog/month/{month}', ['as' => 'blog.month', 'uses' => 'BlogController@ShowbyMonth']);
class BlogController extends BaseController {
public function BlogIndex()
{
//get the posts from the database by asking the Active Record for "all"
$blogs = Blog::all();
$blogs = DB::table('blogs')->paginate(3);
// and create a view which we return - note dot syntax to go into folder
return View::make('pages.blog', array('blogs' => $blogs));
}
public function ShowbyId($Id)
{
$Ids = Blog::where('Id', '=', $Id)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('Ids', $Ids)
}
public function ShowbyMonth($month)
{
$months = Blog::where('month', '=', $month)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
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.slug', [$blog->slug]) }}">
</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>
@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>
</div>
@endif
@endforeach
答案 0 :(得分:0)
我不确定它是否解决了问题但是在刀片文件中你有:
@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>
</div>
@endif
应该是:
@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>
</div>
@endif
你应该关心变量案例。
答案 1 :(得分:0)
我看到的问题是你的URI /路由结构,你现在有这个:
Route::get('blog/{Id}', ['as' => 'blog.id', 'uses' => 'BlogController@ShowbyId']);
Route::get('blog/month/{month}', ['as' => 'blog.month', 'uses' => 'BlogController@ShowbyMonth']);
每当您尝试访问blog.month
路由blog.id
时,都会收到month
字样。将月份路线放在第一位可能会更好,以便在ID路线之前点击月份URI。
Route::get('blog/month/{month}', ['as' => 'blog.month', 'uses' => 'BlogController@ShowbyMonth']);
Route::get('blog/{id}', ['as' => 'blog.id', 'uses' => 'BlogController@ShowbyId']);
此外,要清理控制器,您可以考虑使用查询范围使您的逻辑更可重用且更易于调试。在你的模型中,你可以这样做:
class Blog extends Eloquent {
public function scopeMonth($query, $month) {
return $query->where('month', '=', $month);
}
}
然后在你的控制器中你可以这样做:
class BlogController extends BaseController {
//.. BlogIndex here
public function ShowbyId($Id)
{
$blog = Blog::find($id)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('Ids', $blog);
}
public function ShowbyMonth($month)
{
$months = Blog::month($month)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('months', $months)
}
}
希望这有帮助!