Skip to main content

Order Create/Edit with Pikaday

Premium
16 min read

In this lesson, we will create a Livewire component for creating and editing orders. We will reuse some logic from before lessons, like select2 component or Pikaday, so not everything new will be new here.

working order form

Again, let's start this lesson by creating the Livewire component, Route Model binding Order and we will add a frontend layout with hard-coded data. Next, as this is a new component we need to register a route for it and make the Create and Edit buttons work. Also, to bind the input to the $order property we need validation rules, so let's also add them now...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 21 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

A
AlbertLens ✓ Link copied!

Everything fine but EDIT order.

I keep getting an error: > A non-numeric value encountered

in the render function view line:

$this->order->subtotal += $orderProduct['product_price'] * $orderProduct['quantity'];

Creating is working fine. I have dd() for checking no nulls and everything is ok and also checked the table in MySQL.

Any ideas, please?

PK
Povilas Korop ✓ Link copied!

Hard to "blindly" debug it for you, any of those three variables could be "non-numeric" technically. Could you var_dump() all of those separately and see the values?

A
AlbertLens ✓ Link copied!

I understand, of course. I have dd(productPrice) inside OrderForm.php and when creating it returns the product value of 44 as 4400 but when I try to edit that order, it returns an array with dots and commas, as "4,400.00". I think I will have to check if isNumeric and if it is not convert it all the time. By the way, here in Spain, using Euros, I usually put the price as float with 2 decimal number. I understood you advice to use integer and use the two last digits as decimals. In which way is this better, please?

Thanks in advance for your help and advice.

PK
Povilas Korop ✓ Link copied!

Technically speaking, dealing with money is even more complicated, I suggest you read this in-depth article: Dealing With Money in Laravel/PHP: Best Practices

Regarding that it comes with dots and commas, this is interesting, for some reason it didn't happen during my testing. If you are unable to debug it yourself, you could push your code to GitHub and invite me (username PovilasKorop), I may take a look but only in a week or so.

J
jayadev ✓ Link copied!

Everything fine but when EDIT order in all orders page. it show A non-numeric value encountered error

$this->order->subtotal += $orderProduct['product_price'] * $orderProduct['quantity'];

in render function

J
jayadev ✓ Link copied!

i download your code from github it works fine when i order two product then i try two edit from product list it shows the same error please check A non-numeric value encountered

J
jayadev ✓ Link copied!

some of orders edit working and some orders edit showing error A non-numeric value encountered in your github code please check

PK
Povilas Korop ✓ Link copied!

Can you help to debug find those "some"? How exactly we can reproduce this: just by clicking around on random orders edit?

J
jayadev ✓ Link copied!

Dear sir, please check i shared a screen recording of the error. uploded on youtube link is unlisted. this is the link of the video

https://youtu.be/0_DGOIUJnhg

J
jayadev ✓ Link copied!

order price under six digit edit working but when it crossed seven digit or above for example like $57,356.30, $30,233.06, $96,285.75 it shows the A non-numeric value encountered error

PK
Povilas Korop ✓ Link copied!

Great, thanks! Now the error is clear, we need to debug and fix from our side, will update on this.

N
Nerijus ✓ Link copied!

You can check this commit to see what was changed. Hope it helps other :)

ED
Enrique De Jesus Robledo Camacho ✓ Link copied!

if you try to edit an order adding a product that all ready exists in the order an save it, this last product wont get saved

M
muuucho ✓ Link copied!

Updating a product is only validated frontend. How can this be achieved also backend?

N
Nerijus ✓ Link copied!

Add validation rules. What exactly you wamt to validate?

M
muuucho ✓ Link copied!

Both the products id and the products quantity. Just good practice. I don't know how to achieve this since the rules method is already "taken" by the Order. But I am eager to learn.

N
Nerijus ✓ Link copied!

If you mean that product id should exist then there is exists validation rule. As for quantity depends what you want. If you have some stock then the rule should be Less Than Or Equal.

M
muuucho ✓ Link copied!

Thanks. I am familiar with the rules. But it's not that. Instead, it's that the product is "dynamic" when it hits the saveProduct() method. So, how do I validate and how do I send any error back to the view from a failed product validation? Also the form should show any passed values (can old method be used?)

N
Nerijus ✓ Link copied!

With livewire old isn't needed. When you hit in this saveProduct() call the $this->validate() and livewire will validate with your rules.

M
muuucho ✓ Link copied!

So, I do that and add this line to rules(): 'quantity' => ['required', 'numeric', 'min:3'], (min:3 doesn't make sence here, it is just demonstrate that your sugestion doesn't work. ) Now, storing a quantity of 2 is still fine. I belive it is more complicated than that and therefor I ask for help. Let's aggree that a form shall have backend validation.

N
Nerijus ✓ Link copied!

Without full code cannot help. Come to discord and try asking there.

M
muuucho ✓ Link copied!

You just have to add these line of code to see it fail...

M
McNab ✓ Link copied!

The first code block in the tutorial for order-form.blade.php contains this line;

<x-select2 class="mt-1" id="country" name="country" :options="$this->listsForFields['users']" wire:model="user_id" />

It is missing the data binding for selectedOptions and will throw an unresolvable dependency error, replace with this;

<x-select2 class="mt-1" id="country" name="country" :options="$this->listsForFields['users']" wire:model="user_id" :selectedOptions="$user_id" />

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.