• 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

Make survey at laravel 5.4 and MySQL

How to insert data from multiple select dropdown values into database?

Store multiple fields in JSON column (Nova CMS)

Spam Filter in Contact Form

Facebook PHP SDK - will not logout properly

SMTP ERROR: MAIL FROM command failed: 530 5.7.0 Must issue a STARTTLS command first when using PHPMailer

Eloquent insert id with sequence next value

Using spatie/media-library, how to rename a collection of uploaded files?

Laravel PHP: multiple project run at the same time [closed]

How to generate QR CODE for dynamic generating link and logo using Simple QrCode in laravel?

Assets not referencing to public folder (Laravel)

How can I format this number correctly using PHP?

Laravel query builder binding parameters more than once

Method IlluminateAuthRequestGuard::logout does not exist Laravel Passport

PHPUnit gives error: Target [IlluminateContractsViewFactory] is not instantiable

About Contact Privacy policy Terms and conditions