當我單擊我的 cms 中的帖子塊時,出現(xiàn)錯誤。這是 PostsController.php 的代碼<?phpnamespace App\Http\Controllers\Blog;use App\Http\Controllers\Controller;use App\Post;use Illuminate\Http\Request;use App\Tag;use App\Category;class PostsController extends Controller{ public function show(Post $post) { return view('blog.show')->with('post', $post); } public function category(Category $category) { return view('blog.category') ->with('category', $category) ->with('posts', $category->post()->searched()->simplePaginate(3)) ->with('categories', Category::all()) ->with('tags', Tag::all()); } public function tag(Tag $tag) { return view('blog.tag') ->with('tag', $tag) ->with('categories', Category::all()) ->with('tags', Tag::all()) ->with('posts', $tag->posts()->searched()->simplePaginate(3)); }}這是 Post.php 模型的代碼 <?phpnamespace App;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\SoftDeletes;use Illuminate\Support\Facades\Storage;use App\Category;class Post extends Model{ use SoftDeletes; protected $fillable = [ 'title', 'description', 'content', 'image', 'published_at', 'category_id', 'user_id', ];/** * Delete post image from storage * HHE * @return void */ public function deleteImage() { Storage::delete($this->image); } public function category() { return $this->belongsTo(Category::class); } public function tag() { return $this->belongsToMany(Tag::class); } /** * * @return bool */ public function hasTag($tagId) { return in_array($tagId, $this->tags->pluck('id')->toArray()); } public function user() { return $this->belongsTo(User::class); } public function scopeSearched($query) { $search = request()->query('search'); if (!$search) { return $query; } return $query->where('title', 'LIKE', "%{$search}%"); }}
2 回答

一只斗牛犬
TA貢獻1784條經(jīng)驗 獲得超2個贊
我看不到您定義的任何函數(shù)(方法)或?qū)傩?code>tags。在您的Post
模型中,您有 method tag
,但沒有tags
.
您應該定義新方法或重命名該方法。

蝴蝶刀刀
TA貢獻1801條經(jīng)驗 獲得超8個贊
我認為你正在嘗試獲取特定帖子的標簽。因此,由于在您的模型帖子中與標簽有關(guān)系這應該可以解決問題
<div class="gap-xy-2 mt-6">
@foreach($post->tags->first() as $tag)
<a class="badge badge-pill badge-secondary" href="{{ route('blog.tag', $tag->id) }}">
{{ $tag->name }}
</a>
@endforeach
</div>
- 2 回答
- 0 關(guān)注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消