ezmiks's avatar

ezmiks started a new conversation+100 XP

2mos ago

Is there a way for zodobject to be converted to a plain object?

Like, I have the following schema:

const schema = z.object({
	name: z.string().default(someResource?.name ?? null),
	email: z.email().optional().default(someResource?.email ?? null),
	address: z.string().optional().default(someResource?.address ?? null),
})

and I have tried: schema.parse({}) and it seems like it gives me a plain object, but without the default values of someObject (for this example's sake, let's just say someObject is not undefined or null).

I'm asking this question, because it's very inconvenient if I create the same object for the initial form data values like:

const formData = {
    name: someResource?.name ?? null,
 	email: someResource?.email ?? null,
	address: someResource?.address ?? null)
}

// and then define again the same fields:
const schema = z.object({
	name: z.string().default(someResource?.name ?? null),
	email: z.email().optional().default(someResource?.email ?? null),
	address: z.string().optional().default(someResource?.address ?? null),
})

because what if I have over 10 fields, it would be very tiresome to re-define the same fields for formData and for the schema

ezmiks's avatar

ezmiks started a new conversation+100 XP

4mos ago

Hello,

I have a model DepositMethod, and one of its property is called cost_settings

I casted it like this:

'cost_settings' => AsCollection::of(CostSettingData::Class)

but as far as I know, this casting only mutates the data when accessed. Is there a way to also enforce error if any value formats other than CostSettingData is provided?

Because when I push data like $depositMethod->cost_settings->push(['random' => 'value']), this still works, and I don't want that. One way of course is to validate the request, but I still prefer if it throws an error immediately. Because I want to avoid any format being inserted wrongfully by other devs for some reason.

ezmiks's avatar

ezmiks wrote a reply+100 XP

5mos ago

I think I may have overcomplicated my current setup.

I've actually extended PivotModel, but I think it makes more sense if I just treat this as a regular model.

ezmiks's avatar

ezmiks wrote a reply+100 XP

5mos ago

Can you elaborate why?

Also, would you consider it as a PivotModel or a standard Model?

Thanks!

ezmiks's avatar

ezmiks started a new conversation+100 XP

5mos ago

Hi,

I'm currently designing our database structure, and I'm currently struggling wether to define 1-to-many or Many-to-many.

So, here's a sample scenario:

Let's say I have the following entities:

  1. Hero
  2. City

At first glance, we know that a Hero may save many Cities, and a City can be saved by many Heroes. So we can already say there's a M:M relationship there.

But is it acceptable if I treat this M:M table into a single entity?

Here's the table definitions for each relationship:

  1. If I treat this table as it is, a Many-to-Many pivot table: city_hero
    	city_id
    	hero_id
    
  2. If I treat it as a standalone entitiy: saves
    	city_id
    	hero_id
    	saved_on
    	casualties
    

As you can see, either way, city_id and hero_id are still being referenced.

I know I can extend my model with the Illuminate\Database\Eloquent\Relations\Pivot class, and I also know that there's nothing holding me back on treating this as a standalone entity, but what would be the downside/effects if I treat it as one instead of a pivot entity?