2016年3月23日 星期三

以生成測試數據感受到Faktory +法克爾在PHP良好

介紹

如果你正在尋找一種方式來生成測試數據,最終Faktory Tasu 法克爾試圖通過使用兩個庫來創建。這一次,我試著總結的過程。

關於庫中使用

Faktory

這是對象創建一個測試庫。什麼被稱為它的Rails等,和FactoryGirl我認為是否有一個,但Faktory被個人覺得在它已被證實,並已使非常接近README.md。FactoryGirl-PHP,我也是在摩洛那傢伙,那更Faktory的有README.md被濃墨重彩島,自上次提交接近選擇Faktory雖然星數較低這是。

騙子

它是為測試的假數據創建的庫。日本的數據創建以及數據的英語也可以很方便地進行它從官方文檔的本地化發現,這裡也README.md決定嘗試從你一直在精心編寫的使用。順便說一句,星數也搞砸了很多!太神奇了!

準備

這兩個庫還可以做酥和安裝的作曲家。這是件好事,方便。
#法克爾安裝
$作曲要求Fzaninotto /法克爾

版本1.5 ^使用對於 Fzaninotto /法克爾
./composer.json已更新
裝載作曲家儲存庫包信息
依賴更新含要求-dev的 
  -安裝Fzaninotto /法克爾 V1.5.0 
    下載:100%

寫鎖定文件
生成自動加載的文件
排名第Faktory安裝
$作曲要求Adamwathan / faktory

版本0.4.1 ^使用對於 Adamwathan / faktory
./composer.json已創建
裝載作曲家儲存庫包信息
依賴更新含要求-dev的 
  -安裝Adamwathan / faktory  V0.4.1 
    下載:100%

寫鎖定文件
生成自動加載的文件

盡量讓在Faktory測試對象

首先,我想創造單獨使用Faktory測試對象。
下面的代碼創建一個名為person.php粘貼文件。
需要 './Vendor/autoload.Php“ ;

使用 AdamWathan \ Faktory \ Faktory ;

  {}

faktory $  =   Faktory ; 
$ faktory - > 定義'人'  函數 $ F  { 
  $ F - > 名稱 =  '肯尼亞山口' ; 
});

人$  =  $ faktory - > 構建'人' ); 
後續代碼var_dump $人);
讓我們運行。
我認為,可以理解的是正確地以下述方式被產生的測試對象。
$腓Person.Php

對象#7(1){ 
  [ “名稱” ] = > 15  “肯尼亞山口” 
}

嘗試法克爾的組合

所以,我們想生成測試數據作為最後的與法克爾冠軍組合良好的手感。讓我們修改先前person.php使用以下內容。
<?PHP的
要求 './Vendor/autoload.Php“ ;

使用 AdamWathan \ Faktory \ Faktory ;

  {}

faktory $  =   Faktory ; 
$ faktory - > 定義'人'  函數 $ F  { 
  $ F - > 名稱 =  '' ; 
});

名稱$  =  []; 
$法克爾 =  法克爾\廠:: 創建“Ja_JP表示' ); 
對於 $ I  =  0 ;  $ I  <  10 ;  $ I ++  { 
  $名稱[]  =  $法克爾- > 名稱; 
}

人$  =  $ faktory - > BuildMany '人'  10  [ 
  '名稱'  =>  $名稱
]);

後續代碼var_dump $人);
讓我們運行。
正確測試對象,如以下我認為它可以看出,已經生成的感覺在偽造數據進入良好。
$腓Person.Php

陣列 10  { 
  [ 0 ] = >
  對象排名第26(1){ 
    [ “名稱” ] = > 13  “康弘伊藤” 
  } 
  [ 1 ] = >
  對象排名第27(1){ 
    [ “名稱” ] = > 10  “麥凹榻” 
  } 
  [ 2 ] = >
  對象排名第28(1){ 
    [ “名稱” ] = > 13  “遙三宅” 
  } 
  [ 3 ] = >
  對象排名第29(1){ 
    [ “名稱” ] = > 13  “直子烏諾” 
  } 
  [ 4 ] = >
  對象排名第30(1){ 
    [ “名稱” ] = > 13  “大垣翔太” 
  } 
  [ 5 ] = >
  對象排名第31(1){ 
    [ “名稱” ] = > 10  “君坂” 
  } 
  [ 6 ] = >
  對象排名第32(1){ 
    [ “名稱” ] = > 10  “麗齋藤” 
  } 
  [ 7 ] = >
  對象#33(1){ 
    [ “名稱” ] = > 16  “小百合三宅” 
  } 
  [ 8 ] = >
  對象排名第34(1){ 
    [ “名稱” ] = > 10  “齋藤學” 
  } 
  [ 9 ] = >
  對象#35(1){ 
    [ “名稱” ] = > 13  “遙吉田” 
  } 
}

