【发布时间】:2018-08-26 20:55:16
【问题描述】:
抱歉,这里有什么问题?我无法查看我发布的某些帖子,但我可以查看其他帖子。我认为一切都已更正,所以请任何可以提供帮助的人!
我的意思是,我之前在数据库中创建了一些帖子,我可以很好地查看它们.. 但后来我遇到了问题.. 当我创建新帖子或更新现有记录(帖子)时,它未能加载视图并抛出上述错误。
控制器:PostController.php
<?php
namespace App\Http\Controllers\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\user\Post;
use App\Model\user\Tag;
use App\Model\user\Category;
class PostController extends Controller
{
public function post(Request $request, Post $slug)
{
$tags = Tag::all();
$posts = Post::all();
$categories = Category::all();
$nextPost = Post::where('id', Post::where('id', '>', $slug->id)->min('id'))->firstOrFail();
$previousPost = Post::where('id', Post::where('id', '<', $slug->id)->max('id'))->firstOrFail();
$relatedPosts = Post::where('category_id', $slug->category_id)->simplePaginate(3);
$latestNews = Post::orderBy('id', 'DESC')->get();
return view('posts.show', compact('tags', 'posts', 'categories', 'latestNews', 'previousPost', 'nextPost', 'relatedPosts'))->with('post', $slug);
}
}
查看:post.blade.php
@extends('layouts.app')
@section('content')
<section id="contentSection">
<div class="row">
<div class="col-lg-8 col-md-8 col-sm-8">
<div class="left_content">
<div class="single_page">
<ol class="breadcrumb">
<li><a href="/">Home</a></li>
<li>
<a href="category/{{ $post->category->slug }}">
{{ $post->category->name }}
</a>
</li>
<li class="active">
{{ $post->title }}
</li>
</ol>
<h1>{{ $post->title }}</h1>
<div class="post_commentbox">
<a href="#"><i class="fa fa-user">
</i>Carlton Ballet
</a>
<span>
<i class="fa fa-calendar"></i>{{ $post->created_at->diffForHumans()}}
</span>
<span class="fa fa-tags"></span>
<a href="category/{{ $post->category->slug }}" style="border:1px solid #798992;border-radius:5px;padding:3px;">{{ $post->category->name }}</a>
</div>
<div class="single_page_content"> <img class="img-center" src="../images/single_post_img.jpg" alt="">
{!! htmlspecialchars_decode($post->body) !!}
<h3>Tag Clouds</h3>
<hr/>
@foreach($post->tags as $tag)
<small class="pull-right" style="margin-right:5px;border-radius:5px;border:1px solid #9BCB3B;padding:5px;">
{{ $tag->name }}
</small>
@endforeach
</div>
<div class="social_link">
<ul class="sociallink_nav">
<li><a href="#"><i class="fa fa-facebook"></i></a></li>
<li><a href="#"><i class="fa fa-twitter"></i></a></li>
<li><a href="#"><i class="fa fa-google-plus"></i></a></li>
<li><a href="#"><i class="fa fa-linkedin"></i></a></li>
<li><a href="#"><i class="fa fa-pinterest"></i></a></li>
</ul>
</div>
<div class="related_post">
<h2>Related Post <i class="glyphicon glyphicon-stats"></i> {{ count($relatedPosts) - 1 }}</h2>
<ul class="wow fadeInDown animated">
<li style="float:left;">
@foreach($relatedPosts as $RelatedPost)
@if(count($relatedPosts) > 0 && ($post->slug !== $RelatedPost->slug))
<div class="media"> <a class="media-left" href="/post" > <img src="../images/post_img1.jpg" alt=""> </a>
<div class="media-body">
<a class="catg_title" href="/post/{{$RelatedPost->slug}}"> {{$RelatedPost->title}}</a>
</div>
</div>
@endif
@endforeach
</li>
</ul>
</div>
{{ $relatedPosts->links() }}
</div>
</div>
</div>
<nav class="nav-slit">
<a class="prev" href="/post/{{ $previousPost->slug }}"> <span class="icon-wrap"><i class="fa fa-angle-left"></i></span>
<div>
<h3>City Lights</h3>
<img src="../images/post_img1.jpg" alt=""/>
</div>
</a>
<a class="next" href="/post/{{ $nextPost->slug }}"> <span class="icon-wrap"><i class="fa fa-angle-right"></i></span>
<div>
<h3>Street Hills</h3>
<img src="../images/post_img1.jpg" alt=""/>
</div>
</a>
</nav>
@include('partials._popular')
</div>
</section>
@endsection
包括:_popular.blade.php
<div class="col-lg-4 col-md-4 col-sm-4">
<aside class="right_content">
<div class="single_sidebar">
<h2><span>Popular Post</span></h2>
<ul class="spost_nav">
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 1</a> </div>
</div>
</li>
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 2</a> </div>
</div>
</li>
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 3</a> </div>
</div>
</li>
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 4</a> </div>
</div>
</li>
</ul>
</div>
<div class="single_sidebar">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#category" aria-controls="home" role="tab" data-toggle="tab">Categories</a></li>
<li role="presentation"><a href="#comments" aria-controls="messages" role="tab" data-toggle="tab">Hit Songs</a></li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="category">
<ul>
@foreach($categories as $category)
<li class="cat-item"><a href="/post/category/{{ $category->slug }}">
{{ $category->name }}</a>
</li>
@endforeach
</ul>
</div>
<div role="tabpanel" class="tab-pane" id="comments">
<ul class="spost_nav">
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 1</a> </div>
</div>
</li>
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 2</a> </div>
</div>
</li>
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 3</a> </div>
</div>
</li>
<li>
<div class="media wow fadeInDown"> <a href="/post" class="media-left"> <img src="{{asset('images/post_img2.jpg')}}" alt=""> </a>
<div class="media-body"> <a href="/post" class="catg_title"> This is the post body paragraph. How do you tell about it? Is it cool? 4</a> </div>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="single_sidebar wow fadeInDown">
<h2><span>Sponsor Advertisement</span></h2>
<!--a class="sideAdd" href="#"><img src="{{asset('images/add_img.jpg')}}" alt=""a-->
<div class="adplugg-tag"></div>
</div>
<div class="single_sidebar wow fadeInDown">
<!-- Enter something here-->
</div>
</aside>
</div>
【问题讨论】:
-
你能验证你的模型是否有受保护的 $fillable =...;必须填写类别的所有字段。
-
这样?? belongsToMany('App\Model\user\Tag', 'post_tags')->withTimeStamps(); } 公共函数 category() { return $this->belongsTo('App\Model\user\Category'); } 公共函数 getRouteKeyName() { return 'slug'; } }
-
是的。它工作知道吗?检查缓存清除
-
哦我刚发现这段代码[ $nextPost = Post::where('id', Post::where('id', '>', $slug->id)- >min('id'))->firstOrFail(); $previousPost = Post::where('id', Post::where('id', 'id)->max('id'))->firstOrFail();] 什么时候带来问题我想显示最后发布的帖子或第一个发布的帖子。但是,如何相应地更正代码呢?