繁体   English   中英

挑战 Laravel 复选框发布或不发布帖子

[英]Challenge with Laravel Checkbox to Publish or Not Publish Post

我有一个关于 Laravel 的问题。 我正在创建一个允许用户将新帖子上传到页面的博客。 下图是用户单击页面上的按钮Create New Post时表单的外观。

在表单中,有一个名为Publish的复选框,允许用户选择是否要发布帖子。

  1. 如果用户选中“发布”,则帖子将显示在页面上,并在“ published ”列中显示“发布”字样。
  2. 如果用户勾选“不发布”,当他们点击“保存”按钮时,帖子将不会显示在页面上。

有谁知道该怎么做? 我是 Laravel 的新手,所以我不知道该怎么做。 我在 gg 上进行了搜索,人们在谈论名为 Controller 的东西。 我需要它来执行发布和不发布功能吗?

页面图片:

将显示帖子的主页(在“已发布”字段中应包含“已发布”一词)

后面板

用户点击按钮Create New Post时的表单

模态形式新帖子

后架构

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title');
        $table->string('body');
        $table->string('author');
        $table->boolean('published')->default(0);
        //create the relationship between a task and the user that created it
        $table->integer('user_id')->unsigned()->index();
        $table->timestamps();
    });
}

发布 Model

class Post extends Model
{
    use HasFactory;

    protected $fillable = [
        'title', 'body', 'author', 'published'
    ];

    public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->user_id = Auth::id();
        });

        static::updating(function ($model) {
            $model->user_id = Auth::id();
        });
    }
}

app/Http/Livewire/Posts.php

class Posts extends Component
{
    public $posts, $title, $body, $post_id, $author, $published;
    public $isOpen = 0;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function render()
    {
        $user = auth()->user();

        $this->posts = $user->posts;
        return view('livewire.posts');
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function create()
    {
        $this->resetInputFields();
        $this->openModal();
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function openModal()
    {
        $this->isOpen = true;
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function closeModal()
    {
        $this->isOpen = false;
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    private function resetInputFields()
    {
        $this->title = '';
        $this->body = '';
        $this->post_id = '';
        $this->author = '';
        $this->published = '';
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function store()
    {
        $this->validate([
            'title' => 'required',
            'body' => 'required',
            'author' => 'required',
            'published' => 'required'
        ]);

        Post::updateOrCreate(['id' => $this->post_id], [
            'title' => $this->title,
            'body' => $this->body,
            'author' => $this->author,
            'published' => $this->published
        ]);

        session()->flash('message',
            $this->post_id ? 'Post Updated Successfully.' : 'Post Created Successfully.');

        $this->closeModal();
        $this->resetInputFields();
    }
    
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function edit($id)
    {
        $post = Post::findOrFail($id);
        $this->post_id = $id;
        $this->title = $post->title;
        $this->body = $post->body;
        $this->author = $post->author;
        $this->published = $post->published;
        $this->openModal();
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    public function delete($id)
    {
        Post::find($id)->delete();
        session()->flash('message', 'Post Deleted Successfully.');
    }
}

这是我用来为“发布”创建复选框的.blade 代码

<div class="mb-4">
    <label for="check" class="form-check-label">Publish:</label>
    <input class="form-check-input" id="check" value="publish" type="checkbox" name="published" wire:model="published">Publish</input>
    <input class="form-check-input" id="check" value="no-publish" type="checkbox" name="published" wire:model="published">No Publish</input>
</div>

首先,如果您使用复选框,则不需要两个复选框,复选框是可选的东西......这种方式是合乎逻辑的,如果用户想要发布帖子 select 复选框,如果不仅仅是 select它。 在模态刀片中

<div class="mb-4">
    <label for="check" class="form-check-label">Publish:</label>
    <input class="form-check-input" id="check" value="publish" type="checkbox" name="published" wire:model="published">Publish</input>
</div>

默认情况下,在组件中,$this->published = 0。但如果用户将复选框标记为选中,则更改为 1,这样您就可以将此值存储在 model 数据库中并检索它。 当数据列在刀片中时,就像你的表一样,你可以这样做

<table>
   <thead>
    // ...
   <tr>
     <th>Published</th>
     //....
   <tbody>
   @foreach(...)
     <tr>
      //.....
       <td>
           @if($posts->published) Published @endif
       </td>  
   //....

希望这对您有所帮助。 问候

您可以通过将查询添加到列表方法中来仅返回已发布的帖子

$user->posts->where("published",1)->get();

还使用全局 scope 这将让您只处理符合您的条件的帖子https://laravel.com/docs/8.x/eloquent#global-scopes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM