Answers for "factory laravel"

PHP
0

create a user using factory laravel

1. Create a factory:
	php artisan make:factory ItemFactory --model=Item
      
Import Illuminate\Database\Eloquent\Factories\HasFactory trait to your model:

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    use HasFactory;

    // ...
}

2. Use it like this:
	
	$item = Item::factory()->make(); // Create a single App\Models\Item instance

	// or

	$items = Item::factory()->count(3)->make(); // Create three App\Models\Item instances

3. Use create method to persist them to the database:

  $item = Item::factory()->create(); // Create a single App\Models\Item instance and persist to the database

  // or

  $items = Item::factory()->count(3)->create(); // Create three App\Models\Item instances and persist to the database
Posted by: Guest on September-20-2020
1

laravel factory relations data

factory(App\User::class, 30)->create()->each(function($user) {

    $entity = factory(App\Entity::class)->make();

    $address = factory(App\Address::class)->create([
        'entity_id' => $entity
    ]);

    $user->entities()->save($entity);
});
Posted by: Guest on December-19-2020
4

laravel factory

php artisan make:factory PostFactory --model=Post
Posted by: Guest on October-15-2020
0

How to make a custom factory in Laravel

<?php

namespace Database\Factories;

use App\Models\Comment;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $post_ids = Post::pluck('id')->toArray();

        return [
            'comment' => $this->faker->text(),
            'post_id' => $post_ids[array_rand($post_ids)]
        ];
    }
}
Posted by: Guest on July-18-2021
0

laravel factory

php artisan tinkerProduct::factory()->count(500)->create()
Posted by: Guest on June-13-2021
0

factory laravel

php artisan make:factory CommentFactory
  
  return [
      'name' => $this->faker->name,
      'text' => $this->faker->text()
    ];

public function run()
{
  Comment::factory()
    ->times(3)
    ->create();
}

php artisan db:seed
Posted by: Guest on October-14-2021

Browse Popular Code Answers by Language