Php 尝试获取laravel 5.8中非对象的属性
我想在post表中显示类别的名称,但这给了我一个错误我的代码检查这个 这是模态Php 尝试获取laravel 5.8中非对象的属性,php,laravel,laravel-5,Php,Laravel,Laravel 5,我想在post表中显示类别的名称,但这给了我一个错误我的代码检查这个 这是模态 <?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { protected $fillable=['seotitle', 'h1title', 'image' , 'alt', 'url
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
protected $fillable=['seotitle', 'h1title', 'image' , 'alt', 'url' , 'meta','author','status','keywords', 'tags', 'category_id', 'text','read', 'view'];
public function Category(){
return $this->belongsTo(Category::class);
}
}
刀片文件
<td>{{$post->Category->name}}</td>
{{$post->Category->name}
Category=这是关系中的模态函数名称
我如何解决这个问题
我想在post表中显示类别名称,但出现错误您能告诉我什么问题吗?laravel 5.8错误是因为一篇文章没有类别。你一定很有用
<td>{{$post->Category->name ?? ''}}</td>
{{$post->Category->name???'}
在您的控制器文件中
public function index()
{
$posts = Post::all();
return view('admin/posts/index',compact('posts'));
}
@if(!empty($posts))
@foreach($posts as $post)
<td>{{$post->Category->name ?? '' }}</td>
@endforeach
@endif
在刀片文件中使用
public function index()
{
$posts = Post::all();
return view('admin/posts/index',compact('posts'));
}
@if(!empty($posts))
@foreach($posts as $post)
<td>{{$post->Category->name ?? '' }}</td>
@endforeach
@endif
@if(!empty($posts))
@foreach($posts作为$post)
{{$post->Category->name???'}
@endforeach
@恩迪夫