{
$validatedData = $this->validate();
if(!$this->isEditing){
$product = Product::create([
'name' => $validatedData['newProductName'],
'price' => $validatedData['newProductPrice'],
'supplier_id' => $validatedData['newProductSupplier'],
'barcode' => $validatedData['newProductBarcode'],
'active' => $validatedData['newProductActive'] ?? 0,
]);
$this->dispatchBrowserEvent('swal:toast', [
'background' => 'success',
'html' => "Het product <b><i>{$product->name}</i></b> is toegevoegd.",
]);
}```
```<div class="flex items-center mt-5">
<h1 class="w-1/3 font-bold">Leverancier</h1>
<x-dropdown align="left" wire:model="selectedSupplier">
<x-slot name="trigger" width="64">
<button class="py-2 px-4 rounded-md shadow-md text-black bg-white focus:outline-none focus:ring-2 focus:border-indigo-500 focus:ring-indigo-500 flex-1 sm:flex-none">
@if($selectedSupplier)
{{ $selectedSupplier->name }}
@else
Kies een leverancier
@endif
</button>
</x-slot>
<x-slot name="content">
<ul>
@foreach ($suppliers as $supplier)
<li>
<a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" wire:click.prevent="selectSupplier({{ $supplier->id }})">{{ $supplier->name }}</a>
</li>
@endforeach
</ul>
</x-slot>
</x-dropdown>
</div>
@error('newProductSupplier')
<div class="flex items-center ">
<div class="w-1/3"></div>
<span class="text-red-500 flex-1">{{ $message }}</span>
</div>
@enderror```
#Passing thru data the create a value in a database laravel livewir
93 messages · Page 1 of 1 (latest)
I do know that it's wrong. But I don't know a solution. tested out many things but looks like its not the same principe as the inputs
You can ask Livewire-related questions in the official Livewire Discord server:
https://discord.gg/livewire
The laravel server seems a little bit inactive. If anyone knows a possible solution here i would appreciate 😀
Sorry people haven’t jumped on your question the second you posted it.
You’re welcome to a full refund at your point of purchase.
No problem. I have time
Just wanted to make sure people don't think that I'm getting helped in the other server. I can wait
i don't see where is your livewire component and how you pass it
Well I got it 90% working
But I got this part just having a small bug rn that I'm looking to solve
Can I ask you to elaborate little bit more about your issue, I've been using livewire from past few months and I think I can help
Doesn't how much you got it working. If you can't provide full code no one can help you
Well yeah it work pretty well rn. I can share the code where i still get a bug
Currently not on pc
no code = no help
<h1 class="w-1/3 font-bold">Leverancier</h1>
<x-dropdown align="left" wire:model="selectedSupplier">
<x-slot name="trigger" width="64">
<button class="py-2 px-4 rounded-md shadow-md text-black bg-white focus:outline-none focus:ring-2 focus:border-indigo-500 focus:ring-indigo-500 flex-1 sm:flex-none">
@if($isEditing)
{{ $newProductSupplier }}
@else
@if($selectedSupplier)
{{ $selectedSupplier->name }}
@else
Kies een leverancier
@endif
@endif
</button>
</x-slot>
<x-slot name="content">
<ul>
@foreach ($suppliers as $supplier)
<li>
<a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900" wire:click.prevent="selectSupplier({{ $supplier->id }})">{{ $supplier->name }}</a>
</li>
@endforeach
</ul>
</x-slot>
</x-dropdown>
</div>
@error('newProductSupplier')
<div class="flex items-center ">
<div class="w-1/3"></div>
<span class="text-red-500 flex-1">{{ $message }}</span>
</div>
@enderror```
```public function createProduct()
{
$validatedData = $this->validate();
if(!$this->isEditing){
$product = Product::create([
'name' => $validatedData['newProductName'],
'price' => $validatedData['newProductPrice'],
'supplier_id' => $this->selectedSupplier->id,
'barcode' => $validatedData['newProductBarcode'],
'active' => $validatedData['newProductActive'] ?? 0,
]);
$this->dispatchBrowserEvent('swal:toast', [
'background' => 'success',
'html' => "Het product <b><i>{$product->name}</i></b> is toegevoegd.",
]);
}else {
if ($this->selectedProductId) {
$product = Product::find($this->selectedProductId);
if ($product) {
$product->update([
'name' => $validatedData['newProductName'],
'price' => $validatedData['newProductPrice'],
'supplier_id' => $this->selectedSupplier->id ?? $product->supplier_id,
'barcode' => $validatedData['newProductBarcode'],
'active' => $validatedData['newProductActive'] ?? 0,
]);
$this->dispatchBrowserEvent('swal:toast', [
'background' => 'success',
'html' => "Het product <b><i>{$product->name}</i></b> is aangepast.",
]);
$this->isEditing = false;
}
}
}
$this->reset(['newProductName', 'newProductPrice', 'newProductSupplier', 'newProductActive', 'newProductBarcode', 'selectedSupplier']);
}```
'newProductName' => 'required',
'newProductPrice' => 'required',
'newProductSupplier' => 'nullable',
'newProductActive' => 'nullable',
'newProductBarcode' => 'required',
];```
So this is the code that I think is needed for my bug
I am able to forward data from my dropdown but only when i make the $rules for newProductSupplier nullable. The data just comes trough without any problems
When i have it on required (what it's supposed to be) then it always activates the errormessage and says 'supplier is needed'
So the problem rn is that I don't know how I can validate the dropdown list
i don't get it how you make select
i don't see proper form here
and the whole saving looks complicated
Well yeah I'm kinda a noobie
2 weeks ago i was pretty much 0 knowledged
is there a repo i could see?
But figured out things but not the most efficient ways ig
Euh well it's a schoolwork
But I can pass it through
Ow nmv it's private
can you make a screenshot how those selects look?
I have no idea. I searched on google for dropdown lists and this is what i got
but why not use form with select input?
and you didn't provide full livewire component as i said
Is this an unificient way?
well it's a basic html form
<div>
<form wire:submit.prevent="submit">
<div>
<label class="block text-sm font-medium text-gray-700" for="category"> Category* </label>
<select wire:model="category" name="category"
class="mt-2 w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" required>
<option value="">-- choose category --</option>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
</div>
namespace App\Http\Livewire;
use App\Models\Product;
use App\Models\Supplier;
use Livewire\Component;
use Illuminate\Validation\Rule;
class ManageProducts extends Component
{
public $newProductName;
public $newProductPrice;
public $newProductSupplier;
public $newProductActive;
public $newProductBarcode;
public $isEditing = false;
public $selectedProductId;
public $selectedSupplier;
protected $rules = [
'newProductName' => 'required',
'newProductPrice' => 'required',
'newProductSupplier' => 'nullable',
'newProductActive' => 'nullable',
'newProductBarcode' => 'required',
];
protected $validationAttributes = [
'newProductName' => 'Naam',
'newProductPrice' => 'Prijs',
'newProductSupplier' => 'Leverancier',
'newProductBarcode' => 'Barcode'
];
public function createProduct()
{
$validatedData = $this->validate();
if(!$this->isEditing){
$product = Product::create([
'name' => $validatedData['newProductName'],
'price' => $validatedData['newProductPrice'],
'supplier_id' => $this->selectedSupplier->id,
'barcode' => $validatedData['newProductBarcode'],
'active' => $validatedData['newProductActive'] ?? 0,
]);
$this->dispatchBrowserEvent('swal:toast', [
'background' => 'success',
'html' => "Het product <b><i>{$product->name}</i></b> is toegevoegd.",
]);
}else {
if ($this->selectedProductId) {
$product = Product::find($this->selectedProductId);
if ($product) {
$product->update([
'name' => $validatedData['newProductName'],
'price' => $validatedData['newProductPrice'],
'supplier_id' => $this->selectedSupplier->id ?? $product->supplier_id,
'barcode' => $validatedData['newProductBarcode'],
'active' => $validatedData['newProductActive'] ?? 0,
]);
$this->dispatchBrowserEvent('swal:toast', [
'background' => 'success',
'html' => "Het product <b><i>{$product->name}</i></b> is aangepast.",
]);
$this->isEditing = false;
}
}
}
$this->reset(['newProductName', 'newProductPrice', 'newProductSupplier', 'newProductActive', 'newProductBarcode', 'selectedSupplier']);
}```
{
$this->selectedSupplier = Supplier::find($supplierId);
$this->newProductSupplier = $this->selectedSupplier->name;
}
public function addProduct(){
$this->isEditing = false;
$this->reset(['newProductName', 'newProductPrice', 'newProductSupplier', 'newProductActive', 'newProductBarcode']);
}
public function loadProductData()
{
$product = Product::find($this->selectedProductId);
if ($product) {
$this->newProductName = $product->name;
$this->newProductPrice = $product->price;
$this->newProductSupplier = $product->supplier->name;
$this->newProductBarcode = $product->barcode;
$this->newProductActive = $product->active;
$this->isEditing = true;
}else{
$this->dispatchBrowserEvent('swal:toast', [
'background' => 'danger',
'html' => "Selecteer een product.",
]);
}
}
public function deleteProduct()
{
$product = Product::find($this->selectedProductId);
$product->delete();
$this->dispatchBrowserEvent('swal:toast', [
'background' => 'danger',
'html' => "Het product <b><i>{$product->name}</i></b> is verwijderd.",
]);
}
public function render()
{
$products = Product::all();
$suppliers = Supplier::where('active', true)->get();
return view('livewire.manage-products', compact('products','suppliers'))
->layout('layouts.magTemplate', [
'description' => 'CRUD Products',
'title' => 'Products']);
}
}
you are using it as a fullpage component?
okay, first thing do you know what is mount() method in livewire?
or __construct() in php?
do you know what __counstruct() then is?
nop never heard of it
yeah so the first thing is you need a lot more php basics
but okay let's try
so the mount() gets called when livewire component loads
it's the first thing
Okay
so here what you can you (well that's what I do if I want to use same component for create & edit)
in the mount i would get all products
and suppliers
also how does your routes look for both create and edit?
Route::get('/manageproducts', ManageProducts::class)->name('manageproducts');
It's just that
it's a one component for list, create and edit?
that's really bad
oof
it's complicating your component alot
Mhh yeah I am able to figure it out
It works fine for me but maybe not an efficient way
well first then create a proper form using select
<div>
<form wire:submit.prevent="submit">
<div>
<label class="block text-sm font-medium text-gray-700" for="category"> Category* </label>
<select wire:model="category" name="category"
class="mt-2 w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500" required>
<option value="">-- choose category --</option>
@foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
</div>
</div>
example code for you
This is a dropdownlist?
yes