• 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 if in_array() how to get the key as well?

Laravel Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) on a sub query with "ROW_NUMBER() OVER PARTITION"

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

xdebug 3 not working in ubuntu 20.04 with docker

How to increase the PHP upload limits [duplicate]

How to keep value of selected value after form submission?

PHPExcel_IOFactory::createWriter causes wrong behaviour

How can i hide dt if dd got empty value

Convert date and time to Jalali in Laravel

How to add a heading in between checkout fields of WooCommerce

how to retrieve the first and last instance of a row in pdo dataset

how to get the header value, if we don't know the value because the value is random from the server

Send POST data via raw JSON with Postman

Input and output values for php into the browser?

Log file is not being written in Laravel 5.5

About Contact Privacy policy Terms and conditions