• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

Laravel 8 Multiple Relationships for Factory

phplaravellaravel-factory


PHP Snippet 1:

  public function definition()
  {
    return [
        'post_id' => function () {
            return Post::factory()->create()->id;
        },

        .....
    ];
}

PHP Snippet 2:

Link::factory()->count(3)->create();//Create 3 links with 3 new posts

PHP Snippet 3:

Link::factory()->count(3)->create(['post_id' => Post::first()->id]); //create 3 links and 0 new posts

PHP Snippet 4:

\App\Models\Category::factory(10)
->has(Product::factory()->count(10), 'products')
->create();

Related Snippets

php echo xml documents with header

(Laravel) How to delete multiple models including optional relationships?

How to prevent phpmailer sending embedded image as an attachment on Gmail?

Apply session id from request header

How to redirect to another page after n seconds in wordpress without using javascript?

Can I write PHP code across multiple lines per statement?

Need to display only array value in JSON output

nginx err_connection_refused, can anyone help me?

Laravel 5.2: Unable to locate factory with name [default]

Laravel Schedule not sending email

phpMyAdmin ERROR: mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'@'localhost' (using password: NO)

Symfony process run throws exception - executing on command line works

Star and Half Star Rating in Laravel

Star rating with half star support

Attempted to call an undefined method named "get" of class "MailController"

About Contact Privacy policy Terms and conditions