@@ -243,7 +243,7 @@ protected function getPropertiesFromTable($model)
243
243
$ name !== $ model ::UPDATED_AT
244
244
) {
245
245
if (!method_exists ($ model , 'getDeletedAtColumn ' ) || (method_exists ($ model , 'getDeletedAtColumn ' ) && $ name !== $ model ->getDeletedAtColumn ())) {
246
- $ this ->setProperty ($ name , $ type , $ table );
246
+ $ this ->setProperty ($ model , $ name , $ type , $ table );
247
247
}
248
248
}
249
249
}
@@ -277,7 +277,7 @@ protected function getPropertiesFromMethods($model)
277
277
if ($ pos = stripos ($ code , $ search )) {
278
278
$ relationObj = $ model ->$ method ();
279
279
if ($ relationObj instanceof Relation) {
280
- $ this ->setProperty ($ relationObj ->getForeignKeyName (), 'factory( ' . get_class ($ relationObj ->getRelated ()) . '::class) ' );
280
+ $ this ->setProperty ($ model , $ relationObj ->getForeignKeyName (), 'factory( ' . get_class ($ relationObj ->getRelated ()) . '::class) ' );
281
281
}
282
282
}
283
283
}
@@ -289,31 +289,14 @@ protected function getPropertiesFromMethods($model)
289
289
* @param string $name
290
290
* @param string|null $type
291
291
*/
292
- protected function setProperty ($ name , $ type = null , $ table = null )
292
+ protected function setProperty ($ model , $ name , $ type = null , $ table = null )
293
293
{
294
294
if ($ type !== null && Str::startsWith ($ type , 'factory( ' )) {
295
295
$ this ->properties [$ name ] = $ type ;
296
296
297
297
return ;
298
298
}
299
299
300
- $ fakeableTypes = [
301
- 'enum ' => '$faker->randomElement( ' . $ this ->enumValues ($ table , $ name ) . ') ' ,
302
- 'string ' => '$faker->word ' ,
303
- 'text ' => '$faker->text ' ,
304
- 'date ' => '$faker->date() ' ,
305
- 'time ' => '$faker->time() ' ,
306
- 'guid ' => '$faker->word ' ,
307
- 'datetimetz ' => '$faker->dateTime() ' ,
308
- 'datetime ' => '$faker->dateTime() ' ,
309
- 'integer ' => '$faker->randomNumber() ' ,
310
- 'bigint ' => '$faker->randomNumber() ' ,
311
- 'smallint ' => '$faker->randomNumber() ' ,
312
- 'decimal ' => '$faker->randomFloat() ' ,
313
- 'float ' => '$faker->randomFloat() ' ,
314
- 'boolean ' => '$faker->boolean '
315
- ];
316
-
317
300
$ fakeableNames = [
318
301
'city ' => '$faker->city ' ,
319
302
'company ' => '$faker->company ' ,
@@ -354,6 +337,31 @@ protected function setProperty($name, $type = null, $table = null)
354
337
return ;
355
338
}
356
339
340
+ $ enumValues = $ this ->enumValues ($ model , $ table , $ name );
341
+
342
+ $ fakeableTypes = [
343
+ 'enum ' => '$faker->randomElement( ' . $ enumValues . ') ' ,
344
+ 'string ' => '$faker->word ' ,
345
+ 'text ' => '$faker->text ' ,
346
+ 'date ' => '$faker->date() ' ,
347
+ 'time ' => '$faker->time() ' ,
348
+ 'guid ' => '$faker->word ' ,
349
+ 'datetimetz ' => '$faker->dateTime() ' ,
350
+ 'datetime ' => '$faker->dateTime() ' ,
351
+ 'integer ' => '$faker->randomNumber() ' ,
352
+ 'bigint ' => '$faker->randomNumber() ' ,
353
+ 'smallint ' => '$faker->randomNumber() ' ,
354
+ 'decimal ' => '$faker->randomFloat() ' ,
355
+ 'float ' => '$faker->randomFloat() ' ,
356
+ 'boolean ' => '$faker->boolean '
357
+ ];
358
+
359
+ if ($ enumValues !== '[] ' ) {
360
+ $ this ->properties [$ name ] = $ fakeableTypes ['enum ' ];
361
+
362
+ return ;
363
+ }
364
+
357
365
if (isset ($ fakeableTypes [$ type ])) {
358
366
$ this ->properties [$ name ] = $ fakeableTypes [$ type ];
359
367
@@ -363,19 +371,42 @@ protected function setProperty($name, $type = null, $table = null)
363
371
$ this ->properties [$ name ] = '$faker->word ' ;
364
372
}
365
373
366
- public static function enumValues ($ table , $ name )
374
+ public function enumValues ($ model , $ table , $ name )
367
375
{
368
376
if ($ table === null ) {
369
377
return "[] " ;
370
378
}
371
379
372
- $ type = DB ::select (DB ::raw ('SHOW COLUMNS FROM ' . $ table . ' WHERE Field = " ' . $ name . '" ' ))[0 ]->Type ;
380
+ $ driver = $ model ->getConnection ()->getDriverName ();
381
+ $ values = null ;
382
+
383
+ if ($ driver === 'mysql ' ) {
384
+ $ type = DB ::select (DB ::raw ('SHOW COLUMNS FROM ' . $ table . ' WHERE Field = " ' . $ name . '" ' ))[0 ]->Type ;
373
385
374
- preg_match_all ("/'([^']+)'/ " , $ type , $ matches );
386
+ preg_match_all ("/'([^']+)'/ " , $ type , $ matches );
375
387
376
- $ values = isset ($ matches [1 ]) ? $ matches [1 ] : array ();
388
+ $ values = isset ($ matches [1 ]) ? $ matches [1 ] : array ();
389
+
390
+ return "[' " . implode ("', ' " , $ values ) . "'] " ;
391
+ } else if ($ driver === 'pgsql ' ) {
392
+ $ types = DB ::select (DB ::raw ("
393
+ select matches[1]
394
+ from pg_constraint, regexp_matches(consrc, '''(.+?)''', 'g') matches
395
+ where contype = 'c'
396
+ and conname = ' {$ table }_ {$ name }_check'
397
+ and conrelid = 'public. {$ table }'::regclass;
398
+ " ));
399
+
400
+ $ values = array ();
401
+
402
+ foreach ($ types as $ type ){
403
+ $ values [] = $ type ->matches ;
404
+ }
405
+ }
377
406
378
- return "[' " . implode ("', ' " , $ values ) . "'] " ;
407
+ return $ values
408
+ ? "[' " . implode ("', ' " , $ values ) . "'] "
409
+ : "[] " ;
379
410
}
380
411
381
412
0 commit comments