简体   繁体   English

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

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

I have a question about Laravel.我有一个关于 Laravel 的问题。 I am creating a blog that allows the user to upload a new post to the page.我正在创建一个允许用户将新帖子上传到页面的博客。 Below is the picture about how the form looks like when the user clicks the button Create New Post on the page.下图是用户单击页面上的按钮Create New Post时表单的外观。

In the form, there is a checkbox called Publish which allows the user to choose if they want to publish the post or not.在表单中,有一个名为Publish的复选框,允许用户选择是否要发布帖子。

  1. If the user checks "Publish", the post will be displayed on the page with the word published on the column "Published".如果用户选中“发布”,则帖子将显示在页面上,并在“ published ”列中显示“发布”字样。
  2. If the user checks "No Publish", when they click the button "Save", the post won't be displayed on the page.如果用户勾选“不发布”,当他们点击“保存”按钮时,帖子将不会显示在页面上。

Does anyone know how to do it?有谁知道该怎么做? I am new to Laravel so I have no idea how to do it.我是 Laravel 的新手,所以我不知道该怎么做。 I did a search on gg and people talking about something called Controller.我在 gg 上进行了搜索,人们在谈论名为 Controller 的东西。 Do I need it to do the Publish and No Publish feature?我需要它来执行发布和不发布功能吗?

Picture of page:页面图片:

The main page where the post will be displayed (in the "Published" field should have the word "Published" in it)将显示帖子的主页(在“已发布”字段中应包含“已发布”一词)

后面板

The form when user clicks on the button Create New Post用户点击按钮Create New Post时的表单

模态形式新帖子

Post Schema后架构

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();
    });
}

Post Model发布 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.');
    }
}

This is the.blade code I use to create checkbox for 'Publish'这是我用来为“发布”创建复选框的.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>

First, if you're using checkbox don't need have two for that, check-boxes are optional things...this way is logical that if the user want to publish the post select the checkbox, if not just doesn't select it.首先,如果您使用复选框,则不需要两个复选框,复选框是可选的东西......这种方式是合乎逻辑的,如果用户想要发布帖子 select 复选框,如果不仅仅是 select它。 in modal 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>
</div>

By default, in component, $this->published = 0. But change to 1 if the user mark checkbox as selected, that way you can store this value in post model db and retrieve it.默认情况下,在组件中,$this->published = 0。但如果用户将复选框标记为选中,则更改为 1,这样您就可以将此值存储在 model 数据库中并检索它。 When the data is listed in blade, like your table you can do this当数据列在刀片中时,就像你的表一样,你可以这样做

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

Hope this help you.希望这对您有所帮助。 Greetings问候

you can return only published post by adding query into list method您可以通过将查询添加到列表方法中来仅返回已发布的帖子

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

also use global scope which will let you only deal with posts with your condition https://laravel.com/docs/8.x/eloquent#global-scopes还使用全局 scope 这将让您只处理符合您的条件的帖子https://laravel.com/docs/8.x/eloquent#global-scopes

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

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