• 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

Which is faster php date functions or carbon?

How to upload mpdf file after generating to s3 bucket in php

Symfony there are no commands defined in the "make" namespace

XDebug not working in VScode for php debugging

How to validate Envato Purchase Code in PHP

Elastic Beanstalk with Laravel Envoy

How to install php yaml on CentOs?

zsh: command not found: php

Laravel 5.5 change unauthenticated login redirect url

Laravel Excel::store file empty after stored

Querying only one row from a one to many relationship laravel

PHP | "The requested PHP extension bcmath is missing from your system."

How to get page number on dompdf PDF when using "view"

Display the WooCommerce product price inside a custom function

How to loop sql table data inside a html table

About Contact Privacy policy Terms and conditions