#Passing thru data the create a value in a database laravel livewir

93 messages · Page 1 of 1 (latest)

dark flame
#
    {
        $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```
#

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

earnest spoke
dark flame
#

Thanks!

#

I'm not sure if this is fully laravel related btw

dark flame
#

The laravel server seems a little bit inactive. If anyone knows a possible solution here i would appreciate 😀

earnest spoke
#

You’re welcome to a full refund at your point of purchase.

dark flame
#

Just wanted to make sure people don't think that I'm getting helped in the other server. I can wait

flint sierra
#

i don't see where is your livewire component and how you pass it

dark flame
#

But I got this part just having a small bug rn that I'm looking to solve

tribal obsidian
#

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

flint sierra
dark flame
#

Currently not on pc

flint sierra
#

no code = no help

dark flame
#

                        <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

flint sierra
#

i don't get it how you make select

#

i don't see proper form here

#

and the whole saving looks complicated

dark flame
#

It's not rly a form

#

It's a dropdown with choices of all suppliers

dark flame
#

2 weeks ago i was pretty much 0 knowledged

flint sierra
#

is there a repo i could see?

dark flame
#

But figured out things but not the most efficient ways ig

dark flame
#

But I can pass it through

flint sierra
#

but first thing

#

why no form?

dark flame
#

Ow nmv it's private

flint sierra
#

can you make a screenshot how those selects look?

dark flame
dark flame
flint sierra
#

but why not use form with select input?

dark flame
#

What do you mean?

#

Can you show me that

flint sierra
#

and you didn't provide full livewire component as i said

dark flame
#

Is this an unificient way?

flint sierra
#

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>
dark flame
#

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']);
    }
}
flint sierra
#

you are using it as a fullpage component?

dark flame
#

fullpage?

#

I use it for all my crud

flint sierra
#

okay, first thing do you know what is mount() method in livewire?

#

or __construct() in php?

dark flame
#

Tbh

#

No

#

I heard about mount

#

But don't know how to use/what it it

flint sierra
#

do you know what __counstruct() then is?

dark flame
#

nop never heard of it

flint sierra
#

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

dark flame
#

Okay

flint sierra
#

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?

dark flame
#

Route::get('/manageproducts', ManageProducts::class)->name('manageproducts');

#

It's just that

flint sierra
#

it's a one component for list, create and edit?

dark flame
#

Yes, create edit list load delete

#

All in 1 component

flint sierra
#

that's really bad

dark flame
#

oof

flint sierra
#

it's complicating your component alot

dark flame
#

Mhh yeah I am able to figure it out

#

It works fine for me but maybe not an efficient way

flint sierra
#

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

dark flame
#

This is a dropdownlist?

flint sierra
#

yes

dark flame
#

Alr

#

Give me second to fix it

flint sierra
#

then using Lifecycle Hooks

#

you will be able to set other fields depending on select

dark flame
#

alr

#

ok yeah i implemented the code

#

i'll try this lifecycle thing now