【问题标题】:LARAVEL Publish and Not-publish Function has issueLARAVEL 发布和不发布功能有问题
【发布时间】: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-&gt;published == 1)发表的内容
  • 如果不存在,则显示已发布和未发布

标签: laravel laravel-blade laravel-livewire


【解决方案1】:

我认为您尝试处理收音机的方式并不好。我建议您尝试为每个单选按钮使用不同的属性,例如

<div>
    <input type="radio" name="published" id="published" wire:model="viewPublished">
    <label for="published">Published</label>
    <input type="radio" name="all" id="all" wire:model="viewAll">
    <label for="all">All</label>
</div>

在组件中

    public $viewAll = 'on';
    public $viewPublished = 'off';

    public function render()
    {
        if ($this->viewAll == 'on')
        {

            return view('livewire.radio-component', ['posts' => Post::all()]); // retrieve all posts
        }
        else
        {
            return view('livewire.radio-component', ['posts' => Post::where('published','=',1)->get()]); //....retrieve only published post
        }
    }

    public function updatedViewAll()
    {
        if($this->viewAll == 'on')
            $this->viewPublished = 'off';
    }
    public function updatedViewPublished()
    {
        if ($this->viewPublished == 'on')
            $this->viewAll = 'off';
    }

【讨论】:

  • View [livewire.radio-component] 找不到错误。
  • 当然,这是来自我的示例组件渲染方法,抱歉。使用你的 livewire.posts
  • 我确实使用了 livewire.posts 但它说 foreach() 参数必须是数组|对象类型,给定 null 。然后我确实使用了 foreach ((array) $posts as $post) 但没有显示,我确实检查了数据库并查看了那里的所有帖子,但为什么它们没有出现在网站上?
  • 在 render 方法中创建一个简单的 dd(Post::all()) 并查看是否从 db 检索帖子
【解决方案2】:

试试这个

  @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">
    @elseif($post->published == 0)
            <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">
     @else
                <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">
       @endif
         <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>
  @endforeach
                             

【讨论】:

  • 没有任何改变,因为我实现了您的代码,甚至删除、查看、编辑按钮也没有出现
猜你喜欢
  • 2020-10-31
  • 1970-01-01
  • 2020-04-17
  • 2013-07-04
  • 2015-04-06
  • 2018-10-01
  • 1970-01-01
  • 2019-04-04
  • 1970-01-01
相关资源
最近更新 更多