在結束

由於Faktory也法克爾才剛剛開始被感動,因為它往往還是自己不知道,我想繼續摸。另外,我如果你能告訴我,如果有一種東西叫高興:“我也可以這樣的事情,”和“這是標準的做法是使用這個庫。”

reference : http://qiita.com/ken880guchi/items/d97d5ea4506e8fcb4118

Using Propel With MSSQL Server

Windows

Windows has 4 different driver implementations that could be used. In order of support:pdo_sqlsrvpdo_sybasepdo_mssql, and pdo_odbc.
pdo_dblib can be built against either FreeTDS (pdo_sybase) or MS SQL Server (pdo_mssql) dblib implementations. The driver is not a complete PDO driver implementation and lacks support for transactions or driver attributes.

pdo_sqlsrv

This is a driver released in August 2010 by Microsoft for interfacing with MS SQL Server. This is a very complete PDO driver implementation and will provide the best results when using Propel on the Windows platform. It does not return blobs as a resource right now but this feature will hopefully be added in a future release. There is also a bug with setting blob values to null that Propel has a workaround for.
Sample dsn's for pdo_sqlsrv:
sqlsrv:server=localhost\SQLEXPRESS;Database=propel
sqlsrv:server=localhost\SQLEXPRESS,1433;Database=propel sqlsrv:server=localhost,1433;Database=propel
Sample runtime-conf.xml for pdo_sqlsrv:
 id="bookstore">
  sqlsrv
DebugPDO sqlsrv:server=localhost,1433;Database=propel username password
Sample build.properties for pdo_sqlsrv:
propel.database = sqlsrv
propel.database.url = sqlsrv:server=127.0.0.1,1433;Database=propel

pdo_sybase

When built against FreeTDS dblib it will be called pdo_sybase. This requires properly setting up the FreeTDS freetds.conf andlocales.conf. There is a workaround for the lack of transactions support in the pdo_dblib driver by using MssqlDebugPDO orMssqlPropelPDO classes.
c:\freetds.conf
[global]
  client charset = UTF-8
  tds version = 8.0
  text size = 20971520
c:\locales.conf
[default]
  date format = %Y-%m-%d %H:%M:%S.%z
Sample dsn's for pdo_sybase:
sybase:host=localhost\SQLEXPRESS;dbname=propel
sybase:host=localhost\SQLEXPRESS:1433;dbname=propel sybase:host=localhost:1433;dbname=propel
Sample runtime-conf.xml for pdo_sybase:
 id="bookstore">
  mssql
MssqlDebugPDO sybase:host=localhost:1433;dbname=propel username password
Sample build.properties for pdo_sybase:
propel.database = mssql
propel.database.url = sybase:host=localhost:1433;dbname=propel

pdo_mssql

When built against MS SQL Server dblib the driver will be called pdo_mssql. It is not recommended to use the pdo_mssql driver because it strips blobs of single quotes when retrieving from the database and will not return blobs or clobs longer that 8192 characters. The dsn differs from pdo_sybase in that it uses a comma between the server and port number instead of a colon and mssql instead of sybase for the driver name.
Sample dsn's for pdo_mssql:
mssql:host=localhost\SQLEXPRESS;dbname=propel
mssql:host=localhost\SQLEXPRESS,1433;dbname=propel mssql:host=localhost,1433;dbname=propel

pdo_odbc

