• 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

mysql slow on updates for a well optimized query

How do I upload a laravel project on cPanel shared hosting?

Batch request Google Calendar php API

Method IlluminateAuthRequestGuard::logout does not exist Laravel Passport

php curl_exec() Connection refused when retrieving a remote image

PHPMyAdmin not working as intended at remote server. Error 500, $respond not found

Clients authentication and user authentication with laravel

Dynamic dropdown Ajax PHP request

How to convert time from one timezone to another in PHP

Connecting an HTML webpage to a SQL Server

Failed user login on production server using Symfony framework (Authentication request could not be processed due to...)

IlluminateDatabaseQueryException could not find driver [duplicate]

Laravel 8 factory state afterCreating

Results page in other window

How to remove index.php and index from the URL using htaccess | PHP

About Contact Privacy policy Terms and conditions