我有一个关于 Laravel 的问题。我正在创建一个博客,允许用户将新帖子上传到页面。下图展示了当用户点击页面上的按钮 Create New Post
时表单的外观。
在表单中,有一个名为 Publish
的复选框,允许用户选择是否要发布帖子。
published
字样。有人知道怎么做吗?我是 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();
});
}
发布模型
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>
答案 0 :(得分:1)
首先,如果您使用复选框不需要有两个,复选框是可选的东西......这种方式是合乎逻辑的,如果用户想要发布帖子选择复选框,如果不是的话不要选择它。 在模态刀片
<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,这样您就可以将此值存储在后模型数据库中并检索它。当数据在刀片中列出时,就像你的表格一样,你可以这样做
<table>
<thead>
// ...
<tr>
<th>Published</th>
//....
<tbody>
@foreach(...)
<tr>
//.....
<td>
@if($posts->published) Published @endif
</td>
//....
希望对你有帮助。问候
答案 1 :(得分:0)
您可以通过在列表方法中添加查询来仅返回已发布的帖子
$user->posts->where("published",1)->get();
还使用全局作用域,这样您就可以只处理条件为 https://laravel.com/docs/8.x/eloquent#global-scopes
的帖子