0

I have a Class Like This :

class Ip
{

    public $ip;

    /**
     * @return mixed
     */
    public function getIp()
    {
        return $this->ip;
    }

    /**
     * @param mixed $ip
     */
    public function setIp($ip)
    {
        $this->ip = $ip;
    }

}

and I Write a Factory For This :

/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(\Modules\FraudDetection\ObjectModels\Ip::class, function (Faker\Generator $faker) {

    return [
        'ip' => $faker->numerify('###.###.##.###')
    ];
});

But When i Want to Use That Factory , the result is null :

{"ip":null}

I know If I Extend Model It Will Work . Is There Any Other Solution To use Factory without extending model ?

2
  • try $modelMock = Mockery::mock('App\Model'); and on your model factory call that class. Commented Dec 2, 2019 at 10:39
  • let me check mockery style Commented Dec 2, 2019 at 10:45

1 Answer 1

1

After Searching a while, I Found This Solution

for Using Laravel Factory for Normal Objects ( Like My Ip Class Which is not extends Model ) you should copy Below codes to your Object ( in my example Ip class )

    /**
     * Ip constructor.
     * @param array $attributes
     */
    public function __construct($attributes = [])
    {
        foreach ($attributes as $key => $value) {
            $this->$key = $value;
        }
    }
    
    
        /**
     * Create a new Eloquent Collection instance.
     *
     * @param  array  $models
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function newCollection(array $models = [])
    {
        return new Collection($models);
    }

now you can use Laravel Factory Like always ( which is exist in laravel document )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.