当我单击我的 cms 中的帖子块时,出现错误。这是 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贡献1801条经验 获得超8个赞
我认为你正在尝试获取特定帖子的标签。因此,由于在您的模型帖子中与标签有关系这应该可以解决问题
<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 关注
- 136 浏览
添加回答
举报
0/150
提交
取消