Currently pdo_odbc cannot be used to access MSSQL with propel because of a long standing bug with the MS SQL Server ODBC Client. Last update on 8/3/2010 was that it would be resolved in a future release of the SQL Server Native Access Client. This bug is related to two php bugs (Bug #44643 and Bug #36561)

Linux

Linux has 2 driver implementations that could be used: pdo_dblib, and pdo_obdc.

pdo_dblib

pdo_dblib is built against the FreeTDS dblib implementation. The driver is not a complete PDO driver implementation and lacks support for transactions or driver attributes. This requires properly setting up the FreeTDS freetds.conf and locales.conf. There is a workaround for the lack of transactions support in the pdo_dblib driver by using MssqlDebugPDO or MssqlPropelPDO classes.
Redhat: /etc/freetds.conf Ubuntu: /etc/freetds/freetds.conf
[global]
  client charset = UTF-8
  tds version = 8.0
  text size = 20971520
Redhat: /etc/locales.conf Ubuntu: /etc/freetds/locales.conf
[default]
  date format = %Y-%m-%d %H:%M:%S.%z
Sample dsn's for pdo_dblib:
dblib:host=localhost\SQLEXPRESS;dbname=propel
dblib:host=localhost\SQLEXPRESS:1433;dbname=propel dblib:host=localhost:1433;dbname=propel
Sample runtime-conf.xml for pdo_dblib:
 id="bookstore">
  mssql
MssqlDebugPDO dblib:host=localhost:1433;dbname=propel username password
Sample build.properties for pdo_dblib:
propel.database = mssql
propel.database.url = dblib:host=localhost:1433;dbname=propel

pdo_odbc

pdo_odbc using UnixODBC and FreeTDS. This should be supported in propel but with ubuntu 10.04 and php 5.2.x any statement binding causes apache to segfault so I have not been able to test it further. If anyone has any additional experience with this please post information to the propel development group. If you would like to experiment there are some instructions you can follow here for getting it setup on ubuntu.


reference : http://propelorm.org/Propel/cookbook/using-mssql-server.html

[PHP] PHP 5.4 (for Windows)之後版本連接Microsoft SQL Server

Screen Shot 2013-08-26 at 12.50.45 PM  
PHP要怎麼連Microsoft SQL Server?網路上教學很多
但很多是舊版,這是新版的做法
只有Windows平台受影響

敝人愛用XAMPP所以就用這套講起
它好像自從其中一個版號之後...
XAMPP for Windows把原本mssql的連接方式抽掉了
只能用Native Client的連接方式

而其它平台則不受影響
-------
簡單講一下差異...
Screen Shot 2013-08-26 at 8.28.07 PM  
這裡有三個流程圖...
Mac / Linux 是看最右邊的方式,底層採用FreeTDS連接SQL Server
Windows則有二種方式連接,中間的是舊版做法,左邊的是新版做法

簡單來說...就是換Library了啦!
SQLSRV的話,是微軟提供&維護的Driver,

在Microsoft官網有完整的API Reference
http://msdn.microsoft.com/en-us/library/cc296152.aspx
當然,php官方也Mirror了一份
http://php.net/manual/en/book.sqlsrv.php

所以這篇,主要教怎做這個更換Library的動作


-------
剛開始,你在Configure Command裡面看到類似 "--without-mssql" "--without-pdo-mssql"的字眼就不要太緊張
因為它換模組名稱

-------
敝人愛用XAMPP所以就用這套講起
首先你需要裝一個Native Client
依照這個網址指示
http://msdn.microsoft.com/en-us/library/cc296170.aspx
Screen Shot 2013-08-26 at 1.08.39 PM  
這就看你的SQL Server版本不同平台不同裝不一樣的

我的話,用Microsoft SQL Server 2008,系統是64位元的
所以裝這個
http://go.microsoft.com/fwlink/?LinkID=188401&clcid=0x409
(它有特別提到,系統如果是64位元的SQL Server就一定要裝64位元的)
----------------
到這網址
http://www.microsoft.com/en-us/download/details.aspx?id=20098
抓檔案 

但這也要看PHP版本,以我的版本XAMPP 1.8.2 for Windows來說
Screen Shot 2013-08-26 at 1.16.48 PM  
我的圖面上很清楚看到的是php 5.4
然後比照php資料夾裡的dll是Thread Safe 還是 non-Thread Safe的版本

Screen Shot 2013-08-26 at 1.11.14 PM  


以我的版本XAMPP 1.8.2 for Windows來說
我在C:\xampp\php\裡面
找不到php5.dll但有php5ts.dll
所以我知道我的版本是Thread Safe版本
SQLSRV30.EXE檔案,用7-zip等程式直接解壓縮(本身執行檔是壞的,不用在雙點了)
就會得到
php_sqlsrv_54_ts.dll
php_pdo_sqlsrv_54_ts.dll
這二個檔案

把它複製到C:\xampp\php\ext\ 裡面
然後手動在php.ini裡面加上
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
然後重開Apache
-----------------
看phpinfo(),找sqlsrv就會看到類似以下畫面
Screen Shot 2013-08-26 at 12.49.33 PM  
就完成了

因為模組有變更,程式碼也變更
所以以下這段在Windows底下就棄用了喔(Mac / Linux的話不受影響,還是用這一段)
$conn = mssql_connect($serverName, $DBUsername, $DBPassword)
or die("Couldn't connect to SQL Server on $myServer");
if (!$conn) {
die('Something went wrong while connecting to MSSQL');
}
?>

改成新版的寫法 
$serverName = "localhost"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.
";
}else{
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}
?>

