• 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

Laravel 5.1 Unknown database type enum requested

Not able to override collapsible.js in magento 2

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

MySQL default time format UTC or GMT?

Multiply each value in array using array_map function

TesseractOCR not working for Laravel

Filter array by skipping every nth element from the end

Laravel Unresolvable dependency resolving [Parameter #0 [ <required> $method ]] in class GuzzleHttpPsr7Request

Laravel/docker-compose/redis - Class 'Redis' not found

Issue with laravel eloquent model property

How avoid Moved Permanently The document has moved here

Laravel Displaying image from database

Laravel Database Strict Mode

Method IlluminateAuthRequestGuard::logout does not exist Laravel Passport

PHP cURL (SSL certificate problem: self signed certificate in certificate chain)

About Contact Privacy policy Terms and conditions