好,所以我看到了类似的问题,但没有一个答案有帮助,因此,如果这看起来是重复的,请原谅我。
我正在学习有关Udemy的课程(已经过去了一个星期,尽管他们在该网站上很活跃,但是讲师没有回答我的问题,因此我为什么在这里),下面显示的代码是老师是如何教我们写的。
问题: 我们正在创建一个博客,该博客具有许多类别和许多帖子,但是每个帖子仅属于一个类别,可以容纳多个帖子。 这是他们的模型。
public function user(){
return $this->belongsTo('App\User');
}
public function posts(){
return $this->hasMany('App\Post');
}
POST模型
public function user(){
return $this->belongsTo('App\User');
}
public function category(){
return $this->belongsTo('App\Category');
}
他们每个人也都有自己的控制器,我们还有一个前端控制器,它显示所有类别,当您单击“阅读更多”时,将带您到一个页面,显示该类别中的最新帖子。 该单个页面还具有一个分页,该分页应显示属于该类别的先前帖子。 INSTEAD:将用户带到最近的帖子,无论它属于哪个类别,并显示所有类别的所有先前帖子。 下面是前端控制器
public function index()
{
return view('categories')
->with('categories',Category:orderBy('created_at', 'desc')->take(24)->get())
->with('tag', Tag::all())
->with('posttitle', Post::orderBy('created_at', 'desc')->first());
}
public function tag($id)
{
$tag = Tag::find($id);
return view('tag')->with('tag', $tag)
->with('categories', $tag->tag)
->with('categories', Category::all());
}
public function singlePage($slug)
{
$post = Post::where('slug', $slug)->first();
$next_id = Page::where('id', '>', $page->id)->min('id');
$prev_id = Post::where('id', '<', $post->id)->max('id');
return view('single')->with('post', $post)
->with('categories', Category::all())
->with('posttitle', Post::take(1)->get())
->with('next', Post::find($next_id))
->with('prev', Post::find($prev_id));
}
下面是索引函数的视图
<div class="container">
<div class="container-fluid">
<div class="row medium-padding120 bg-border-color">
<div class="container">
<div class="col-lg-12">
<div class="offers">
<div class="row">
<div class="col-lg-8 col-md-12 col-sm-12 col-xs-12">
<div class="heading">
<h4 class="h2 heading-title">Discover New Articles</h4>
<div class="heading-line">
<span class="short-line"></span>
<span class="long-line"></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="case-item-wrap">
@foreach($categories as $category)
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="case-item">
<div class="case-item__thumb">
<img src="{{ $category->featured_img }}" alt="{{ $category->title }}"style="width:300px; height:280px;">
</div>
<h6 class="case-item__title text-center"><a href="#">{!! $category->title !!}</a></h6>
<!--<i class="seoicon-clock"></i>
<time class="published" datetime="2016-04-17 12:00:00">
{{ $category->created_at->diffForHumans() }}
</time>-->
<h6 style="width:300px; height:85px;">{!! str_limit($category->summary, 85) !!}
<small> <a href="{{ route('post.single',$posttitle->slug) }}">Read More</a></small>
</h6>
</div>
</div>
@endforeach
</div>
</div>
<div class="padded-50"></div>
</div>
</div>
</div>
</div>
这是下面的单页视图
<div class="content-wrapper"> <div class="container"> <div>
<main class="main">
<div class="col-xs-10 col-xs-offset-1 col-sm-10 col-sm-offset-1 col-md-7 col-md-offset-1 col-lg-6 col-lg-offset-3 panel panel-reading">
<article class="hentry post post-standard-details">
<h2>{!! $post->posttitle !!}</h2>
<div class="post__content">
<div class="post-additional-info">
<div class="post__author author vcard">
By:
<div class="post__author-name fn">
<a href="#" class="post__author-link">{{ $post->user->username}}</a>
</div>
</div>
{{--Post date aka page date --}}
<span class="post__date">
<time class="published" datetime="2016-03-20 12:00:00">
<i class="seoicon-clock"></i> {{ $post->created_at->diffForHumans() }}
</time>
</span>
{{--End Post date aka page date--}}
</div>
{{--Post content aka page content--}}
<div class="post__content-info">
<p> {!! $post->content !!} </p>
</div>
{{--End post content aka page content--}}
</div>
<div class="pagination-arrow">
{{--Left arrow--}}
@if($prev)
<a href="{{ route('post.single', ['slug' => $prev->slug] )}}" class="btn-prev-wrap">
<svg class="btn-prev">
<use xlink:href="#arrow-left"></use>
</svg>
<div class="btn-content">
<div class="btn-content-title">Previous Page</div>
<p class="btn-content-subtitle">{{ $prev->posttitle}}</p>
</div>
</a>
@endif
{{--Right arrow--}}
@if($next)
<a href="{{ route('post.single', ['slug'=>$next->slug]) }}" class="btn-next-wrap">
<div class="btn-content">
<div class="btn-content-title">Next Page</div>
<p class="btn-content-subtitle">{{ $next->posttitle }}</p>
</div>
<svg class="btn-next">
<use xlink:href="#arrow-right"></use>
</svg>
</a>
@endif
</div>
</article>
</div>
</main>
这里是帖子数据库
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('category_id');
$table->string('posttitle');
$table->string('slug');
$table->text('content');
$table->softDeletes();
$table->timestamps();
});
}
结论: 使用此方法,如何过滤它,以便当用户单击该类别时仅显示具有相同category_id的帖子?
谢谢 作为奖励,我简短记录了问题https://www.screencast.com/t/vVzRMSunH
答案 0 :(得分:0)
对不起,我无法重复您的所有代码,但是如果我很了解您,那么您希望能够在一个页面上显示属于特定类别的所有帖子。如果是这样,则生成视图的控制器应该像这样。组织方面的问题,我希望有一个专门的负责人负责有关类别模型的所有操作。
/**
* Show all posts of category $id
*
* @param int $id : Id of category mapped via route
*/
public function show($id)
{
$category = Category::findOrFail($id);
$posts = $category->posts()->paginate();
return view('category.show', compact('posts'));
}
您的resources/views/category/show.blade.php
可能是这样
@extends('layouts.app')
@section('content')
@foreach($posts as $post)
<h2>{{ $post->title }}</h2>
{{-- and you can show more informations about post here --}}
@endforeach
{{-- pagination links --}}
{!! $posts->links() !!}
@stop