Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add faker() helper #41130

Closed
wants to merge 4 commits into from
Closed

Add faker() helper #41130

wants to merge 4 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Feb 20, 2022

This pull request adds a faker() helper, primarily to use in blade views.

This makes it ease to get fake data in your Laravel blade view, without having to make a factory based on your eloquent model.

<div>{{ faker()->name() }}</div>

Get a collection of faker instances

<ul>
    @foreach(faker(10) as $person)
        <li>{{ $person->name() }}</li>
    @endforeach
</ul>

Get a random image

    <img src="{{ faker()->image() }}" alt="{{ faker()->alt() }}" />

@ghost ghost changed the title Add faker helper Add faker() helper Feb 20, 2022
public function __call(string $name, array $arguments)
{
return match ($name) {
'image', 'alt' => $this->faker->$name(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't here be $this->$name() instead?

Otherwise it is equal to the default arm

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigopedra I think you are correcrt, code could be simplified to:

    public function __call(string $name, array $arguments)
    {
        return $this->faker->$name();
    }

Then it would still use the custom image and alt method, and pass the other calls to faker.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sure… I missed that the magic method will only be called for missing methods… might be the late hours from yesterday :)

@ankurk91
Copy link
Contributor

Faker is a dev dependency, and would fail to run in production.
I afraid that developer may start using this method beyond seeders

@ghost
Copy link
Author

ghost commented Feb 21, 2022

Faker is a dev dependency, and would fail to run in production.
I afraid that developer may start using this method beyond seeders

@ankurk91 That's a good point. Although this helper would only make sense for development, there isn't any way to stop a developer for mistakingly use it in production.

One solution might be to check the environment in the Faker magic __cal method and throw an exception when not in development mode. This might make it more clear for anyone using this helper. Although not ideal.

@dennisprudlo
Copy link
Contributor

@JohannesCleve An exception will be thrown in production either way, because the classes for faker won't exist in production. I think @ankurk91 concerns are that people use the faker helper in a dev environment because it works and then deploy the code to production, where it will fail.

@taylorotwell
Copy link
Member

Feel free to add this helper to your own application!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants