试图解决在尝试向视图显示一些数据时遇到的错误。我正在使用v5.7,我感觉它可能与控制器中的index方法有关,这可能是非常错误的。如果需要更多信息,请告诉我。
试图获取非对象(0)的属性'slug'
路线:
Route::get('/category/{category}','BlogController@category')->name('category');
BlogCategory模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class BlogCategory extends Model
{
protected $fillable = ['title', 'slug'];
public function posts()
{
return $this->hasMany(Post::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
发布模型
public function category()
{
return $this->belongsTo(BlogCategory::class);
}
控制器:
protected $limit = 3;
public function index()
{
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = Post::with('author')
->latestFirst()
->published()
// ->filter(request()->only(['term','year','month']))
->simplePaginate($this->limit);
return view('pages.frontend.blog.index', compact('posts', 'categories'));
}
public function category(BlogCategory $category)
{
$categoryName = $category->title;
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = $category->posts()
->with('author')
->latestFirst()
->published()
->simplePaginate($this->limit);
return view("pages.frontend.blog.index", compact('posts', 'categories', 'categoryName'));
}
查看:
@foreach ($posts as $post)
<article class="post-item">
@if ($post->image_url)
<div class="post-item-image">
<a href="{{ route('blog.show', $post->slug) }}">
<img src="{{ $post->image_url }}" alt="">
</a>
</div>
@endif
<div class="post-item-body">
<div class="padding-10">
<h2>
<a href="{{ route('blog.show', $post->slug) }}">{{ $post->title }}</a>
</h2>
{!! $post->excerpt_html !!}
</div>
<div class="post-meta padding-10 clearfix">
<div class="pull-left">
<ul class="post-meta-group">
<li>
<i class="fa fa-user"></i>
<a href="#"> {{ $post->author->name }} </a>
</li>
<li>
<i class="fa fa-clock-o"></i>
<time> {{ $post->date }}</time>
</li>
<li>
<i class="fa fa-folder"></i>
<a href="{{ route('category', $post->category->slug) }}"> {{ $post->category->title }}</a>
</li>
<li>
<i class="fa fa-comments"></i>
<a href="#">4 Comments</a>
</li>
</ul>
</div>
<div class="pull-right">
<a href="{{ route('blog.show', $post->slug) }}">Continue Reading »</a>
</div>
</div>
</div>
</article>
@endforeach
答案 0 :(得分:1)
belongsTo
函数在posts表中接受第二个参数作为外键的名称,如果不提供该参数,框架将尝试猜测给定外键名称的外键列名称是什么。在您的情况下,category()
充当模式,因此框架正在搜索category_id
,但是,您的外键列名称为blog_category_id
。
public function category()
{
return $this->belongsTo(BlogCategory::class, 'blog_category_id');
}
答案 1 :(得分:0)
如果此视图与索引方法相关,则应在控制器的索引中调用类别关系!
public function index()
{
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = Post::with('author')
->category()
->latestFirst()
->published()
// ->filter(request()->only(['term','year','month']))
->simplePaginate($this->limit);
return view('pages.frontend.blog.index', compact('posts', 'categories'));
}