laravel 如何使用livewire分页与公共属性?

hec6srdp  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(154)

我面临着一个奇怪的问题,只有当我使用livewire组件分页时,
这是错误:

BadMethodCallException
Method Illuminate\Support\Collection::items does not exist.

我知道这个错误意味着什么,问题是为什么它只在我改变分页时发生:
这是我的组件:

<?php

namespace App\Http\Livewire\Admin;

use Livewire\Component;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use App\Models\Product;
use App\Models\Category;
use File;
use GlobalsHelper;

class Products extends Component
{
    use WithPagination, WithFileUploads;

    public $products;
    public $subcategories;
    
    // Form
    public $name;
    public $description;
    public $price;
    public $quantity;
    public $category_id = null;
    public $sub_category_id = null;
    public $image;

    public $selected = [];
    public $query = '';
    public $filter_by_category = '';

    protected $paginationTheme = 'bootstrap';

    public function render()
    {
        $pagination = $this->products;
        $this->products = collect($this->products->items());

        return view('livewire.admin.products', [
            'categories' => Category::all(),
            'subcategories' => $this->subcategories,
            'products' => $this->products,
            'pagination' => $pagination
        ]);
    }
    // public function render()
    // {
    //     return view('livewire.admin.products', [
    //         'categories' => Category::all(),
    //         'subcategories' => $this->subcategories,
    //         'products' => $this->products,
    //     ]);
    // }

    public function mount()
    {
        $this->getSubcategories();
        $this->index();
    }

    public function getSubcategories()
    {
        $this->subcategories = [];
        if ( !$this->category_id )
        {
            $this->category_id = (Category::first()) ? Category::first()->id : null;
        }  

        $category = Category::find($this->category_id);
        if ( !$category )
        {
            return null;
        }
            
        $this->subcategories = $category->children;
        $this->sub_category_id = ($this->subcategories->first()) ? $this->subcategories->first()->id : null;
    }

    public function store()
    {
        $fields = $this->validate([
            'name' => ['required', 'string', 'min:5', 'max:250'],
            'description' => ['string', 'max:250'],
            'price' => ['required', 'numeric', 'min:1'],
            'quantity' => ['required', 'numeric', 'min:1'],
            'category_id' => ['nullable'],
            'sub_category_id' => ['nullable'],
        ]);

        $fields['user_id'] = GlobalsHelper::auth()->id;
        
        $product = Product::create($fields);
    
        if ( !$product )
        {
            session()->flash('error', __('messages.error_create'));
            return;
        }

        // upload image 
        $this->storeImage($product);

        $this->resetFields();

        session()->flash('success', __('messages.success_create'));
    }

    public function search()
    {
       $results = Product::where('name', 'LIKE', '%'.$this->query.'%')
                    ->orderBy('id', 'desc')->paginate(5);

       $this->products = $results;
    }

    public function index()
    {
        $this->products = Product::orderBy('id', 'desc')->paginate(5);
    }

    public function filterByCategory()
    {
       $results = Product::where('category_id', '=', $this->filter_by_category)
                    ->orderBy('id', 'desc')->paginate(5);

        
       $this->products = $results;
    }

    // private
    private function resetFields()
    {
        $this->name = null;
        $this->description = null;
        $this->price = 0;
        $this->quantity = 1;
        $this->image = null;
        $this->filename = '...';
    }

    private function storeImage($product)
    {
        $this->validate([
            'image' => ['nullable', 'image', 'max:1024'],
        ]);
        if ( $this->image )
        {
            // Delete old image if exists
            if ( $product->image )
            {
                File::delete([$product->image->fullpath]);
            }     
            $filename = sha1( uniqid('', true) );
            $ext = $this->image->getClientOriginalExtension();
            $fullpath = $this->image->store(GlobalsHelper::PRODUCTS_UPLOADS_DIR.'/'.$product->id, 'public');

            $product->update([
                'image' => [
                    'name' => basename($fullpath),
                    'type' => $ext,
                    'fullpath' => GlobalsHelper::STORAGE_DIR.$fullpath,
                    'url' => asset(GlobalsHelper::STORAGE_DIR.$fullpath)
                ]
            ]);
        }
    }
}

我的想法是,我想实现searchstoreupdatedeletefilter操作在相同的组成部分一样,在laravel控制器,我找到了解决方案在线,但只有50%,我需要实现。
我浪费了很多时间想弄明白这件事。
如果有人愿意帮助我,我将不胜感激。

qvsjd97n

qvsjd97n1#

在没有看到视图的情况下,您会通过mount()方法获得$produts,这只会在livewire组件首次渲染时运行一次。
mount()仅在首次挂载组件时调用,即使刷新或重新呈现组件,也不会再次调用。

相关问题