網路找了個半天,終於解出來問題了
之後來測試一下連線

至於更詳細sqlsrv和mssql使用上的差異
可以參考這一篇
http://blogs.msdn.com/b/brian_swan/archive/2010/03/10/mssql-vs-sqlsrv-what-s-the-difference-part-2.aspx

--------------
參考資料:
整個模組Reference
http://php.net/manual/en/book.sqlsrv.php
http://msdn.microsoft.com/en-us/library/cc296152.aspx
安裝準備
http://msdn.microsoft.com/en-us/library/cc296170.aspx
官方的範例
http://msdn.microsoft.com/en-us/library/cc296196(v=sql.105).aspx

http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx
http://blogs.msdn.com/b/brian_swan/archive/2010/03/10/mssql-vs-sqlsrv-what-s-the-difference-part-2.as
http://www.apachefriends.org/f/viewtopic.php?f=16&t=49670&p=191610&hilit=SQLSRV#p191610


reference : http://j796160836.pixnet.net/blog/post/32652509-%5Bphp%5D-php-5.4-(for-windows)%E4%B9%8B%E5%BE%8C%E7%89%88%E6%9C%AC%E9%80%A3%E6%8E%A5microsoft-sql-s

2016年3月22日 星期二

Eloquent factory girl

Faktory is a tool for easily building test objects ala FactoryGirl, but for PHP. It's still in it's early stages, but give it a go if you're interested, and open issues for the features it's missing that you think are really important.

Installing with Composer

You can install this package via Composer on the command line from your project's root:
composer require adamwathan/faktory

Laravel 4

If you are using Laravel 4, you can get started very quickly by registering the included service provider.
Modify the providers array in app/config/app.php to include the FaktoryServiceProvider:
'providers' => array(
        //...
        'AdamWathan\Faktory\FaktoryServiceProvider'
    ),
Add the Faktory facade to the aliases array in app/config/app.php:
'aliases' => array(
        //...
        'Faktory' => 'AdamWathan\Faktory\Facades\Faktory'
    ),
You can now start using Faktory by calling methods directly on the Faktory facade:
Faktory::define('User', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
});

Outside of Laravel 4

To use outside of Laravel 4, just instantiate a new Faktory. Make sure you register this as a singleton in your favorite dependency injection container, since you probably want to be using the same instance everywhere.
$faktory = new AdamWathan\Faktory\Faktory;
Note: When using outside of Laravel 4 and not having access to the Faktory facade, you will need to make sure you use your $faktory instance in any nested closures that need to generate other objects. Sucks but that's PHP.

Using Faktory

Defining factories

Define factories anywhere you want.
In Laravel 4, I've been creating a factories.php file in my tests directory and adding it to app/bootstrap/testing.php like so:
// app/bootstrap/testing.php
require app_path().'/tests/factories.php';

Basic definition

The most basic factory definition requires a class name and a closure that defines the default attributes for the factory. This will define a factory named after that class that generates instances of that same class.
Faktory::define('Album', function ($f) {
    $f->name = 'Diary of a madman';
    $f->release_date = new DateTime('1981-11-07');
});

Faktory::define('Song', function ($f) {
    $f->name = 'Over the mountain';
    $f->length = 271;
});

Using factories

Once you have your factories defined, you can very easily generate objects for your tests.
Objects can be generated using one of two different build strategies.
  • build creates objects in memory only, without persisting them.
  • create creates objects and persists them to whatever database you have set up in your testing environment.
Note: The create strategy is meant for Laravel 4's Eloquent ORM, but as long as your objects implement a save()method and a getKey() method to retrieve the object's ID, it should work outside of Eloquent.
To generate an object, simply call build or create and pass in the name of the factory you want to generate the object from.
// Returns an Album object with the default attribute values
$album = Faktory::build('Album');
$album->name;
// 'Diary of a madman'
$album->release_date;
// '1981-11-07'


// Create a basic instance and persist it to
// the database
$album = Faktory::create('Album');
$album->id
// 1

Overriding attributes

