首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法App\Http\Livewire\Product::extension不存在

方法App\Http\Livewire\Product::extension不存在
EN

Stack Overflow用户
提问于 2020-12-01 22:19:48
回答 1查看 1.8K关注 0票数 0

我正在学习laravel livewire,这是我第一次使用livewire。我在Laravel 8和laravel-livewire上运行我的代码时遇到了问题。当我单击submit时,总是显示这样的错误。我不知道哪里出了问题,也不知道该怎么解决

web.php

代码语言:javascript
复制
<?php
    
use Illuminate\Support\Facades\Route;
use App\Http\Livewire\Product;
Route::get('/products', Product::class);

控制器

代码语言:javascript
复制
<?php
    
    namespace App\Http\Livewire;
    
    use Livewire\Component;
    use Livewire\WithFileUploads;
    use App\Models\Product as ProductModel;
    use Illuminate\Support\Facades\Storage;
    
    class Product extends Component
    {
        use WithFileUploads;
    
        public $name, $image, $description, $qty, $price;
    
    
        public function previewImage()
        {
            $this->validate([
                'image' => 'image|max:2048'
            ]);
        }
    
        public function store()
        {
            $this->validate([
                'name' => 'required',
                'image' => 'image|max:2048|required',
                'description' => 'required',
                'qty' => 'required',
                'price' => 'required',
            ]);
    
            $imageName = md5($this->image.microtime().'.'. $this->extension());
            
            Storage::putFileAs(
                'public/images',
                $this->image,
                $imageName
            );
    
            ProductModel::create([
                'name' => $this->name,
                'image' => $imageName,
                'description' => $this->description,
                'qty' => $this->qty,
                'price' => $this->price
            ]);
    
            session()->flash('info', 'Product created sSccessfully');
    
            $this->name = '';
            $this->image = '';
            $this->description = '';
            $this->qty = '';
            $this->price = '';
        }
    }

这是我的刀片代码,希望有人能帮我。谢谢

代码语言:javascript
复制
<div>
<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-body">
                <h2 class="font-weight-bold mb-3">Product List</h2>
                <table class="table table-bordered table-hover table-striped">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>Name</th>
                            <th>Image</th>
                            <th>Description</th>
                            <th>Qty</th>
                            <th>Price</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($products as $index=>$product)
                        <tr>
                            <td>{{ $index+1 }}</td>
                            <td>{{ $product->name }}</td>
                            <td>{{ $product->image }}</td>
                            <td>{{ $product->description }}</td>
                            <td>{{ $product->qty }}</td>
                            <td>{{ $product->price }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <h2 class="font-weight-bold mb-3">Create Product</h2>
                <form wire:submit.prevent="store">
                    <div class="form-group">
                        <label>Product Name</label>
                        <input wire:model="name" type="text" class="form-control">
                        @error('name') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>

                    <div class="form-group">
                        <label>Product Image</label>
                        <div class="custom-file">
                            <input wire:model="image" type="file" class="custom-file-input" id="customFile">
                            <label for="customFile" class="custom-file-label">Choose Image</label>
                        </div>
                        @error('image') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>
                    @if($image)
                        <label class="mt-2">Image Preview</label>
                        <img src="{{ $image->temporaryUrl() }}" class="img-fluid" alt="Preview Image">
                    @endif

                    <div class="form-group">
                        <label>Description</label>
                        <textarea wire:model="description" class="form-control"></textarea>
                        @error('description') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>

                    <div class="form-group">
                        <label>Qty</label>
                        <input wire:model="qty" type="number" class="form-control">
                        @error('qty') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>

                    <div class="form-group">
                        <label>Price</label>
                        <input wire:model="price" type="number" class="form-control">
                        @error('price') <small class="text-danger">{{ $message }}</small> @enderror
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn btn-primary btn-block">Submit Product</button>
                    </div>
                </form>
            </div>
        </div>
        <div class="card mt-3">
            <div class="card-body">
                <h3>{{ $name }}</h3>
                <h3>{{ $image }}</h3>
                <h3>{{ $description }}</h3>
                <h3>{{ $qty }}</h3>
                <h3>{{ $price }}</h3>
            </div>
        </div>
    </div>
</div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-02 01:25:55

使用$this->image->extension()\File::extension($this->image);

而不是在代码中使用$this->extension

$imageName = md5($this->image.microtime().'.'. $this->extension());

extension()是FIle类的方法,因此需要Symfony\Component\HttpFoundation\File\UploadedFile类的实例

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65092282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档