Feb 9, 2023
0
Level 1
Livewire image upload validata fail
This is my controller, product page and my route. i want upoload image in table view and save path in product database, but when click upload button i have validation error (image required). The image is saved in storage temp directory. Why? thanks for all
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Product;
use Livewire\WithFileUploads;
use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\Calculation\Database\DProduct;
use Illuminate\Support\Facades\Storage;
class Products extends Component
{
use WithFileUploads;
public $products, $name, $detail, $giac, $classe, $product_id;
public $isOpen = 0;
public $photo;
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function render()
{
// dd(env(storage_path()));
$this->products = Product::orderBy('name', 'asc')->get();
return view('livewire.products');
}
/**
* 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->name = '';
$this->detail = '';
$this->product_id = '';
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function store()
{
$this->validate([
'name' => 'required',
// 'detail' => 'required',
'giac' => 'required',
'classe' => 'required',
'photo' => 'required|image|mimes:jpg,jpeg,png,svg,gif|max:1024', // 1MB Max
]);
Product::updateOrCreate(['id' => $this->product_id], [
'name' => $this->name,
// 'detail' => $this->detail,
'giac' =>$this->giac,
'photo' =>$this->photo
]);
session()->flash('message',
$this->product_id ? 'Product Updated Successfully.' : 'Product Created Successfully.');
$this->closeModal();
$this->resetInputFields();
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function edit($id)
{
$product = Product::findOrFail($id);
$this->product_id = $id;
$this->minsan = $product->minsan;
$this->name = $product->name;
$this->classe = $product->classe;
$this->detail = $product->detail;
$this->Giac = $product->giac;
$this->photo=$product->photo;
$this->openModal();
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function delete($id)
{
Product::find($id)->delete();
session()->flash('message', 'Product Deleted Successfully.');
}
public function uploadImage()
{
// dd($this->photo);
// dd(env(storage_path()));
$data = $this->validate([
'photo' => 'required|image|mimes:jpg,jpeg,png,svg,gif|max:1024', // 1MB Max
]);
//dd($data);
$data['photo']= $this->photo->store('documents');
$filename = $this->photo->store('documents','public');
$data['photo'] = $filename;
Product::create($data);
session()->flash('message', 'File has been successfully Uploaded.');
// $this->photo->store('public/images');
session()->flash('message', 'Image successfully Uploaded.');
}
}
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight text-center">
Elenco Prodotti - Farmaworld
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg px-4 py-4">
@if (session()->has('message'))
<div class="bg-teal-100 border-t-4 border-teal-500 rounded-b text-teal-900 px-4 py-3 shadow-md my-3" role="alert">
<div class="flex">
<div>
<p class="text-sm">{{ session('message') }}</p>
</div>
</div>
</div>
@endif
<button wire:click="create()" class="bg-blue-500 hover:bg-blue-700 text-white py-1 mb-6 px-3 rounded my-3 mt-1">Create New Product</button>
@if($isOpen)
@include('livewire.create')
@endif
<table class="table-auto">
<thead>
<tr class="bg-gray-100">
<th class="px-4 py-2 w-20">Id</th>
<th class="px-4 py-2">Image</th>
<th class="px-4 py-2">Minsan</th>
<th class="px-4 py-2">Descrizione</th>
<th class="px-4 py-2">Classe</th>
<th class="px-4 py-2 ">Prezzo</th>
<th class="px-4 py-2 ">Pz venduti</th>
<th class="px-4 py-2 w-60">Action</th>
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td class="border px-4 py-2">{{ $product->id }}</td>
<td class="border px-4 py-2">
<img class="w-8 h-8 rounded-full" src="{{ Storage::url($product->image_path) }}" />
</td>
<td class="border px-4 py-2">{{ $product->minsan }}</td>
<td class="border px-4 py-2">{{ $product->name }}</td>
<td class="border px-4 py-2">{{ $product->classe }}</td>
<td class="border px-4 py-2">{{ $product->prezzo }}</td>
<td class="border px-4 py-2">{{ $product->tot_vend }}</td>
<td class="border px-4 py-2 text-center">
<button wire:click="edit({{ $product->id }})" class="bg-blue-500 hover:bg-blue-700 text-white py-1 px-3 rounded">Edit</button>
<button wire:click="delete({{ $product->id }})" class="bg-red-500 hover:bg-red-700 text-white py-1 px-3 rounded">Delete</button>
<form wire:submit.prevent="uploadImage" enctype="multipart/form-data">
<input type="file" wire:model="photo">
@error('photo') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Upload</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Livewire\Crud;
use App\Http\Livewire\Products;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/products', Products::class)->name('products.index');
Route::post('/products', Products::class)->name('products.index');
Please or to participate in this conversation.