0

I am working on a Laravel 8 blogging application. I need a large numer of articles in order to test the pagination.

For this purpose, I have made this factory:

class ArticleFactory extends Factory
{ 
 /**
 * The name of the factory's corresponding model.
 *
 * @var string
 */
protected $model = Article::class;

/**
 * Define the model's default state.
 *
 * @return array
 */
public function definition()
{
    
    $title = $this->faker->sentence(2);

    return [
          'user_id' => $this->faker->randomElement([1, 2]),
          'category_id' => 1,
          'title' => $title,
          'slug' => Str::slug($title, '-'),
          'short_description' => $this->faker->paragraph(1),
          'content' => $this->faker->paragraph(5),
          'featured' => 0,
          'image' => 'default.jpg',
    ];
  }
}

The problem

Unfortunately, the title column in the articles table is populated with sentences that have a dot at the end. Titles are not supposed to end with a dot.

How can I fix this?
2
  • 1
    For your use case i don't see a reason why you couldn't just do $title = $this->faker->name() Apr 1, 2022 at 12:57
  • Well name() returns a name, and to get the fake data to be as true to life as possible it would make sense to provide something other than a name. Apr 1, 2022 at 13:10

2 Answers 2

3

Instead of $this->faker->sentence(2); you could use $this->faker->words(3, true); where you can replace the 3 with the amount of words you want. true is there so it returns a string and not an array

It adds a dot because you use ->sentence() and sentences, normally, have a period at the end. Whereas words typically do not have a period at the end.

You can ofcourse also provide a random amount of words by using rand(). Say you want a title to be between 5 and 15 words, you can use $this->faker->words(rand(5, 15), true);

4
  • 1
    or remove the dot without asking too many questions: $title = rtrim($title, ".");
    – Diego D
    Apr 1, 2022 at 12:59
  • How about a random number of words? Apr 1, 2022 at 12:59
  • @DiegoDeVita Sure, that's an option aswell. Apr 1, 2022 at 12:59
  • 1
    If one would choose to generate words, then I recommend wrapping that in ucfirst to make the first letter uppercase, just like that: ucfirst($this->faker->words(rand(5, 15), true))
    – Rusurano
    Jan 31, 2023 at 16:57
1

This is how I choose to achieve the desired result, in case it helps someone else:

public function definition() {
        
  $title = $this->faker->sentence(2);

  return [
    'user_id' => $this->faker->randomElement([1, 2]),
    'category_id' => 1,
    'title' => rtrim($title, '.'),
    'slug' => Str::slug($title, '-'),
    'short_description' => $this->faker->paragraph(1),
    'content' => $this->faker->paragraph(5),
    'featured' => 0,
    'image' => 'default.jpg',
   ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.