Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use trait in Laravel model factory?

Tags:

php

laravel

If Model factroy is as below then how to use Trait getData() in here?

This code not worked.

<?php

use App\Working;
use App\Traits\Calculate;

...

    $factory->define(App\Working::class, function  (Faker\Generator $faker) {        
       ...    
       $getData = $this->getData();        
       ...     
       return['get_data' => $getData];

    }

Error message:

Symfony\Component\Debug\Exception\FatalThrowableError : Call to undefined method Illuminate\Database\Eloquent\Factory::getData()

Exception trace:

1 Illuminate\Database\Eloquent\Factory::{closure}(Object(Faker\Generator), []) G:\test\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:263

2 call_user_func(Object(Closure), Object(Faker\Generator), []) G:\test\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:263

like image 808
JsWizard Avatar asked Jun 27 '18 09:06

JsWizard


People also ask

How are traits used in laravel models?

Using Traits in Laravel We have to evoke a new controller where we can assimilate the Traits, which will fetch all the records from the database. Generate a new controller using the below command. Insert following code in app/Http/Controllers/StudentController. php file to set the newly created Trait.

What is difference between helper and trait in laravel?

Helpers functions are used by the framework itself and also, you can use them in your own applications as per your convenience. Also, you can create custom helper functions. Traits are commonly utilized code. You can write a trait and can utilize it anywhere you desire.

What is use Hasfactory in laravel?

It is a trait that links a Eloquent model to a model factory. Factories are normally used in testing when wanted test-data for a specific model. You can read more about factories in Laravel here: https://laravel.com/docs/9.x/database-testing#model-factories and here: https://laravel.com/docs/9.x/eloquent-factories.

Where are traits located in laravel?

Like traits for models should go in App/Models/Traits, for controllers in App/Http/Controllers/Traits and so.


1 Answers

Probably not what you're looking for but here goes:

You can create an inline class that defines a function that returns the real function you want to use. That inline class can use a trait.

Note: Anonymous classes were added in PHP 7

<?php

use App\Working;
use App\Traits\Calculate;

// ...
$factory->define(App\Working::class, (new class {
     use Calculate;
     public function generatorFunction() {
         return function (Faker\Generator $faker) {
             // ...
             $getData = $this->getData();
             // ...
             return ['get_data' => $getData];

         };
     }
 })->generatorFunction());

Update:

Since Laravel 8 model factories are now classes instead of functions so can use traits making the above solution unnecessary.

like image 120
apokryfos Avatar answered Oct 15 '22 08:10

apokryfos