将伪装价值设置为少于另一个伪造的田地

发布于 2025-01-30 11:53:01 字数 2458 浏览 4 评论 0原文

您好,我正在创建一个用于折扣型号的工厂。我有两个字段('Original_price'和'discounted_price')。我在工厂中使用RandomFloat来创建一个随机数,其中有2个小数为“ Original_price”。如何将“ discounted_price”设置为始终比'Original_price'的值始终较低?

目前,“ discounted_price”的伪装值与“ ointer_price”相同,因为我找不到一种方法。

<?php

namespace Database\Factories;

use App\Models\Category;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Discount>
 */
class DiscountFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'user_id' =>User::factory(),
            'category_id' => Category::factory(),
            'title' => $this->faker->sentence(),
            'slug' => $this->faker->slug(),
            'body' => '<p>' . implode('</p><p>', $this->faker->paragraphs(6)) . '</p>',
            'original_price' => $this->faker->randomFloat('2',0,2),
            'discounted_price' => $this->faker->randomFloat('2',0,2),
        ];
    }
}

我想我遇到了一个解决方案,但我不知道这是否正确。我的想法是,“ discounted_price”的最小值应为“ ointer_price”的50%,最大值应该是'onigral_price' - 5。这是正确的方法吗?

<?php

    namespace Database\Factories;

    use App\Models\Category;
    use App\Models\User;
    use Illuminate\Database\Eloquent\Factories\Factory;

    /**
     * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Discount>
     */
    class DiscountFactory extends Factory
    {
        /**
         * Define the model's default state.
         *
         * @return array<string, mixed>
         */
        public function definition()
        {
            return [
                'user_id' =>User::factory(),
                'category_id' => Category::factory(),
                'title' => $this->faker->sentence(),
                'slug' => $this->faker->slug(),
                'body' => '<p>' . implode('</p><p>', $this->faker->paragraphs(6)) . '</p>',
                $original_price = 'original_price' => $this->faker->randomFloat('2',0,2),
                'discounted_price' => $this->faker->numberBetween($min=$original_price/2, $max=$original_price-5)
            ];
        }
    }

Hello I'm creating a Factory for a discount model. I have two fields ('original_price' and 'discounted_price'). I'm using randomFloat in factory to create a random number with 2 decimals for the 'original_price'. How can I set 'discounted_price' to be always a lower value than 'original_price'?

At the moment 'discounted_price' has the same faker value than 'original_price' because I cant find a way of doing this.

<?php

namespace Database\Factories;

use App\Models\Category;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Discount>
 */
class DiscountFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'user_id' =>User::factory(),
            'category_id' => Category::factory(),
            'title' => $this->faker->sentence(),
            'slug' => $this->faker->slug(),
            'body' => '<p>' . implode('</p><p>', $this->faker->paragraphs(6)) . '</p>',
            'original_price' => $this->faker->randomFloat('2',0,2),
            'discounted_price' => $this->faker->randomFloat('2',0,2),
        ];
    }
}

I think I came across a solution but I dont know if this would be right. My idea is that the minimum value of 'discounted_price' should be 50% of 'original_price' and the maximum should be 'original_price' - 5. Would this be the correct way of doing it?

<?php

    namespace Database\Factories;

    use App\Models\Category;
    use App\Models\User;
    use Illuminate\Database\Eloquent\Factories\Factory;

    /**
     * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Discount>
     */
    class DiscountFactory extends Factory
    {
        /**
         * Define the model's default state.
         *
         * @return array<string, mixed>
         */
        public function definition()
        {
            return [
                'user_id' =>User::factory(),
                'category_id' => Category::factory(),
                'title' => $this->faker->sentence(),
                'slug' => $this->faker->slug(),
                'body' => '<p>' . implode('</p><p>', $this->faker->paragraphs(6)) . '</p>',
                $original_price = 'original_price' => $this->faker->randomFloat('2',0,2),
                'discounted_price' => $this->faker->numberBetween($min=$original_price/2, $max=$original_price-5)
            ];
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2025-02-06 11:53:01

似乎是对的,我建议您不要在数组值中混合和匹配属性分配以保持清晰

        public function definition()
        {
            $originalPrice = $this->faker->randomFloat('2', 0, 2);
            $discountMin = $originalPrice / 2;
            $discountMax = $originalPrice - 5;

            return [
                'user_id' =>User::factory(),
                'category_id' => Category::factory(),
                'title' => $this->faker->sentence(),
                'slug' => $this->faker->slug(),
                'body' => '<p>' . implode('</p><p>', $this->faker->paragraphs(6)) . '</p>',
                'original_price' => $originalPrice,
                'discounted_price' => $this->faker->numberBetween($discountMin, $discountMax)
            ];
        }

Seems about right, I would advise you to not mix and match property assignment in the array values to keep things clear

        public function definition()
        {
            $originalPrice = $this->faker->randomFloat('2', 0, 2);
            $discountMin = $originalPrice / 2;
            $discountMax = $originalPrice - 5;

            return [
                'user_id' =>User::factory(),
                'category_id' => Category::factory(),
                'title' => $this->faker->sentence(),
                'slug' => $this->faker->slug(),
                'body' => '<p>' . implode('</p><p>', $this->faker->paragraphs(6)) . '</p>',
                'original_price' => $originalPrice,
                'discounted_price' => $this->faker->numberBetween($discountMin, $discountMax)
            ];
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文