猿问

Laravel-5.8:试图获取非对象的属性“display_name”

当我尝试评估来自类别模型的数据时,我在 Laravel 中收到“试图获取非对象的属性‘display_name’”错误。


我有 2 个模型:Post 和 Category;我有相应的表格:

Posts('id', 'title', 'body', 'cover_image')

Categories('id', 'display_name','name', 'key).


数据透视表是:category_post


这是我的帖子模型:


namespace App;


use Illuminate\Database\Eloquent\Model;


class Post extends Model

{

    //Table Name

    protected $table = 'posts';



    //Model Relationships


    public function tag(){

        return $this->belongsToMany('App\Tag', 'id');

    }


    public function category() {

        return $this->belongsTo('App\Category','id');

    }

}


这是我的类别模型:


namespace App;


use Illuminate\Database\Eloquent\Model;


class Category extends Model

{

    protected $table = 'categories';


//Model Relationships


    public function post() 

    {

        return $this->belongsTo('App\Post', 'id');

    }


}


另外,这是我的 PagesController:


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Post;


class PagesController extends Controller

{

    public function index()

    {

        if ( !is_null(Post::class) ) {    

            $posts = Post::latest()->with('category')->paginate(5);


            return view('pages.index')->with(['posts' => $posts]);

        }

        else {

            return view('pages.empty');

        }

    }

这是我的刀片文件:


@if(count($posts) > 0)

   @foreach($posts as $post)

      <span> <i class="fa fa-folder-open"></i> {{ $post->category->display_name }} </span>

   @endforeach

@endif


杨__羊羊
浏览 134回答 2
微课
2回答

有只小跳蛙

问题是你没有在你的帖子表中保存CATEGORY_ID,保存好类别 ID 并尝试这样:更新 POST 模型:namespace App;use Illuminate\Database\Eloquent\Model;class Post extends Model{&nbsp; &nbsp; //Table Name&nbsp; &nbsp; protected $table = 'posts';&nbsp; &nbsp; // Setup fields of table "posts"&nbsp; &nbsp; protected $fillable = ['id', 'title', 'body', 'cover_image','category_id'];&nbsp; &nbsp; //Model Relationships&nbsp; &nbsp; public function tag(){&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany('App\Tag', 'id');&nbsp; &nbsp; }&nbsp; &nbsp; // Relation category to foreign key&nbsp;&nbsp; &nbsp; public function category() {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsTo('App\Category','category_id');&nbsp; &nbsp; }}类别模型namespace App;use Illuminate\Database\Eloquent\Model;class Category extends Model{&nbsp; &nbsp; protected $table = 'categories';&nbsp; &nbsp; // Setup fields of table "category"&nbsp; &nbsp; protected $fillable = ['id', 'display_name', 'name', 'key'];&nbsp; &nbsp; //Model Relationships&nbsp; &nbsp; public function posts()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasMany('App\Post');&nbsp; &nbsp; }}看法@if(count($posts) > 0)&nbsp; &nbsp;@foreach($posts as $post)&nbsp; &nbsp; &nbsp; <span> <i class="fa fa-folder-open"></i> {{ $post->category->display_name }} </span>&nbsp; &nbsp;@endforeach@endif移民帖 - 外键 category_idSchema::create('posts', function (Blueprint $table) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Your other fields.....&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You must setup category like this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->integer('category_id')->nullable()->unsigned();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');&nbsp; &nbsp; &nbsp; &nbsp; });控制器namespace App\Http\Controllers;use Illuminate\Http\Request;use App\Post;class PagesController extends Controller{&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $posts = Post::all();&nbsp; &nbsp; &nbsp; &nbsp; if (!empty($posts)) {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Please let me know this test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dd($posts[0]->category->display_name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return view('pages.index', ['posts' => $posts]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return view('pages.empty');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

鸿蒙传说

我认为您post()在Category模型中的方法有误。尝试像这样修改这个方法:类别型号:public&nbsp;function&nbsp;post()&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$this->hasMany('App\Post',&nbsp;'id');}来源:Eloquent-relationship 一对多
随时随地看视频慕课网APP
我要回答