如何为帖子创建类别标签?我正在使用 laravel 8
Posted
技术标签:
【中文标题】如何为帖子创建类别标签?我正在使用 laravel 8【英文标题】:How to create category tags for posts? I am using laravel 8 【发布时间】:2021-06-19 23:20:50 【问题描述】:我试图允许用户在表单中创建帖子时创建类别标签并将其添加到帖子中。
我还希望这些标签出现在个人资料视图中,并充当过滤器按钮,根据它们拥有的标签名称显示帖子。
但是,在我尝试实现这个整体结果时,我被卡住了,因为每次我提交带有标签的帖子时,标签数组都会在视图中显示为空。
我的帖子表是:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
/**
* Run the migrations.
*
* @return void
*/
public function up()
Schema::create('posts', function (Blueprint $table)
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('url');
$table->string('image');
$table->text('tags');
$table->timestamps();
$table->index('user_id');
);
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
Schema::dropIfExists('posts');
我的帖子模型是:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Post extends Model
use \Conner\Tagging\Taggable;
use HasFactory;
protected $fillable = ['caption','url','image', 'tags'];
public function user()
return $this->belongsTo(User::class);
protected $dates = [
'created_at',
'updated_at',
];
我在 PostsController 中的创建和存储方法是:
public function create()
return view('posts.create');
public function store(Request $request)
$data = request()->validate([
'caption' => 'required',
'url' => 'required',
'image' => ['required', 'image'],
'tags' => 'required',
]);
$tags = explode(", ", $request->tags);
$imagePath = request('image')->store('uploads', 'public');
auth()->user()->posts()->create([
'caption' => $data['caption'],
'url' => $data['url'],
'image' => $imagePath,
'tags' => $data['tags'],
]);
return redirect('/users/' . auth()->user()->id);
我创建帖子的表单是:
<form action="/posts" enctype="multipart/form-data" method="post">
@csrf
<div class="form-group">
<label for="caption" class="create_caption_label">Post Caption</label>
<div class="create_caption_div">
<input id="caption"
type="text"
class="form-control @error('caption') is-invalid @enderror"
name="caption"
value=" old('caption') ?? '' "
autocomplete="caption" autofocus>
@error('caption')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong> $message </strong>
</span>
</div>
@enderror
</div>
</div>
<div class="form-group">
<label for="tags" class="create_tags_label">Tags</label>
<div class="create_tags_div">
<input id="tags"
type="text"
data-role="tagsinput"
class="form-control @error('tags') is-invalid @enderror"
name="tags"
value=" old('tags') ?? '' "
autocomplete="tags" autofocus>
@error('tags')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong> $message </strong>
</span>
</div>
@enderror
</div>
</div>
<div class="form-group">
<label for="url" class="edit_title_label">URL</label>
<div class="edit_url_div">
<input id="url"
type="text"
class="form-control @error('url') is-invalid @enderror"
name="url"
value=" '' "
autocomplete="url" autofocus>
@error('url')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong> $message </strong>
</span>
</div>
@enderror
</div>
</div>
<div class="create_post_image_div">
<label for="image" class="create_image_label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
@error('image')
<div class="invalid-feedback-div">
<strong> $message </strong>
</div>
@enderror
<div class="create_post_btn_div">
<button class="create_post_btn">Save Post</button>
</div>
</div>
</form>
最后,我的看法是:(这是提交帖子后标签数组显示为空的地方)
@foreach( $user->posts as $post )
<div class="carousel_posts_container">
<div class="post_date_and_edit_div">
<div class="posted_date_div">
<p class="posted_date">posted: $post->created_at->diffForHumans() </p>
</div>
<div class="post_edit_div">
<form action="/posts/$post->id/edit">
<input class="post_edit_btn" type="submit" value="• • •">
</form>
</div>
</div>
<div class="post_counter_div">
<p class="post_counter">1 like</p>
</div>
<div class="post_counter_div">
<p class="post_counter">1 comment</p>
</div>
<div class="carousel_post_img_div">
<img src="/storage/ $post->image " class="carousel_img_placeholder">
</div>
<div class="like_comment_view_container">
<div class="view_btn_div">
<form action="$post->url">
<input class="like_comment_view_btns" type="submit" value="( View Post )">
</form>
</div>
<div class="like_btn_div">
<button type="button" class="like_comment_view_btns">( Like )</button>
</div>
<div class="comment_btn_div">
<button type="button" class="like_comment_view_btns">( Comment )</button>
</div>
</div>
<div class="carousel_caption_container">
<div class="carousel_caption_div">
<p class="carousel_caption_username">$user->username - $post->caption</p>
<p class="carousel_caption">$post->caption</p>
</div>
<div class="post-tags mb-4">
<strong>Tags : </strong>
@foreach($post->tags as $tag)
<span class="badge badge-info">$tag->name</span>
@endforeach
</div>
</div>
</div>
@endforeach
我该如何解决这个问题?
此外,如何让标签充当过滤按钮,根据标签名称显示帖子?
【问题讨论】:
您将标签保存为文本,而不是 JSON。所以你不会在foreach中得到它。此外,检查帖子的其他详细信息是否保存? 查看link 帖子的所有详细信息都保存在数据库中。我尝试将标签保存为 JSON,但标签仍未显示在视图中。我还尝试从您发送的链接中应用代码。它实际上是我以前使用的资源,但最终让我感到困惑。 【参考方案1】:您可以将标签保存为 db 中的 json 数组。然后在模型中转换为数组,这样当你检索时它会自动数组。保存时将标签作为数组传递,然后它会自动转换为 json 字符串。
protected $casts = [
'tags' => 'array',
];
【讨论】:
在尝试保存到数据库时,您必须将标签作为数组传递。您可以多次使用 select2。以上是关于如何为帖子创建类别标签?我正在使用 laravel 8的主要内容,如果未能解决你的问题,请参考以下文章
如何为多标签分类器/一对休息分类器腌制 sklearn 管道?