The real benefit of using these factories appears when you are writing a test that requires your objects to satisfy some precondition, but you don't really care about the rest of the attributes.
You can specify the values you need for the attributes that matter for the test, and let the factory handle filling out the attributes you don't care about with default data so that the object is in a valid state.
If you just need to change some simple attributes to static values, you can just pass an array of attribute overrides as a second argument:
// Create an instance and override some properties
$album = Faktory::build('Album', ['name' => 'Bark at the moon']),
]);

$album->name;
// 'Bark at the moon'
$album->release_date;
// '1981-11-07'
If you need to do something trickier, you can pass in a closure that provides all of the same functionality you get when actually defining the factory. This is most useful when working with relationships:
// Create an instance and override some properties
$album = Faktory::build('Album', function ($album) {
    $album->name => 'Bark at the moon';
    $album->songs->quantity(4)->attributes(['length' => 267]);
});

$album->name;
// 'Bark at the moon'
$album->songs->count();
// 4
$album->songs[0]->length;
// 267

Named factories

Factories can also be given a name, so that you can define multiple factories for the same class that generate objects in different predefined states.
To define a named factory, pass the name as the second parameter, and move the callback to the third parameter.
Faktory::define('Album', 'album_with_copies_sold', function ($f) {
    $f->name = 'Diary of a madman';
    $f->release_date = '1981-11-07';
    $f->copies_sold = 3200000;
});


$album = Faktory::build('album_with_copies_sold');

get_class($album);
// 'Album'
$album->name;
// 'Diary of a madman'
$album->release_date;
// '1981-11-07'
$album->copies_sold;
// 3200000

Factory inheritance

You can create factories that inherit the attributes of an existing factory by nesting the definition. This allows you to define a basic factory, as well as more specific factories underneath it to generate objects in a specific state without having to redeclare the attributes that don't need to change.
Faktory::define('User', 'basic_user', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
    $f->is_admin = false;

    $f->define('admin', function ($f) {
        $f->is_admin = true;
    });
});


$user = Faktory::build('admin');

$user->first_name;
// 'John'
$user->last_name;
// 'Doe'
$user->is_admin;
// true

Lazy attributes

If you don't want an attribute to be evaluated until you try to build an object, you can define that attribute as a closure.
Faktory::define('User', function ($f) {
    $f->username = 'john.doe';

    $f->created_at = function () {
        return new DateTime;
    };
});


$user1 = Faktory::build('User');
$user1->created_at;
// '2014-04-22 14:10:05'

sleep(7);

$user2 = Faktory::build('User');
$user2->created_at;
// '2014-04-22 14:10:12'

Dependent attributes

You can also use lazy attributes to define attributes that depend on other attributes in the factory.
Faktory::define('User', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
    $f->email = function ($f) {
        return "{$f->first_name}.{$f->last_name}@example.com";
    };
});


$user = Faktory::build('User');
$user->first_name;
// 'John'
$user->last_name;
// 'Doe'
$user->email;
// 'John.Doe@example.com'

$user = Faktory::build('User', ['first_name' => 'Bob']);
$user->first_name;
// 'Bob'
$user->last_name;
// 'Doe'
$user->email;
// 'Bob.Doe@example.com'

Unique attributes

Lazy attributes to the rescue again. The closure also takes an autoincrementing integer as it's second parameter, which is really handy for ensuring that a field value is unique.
Faktory::define('User', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
    $f->email = function ($f, $i) {
        return "example{$i}@example.com";
    };
});


$user1 = Faktory::build('User');
$user1->email;
// 'example1@example.com'

$user2 = Faktory::build('User');
$user2->email;
// 'example2@example.com'

Defining relationships

Faktory lets you easily define relationships between objects.
Currently, there is support for belongsTohasOne, and hasMany relationships.

Belongs to

Define a belongsTo relationship by assigning a belongsTo call to an attribute.
belongsTo() takes the name of the factory that should be used to generate the related object as the first argument, the name of the foreign key column as the second argument, and an optional array of override attributes as the third argument.
$faktory->define('Song', 'song_with_album', function ($f) {
    $f->name = 'Concatenation';
    $f->length = 257;
    $f->album = $f->belongsTo('album', 'album_id', [
        'name' => 'Chaosphere',
    ]);
});

$faktory->define('Album', 'album', function ($f) {
    $f->name = 'Destroy Erase Improve';
});


// Build the objects in memory without persisting to the database
$song = Faktory::build('song_with_album');
$song->album;
// object(Album)(
//    'name' => 'Destroy Erase Improve'
// )
$song->album_id;
// NULL


// Save the objects to the database and set up the correct
// foreign key associations
$song = Faktory::create('song_with_album');
$song->album_id;
// 1

