将伪装价值设置为少于另一个伪造的田地
您好,我正在创建一个用于折扣型号的工厂。我有两个字段('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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎是对的,我建议您不要在数组值中混合和匹配属性分配以保持清晰
Seems about right, I would advise you to not mix and match property assignment in the array values to keep things clear