這是我的類別模型 namespace App; use Illuminate\Database\Eloquent\Model; class Category extends Model { public function posts() { return $this->belongsToMany('App\Post')->withTimestamps(); } }這是我的帖子模型 namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function categories() { return $this->belongsToMany('App\Category')->withTimestamps(); } public function tags() { return $this->belongsToMany('App\Tag')->withTimestamps(); } public function user() { return $this->belongsTo('App\User'); } }這是我的索引功能 public function index() { $posts=Post::latest()->get(); return view('admin.post.index',compact('posts')); }這是我的查看頁面 @foreach($posts as $post) <tr> <td>{{ $post->id }}</td> <td>{{ str_limit($post->title,'10') }}</td> <td>{{$post->categories->pivot->name}}</td> </tr> @endforeach當我想回顯我的類別名稱時,laravel 給了我這個錯誤。此集合實例上不存在 ErrorException (E_ERROR) 屬性 [pivot]。(查看:C:\xampp\htdocs\Advanced_Blog_System\resources\views\admin\post\index.blade.php)我只需要查看類別名稱我該如何解決這個問題???
- 2 回答
- 0 關注
- 125 瀏覽
添加回答
舉報
0/150
提交
取消