Album::find(1);
// object(Album)(
//    'name' => 'Destroy Erase Improve'
// )

Has one

Define a hasOne relationship by assigning a hasOne call to an attribute.
hasOne() takes the name of the factory that should be used to generate the related object as the first argument, the name of the foreign key column (on the related object) as the second argument, and an optional array of override attributes as the third argument.
$faktory->define('User', 'user_with_profile', function ($f) {
    $f->username = 'johndoe';
    $f->password = 'top-secret';
    $f->profile = $f->hasOne('profile', 'user_id');
});

$faktory->define('Profile', 'profile', function ($f) {
    $f->email = 'johndoe@example.com';
});


// Build the objects in memory without persisting to the database
$user = Faktory::build('user_with_profile');
$user->profile;
// object(Profile)(
//    'email' => 'johndoe@example.com'
// )


// Save the objects to the database and set up the correct
// foreign key associations
$user = Faktory::create('user_with_profile');
$user->id;
// 1

Profile::first();
// object(Album)(
//    'user_id' => 1,
//    'email' => 'johndoe@example.com'
// )

Has many

Define a hasMany relationship by assigning a hasMany call to an attribute.
hasMany() takes the name of the factory that should be used to generate the related objects as the first argument, the number of objects to generate as the second argument, the name of the foreign key column (on the related object) as the third argument, and an optional array of override attributes as the final argument.
$faktory->define('Album', 'album_with_songs', function ($f) {
    $f->name = 'Master of Puppets';
    $f->release_date = new DateTime('1986-02-24');
    $f->songs = $f->hasMany('song', 8, 'album_id');
});

$faktory->define('Song', 'song', function ($f) {
    $f->title = 'The Thing That Should Not Be';
    $f->length = 397;
});

Relationships and build strategies

Relationships are handled differently by each build strategy.
When using the build strategy, the related object(s) will be available directly as a property on the base object.
When using the create strategy, the related object(s) will be persisted to the database with the foreign key attribute set to match the ID of the base object, and nothing will actually be set on the actual attribute itself, allowing you to retrieve the related object through the methods you've actually defined in the base object's class.

Overriding relationship attributes

If you need to override attributes on a relationship when building or creating an object, you can do so by manipulating the actual relationship attribute itself.
// Define the factories
$faktory->define('Song', 'song_with_album', function ($f) {
    $f->name = 'Concatenation';
    $f->length = 257;
    $f->album = $f->belongsTo('album', 'album_id');
});
$faktory->define('Album', 'album', function ($f) {
    $f->name = 'Destroy Erase Improve';
    $f->release_date = new DateTime('1995-07-25');
});


// Build a song but override the album name
$song = Faktory::build('song_with_album', function ($song) {
    $song->album->name = 'Chaosphere';
});
$song->album;
// object(Album)(
//    'name' => 'Chaosphere'
// )

// Build a song but override a couple attributes at once
$song = Faktory::build('song_with_album', function ($song) {
    $song->album->attributes([
        'name' => 'Chaosphere',
        'release_date' => new DateTime('1998-11-10'),
    ]);
});
$song->album;
// object(Album)(
//    'name' => 'Chaosphere'
// )
$song->album->release_date;
// '1998-11-10'

Building multiple instances at once

You can use buildMany and createMany to generate multiple objects at once:
// Create multiple instances
$albums = Faktory::buildMany('Album', 5);

// Create multiple instances with some overridden properties
$songs = Faktory::buildMany('Song', 5, [ 'length' => 100 ])
$songs[0]->length;
// 100
// ...
$songs[4]->length;
// 100

// Add a nested relationship where each item is different
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => [
        Faktory::build('Song', [ 'length' => 143 ]),
        Faktory::build('Song', [ 'length' => 251 ]),
        Faktory::build('Song', [ 'length' => 167 ]),
        Faktory::build('Song', [ 'length' => 229 ]),
    ],
]);

// Add a nested relationship where each item shares the same
// properties
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => Faktory::buildMany('Song', 5, [ 'length' => 100 ]
    ),
]);

// Add a nested relationship where each item is different,
// but using buildMany
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => Faktory::buildMany('Song', 4, [
        'length' => [143, 251, 167, 229]
    ]),
]);

// Add a nested relationship using buildMany, but wrap
// it in a collection
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => function () {
        return new Collection(Faktory::buildMany('Song', 4, [
            'length' => [143, 251, 167, 229]
        ]));
    }
]);

reference : https://github.com/adamwathan/faktory

wibiya widget