【发布时间】:2021-06-20 20:12:43
【问题描述】:
我正在创建一个博客站点,但我遇到了一个问题,因为当用户单击“创建新帖子”按钮时,会出现一个表单显示(创建新帖子图片)。表单中有两个复选框,“已发布”“未发布”。当用户选中“已发布”时,他们的帖子将显示在帖子网站上,状态为“已发布”。当用户勾选“未发布”时,他们的帖子不会显示在 Post 网站上,而是显示在帖子数据库中。
在 Post 网站上,我必须选择单选按钮。第一个是“Publish Posts”,仅显示已发布的帖子,第二个是“All Posts”,将显示所有帖子,包括“未发布”的帖子。
我有一个问题,当我点击“发布帖子”时,只会显示已发布的帖子,这很好。但是当我点击“所有帖子”时,仍然显示已发布的帖子,我没有看到任何“未发布”的帖子。我检查了数据库,看到那里有“未发布”的帖子,所以当我点击“所有帖子”时为什么不出现。有人可以帮我解决吗?
posts.blade.php
<button wire:click="create()" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded my-3">Create New Post</button>
@if($isOpen)
@include('livewire.create')
@endif
<div class="float-right">
<span class="mr-3 d-inline">
<label class="inline-flex items-center">
<input type="radio" class="form-radio" wire:model="viewAll" value="0">
<span class="ml-2">Publish Posts</span>
</label>
<label class="inline-flex items-center ml-6">
<input type="radio" class="form-radio" wire:model="viewAll" value="1">
<span class="ml-2">All Posts</span>
</label>
</span>
</div>
<table class="table-fixed w-full">
<thead>
<tr class="bg-gray-100">
<th class="px-4 py-2 w-20">No.</th>
<th class="px-4 py-2">Title</th>
<th class="px-4 py-2">Body</th>
<th class="px-4 py-2">Published</th>
<th class="px-4 py-2">Action</th>
</tr>
</thead>
<tbody>
@foreach($posts as $post)
@if($post->published == 1)
<tr>
<td class="border px-4 py-2">{{ $post->id }}</td>
<td class="border px-4 py-2 overflow-ellipsis truncate ">{{ $post->title }}</td>
<td class="border px-4 py-2 overflow-ellipsis truncate">{{ $post->body }}</td>
<td class="border px-4 py-2 overflow-ellipsis truncate">Published</td>
<td class="border px-4 py-2">
<button class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded"><a href="/posts/{{ $post->id }}">View</a></button>
@if (Auth::user()->id == $post->user_id )
<button wire:click="edit({{ $post->id }})" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Edit</button>
<button wire:click="delete({{ $post->id }})" class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">Delete</button>
@endif
</td>
</tr>
@endif
@endforeach
</tbody>
</table>
发布架构
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string("title");
$table->string("body");
$table->boolean("published");
$table->integer("user_id")->unsigned()->index();
$table->timestamps();
});
create.blade.php
<div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full" role="dialog" aria-modal="true" aria-labelledby="modal-headline">
<form>
<div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
<div class="">
<div class="mb-4">
<label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Title:</label>
<input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Title" wire:model="title">
@error('title') <span class="text-red-500">{{ $message }}</span>@enderror
</div>
<div class="mb-4">
<label for="exampleFormControlInput2" class="block text-gray-700 text-sm font-bold mb-2">Body:</label>
<textarea class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput2" wire:model="body" placeholder="Enter Body"></textarea>
@error('body') <span class="text-red-500">{{ $message }}</span>@enderror
</div>
<div class="mb-4">
<label class="inline-flex items-center">
<input type="radio" class="form-radio" wire:model="published" name="published" value="1">
<span class="ml-2">Published</span>
</label>
<label class="inline-flex items-center ml-6">
<input type="radio" class="form-radio" wire:model="published" name="published" value="0">
<span class="ml-2">Not-Published</span>
</label>
@error('published') <span class="text-red-500">{{ $message }}</span>@enderror
</div>
</div>
</div>
后模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'body', 'published', 'user_id'
];
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class);
}
}
app/Http/Livewire/Posts.php
<?php
namespace App\Http\Livewire;
use http\Env\Request;
use Livewire\Component;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
class Posts extends Component
{
public $posts, $title, $body, $published, $post_id;
public $viewAll = 0;
public $title_filter;
public $isOpen = 0;
public function render()
{
if($this->viewAll == 0){
$this->posts = Post::where('user_id', Auth::user()->id)
->where('title', 'like', '%' . $this->title_filter. '%')
->get();
} else {
$this->posts = Post::where('user_id', Auth::user()->id)
->where('title', 'like', '%' . $this->title_filter. '%')
->get();
}
return view('livewire.posts');
}
public function create()
{
$this->resetInputFields();
$this->openModal();
}
public function openModal()
{
$this->isOpen = true;
}
public function closeModal()
{
$this->isOpen = false;
}
private function resetInputFields(){
$this->title = '';
$this->body = '';
$this->post_id = '';
}
public function store()
{
$this->validate([
'title' => 'required',
'body' => 'required',
'published' => 'required'
]);
Post::updateOrCreate(['id' => $this->post_id], [
'title' => $this->title,
'body' => $this->body,
'published' => $this->published,
'user_id' => Auth::user()->id
]);
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->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.');
}
}
【问题讨论】:
-
在posts.blade中看到你说只显示这个
@if($post->published == 1)发表的内容 -
如果不存在,则显示已发布和未发布
标签: laravel laravel-blade laravel-livewire