{"id":300653,"date":"2020-03-24T15:00:16","date_gmt":"2020-03-24T15:00:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=300653"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=300653","title":{"rendered":"\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0445\u0438\u0442\u0440\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 Laravel \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Model Factories"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/493926\/\">\n<h4 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h4>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Laravel \u0432\u0435\u0440\u0441\u0438\u0438 \u0432\u044b\u0448\u0435 6 \u0438 \u0445\u043e\u0442\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u0435\u0441\u0442\u044b. <\/p>\n<p>  <\/p>\n<p><strong>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043e \u043d\u0438\u0436\u0435:<\/strong><\/p>\n<p>  <\/p>\n<ol>\n<li><a href=\"#start\">\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438<\/a><\/li>\n<li><a href=\"#app\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/a><\/li>\n<li><a href=\"#entities\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/a><\/li>\n<li><a href=\"#tests\">\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432<\/a><\/li>\n<li><a href=\"#problem\">\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/a><\/li>\n<li><a href=\"#solution\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/a><\/li>\n<\/ol>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h4 id=\"anchorstartanchoropisanie-predmetnoy-oblasti\"><a name=\"start\"><\/a>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438<\/h4>\n<p>  <\/p>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442-\u043c\u0430\u0433\u0430\u0437\u0438\u043d, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043d\u0435\u043a\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u043a\u0438\u0439 \u0437\u0430\u043a\u0430\u0437. \u0418\u0437 \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c, \u0447\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0437\u0430\u043a\u0430\u0437 \u0438 \u0442\u043e\u0432\u0430\u0440\u044b. \u041c\u0435\u0436\u0434\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0438 \u0437\u0430\u043a\u0430\u0437\u043e\u043c \u0441\u0432\u044f\u0437\u044c \u043e\u0434\u0438\u043d-\u043a\u043e-\u043c\u043d\u043e\u0433\u0438\u043c, \u0442. \u0435. \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0437\u0430\u043a\u0430\u0437\u043e\u0432, \u0430 \u0443 \u0437\u0430\u043a\u0430\u0437\u0430 \u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c (\u0434\u043b\u044f \u0437\u0430\u043a\u0430\u0437\u0430 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e). \u041c\u0435\u0436\u0434\u0443 \u0437\u0430\u043a\u0430\u0437\u043e\u043c \u0438 \u0442\u043e\u0432\u0430\u0440\u0430\u043c\u0438 \u0441\u0432\u044f\u0437\u044c \u043c\u043d\u043e\u0433\u0438\u0435-\u043a\u043e-\u043c\u043d\u043e\u0433\u0438\u043c, \u0442. \u043a. \u0442\u043e\u0432\u0430\u0440 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u043a\u0430\u0437\u0430\u0445 \u0438 \u0437\u0430\u043a\u0430\u0437 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0438\u0437 \u043c\u043d\u043e\u0433\u0438\u0445 \u0442\u043e\u0432\u0430\u0440\u043e\u0432. \u0414\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0442\u043e\u0432\u0430\u0440\u044b \u0438 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u0445 \u0438 \u0437\u0430\u043a\u0430\u0437\u0430\u0445. <\/p>\n<p>  <\/p>\n<h4 id=\"anchorappanchorsozdanie-prilozheniya\"><a name=\"app\"><\/a>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h4>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 Laravel \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/laravel.com\/docs\/7.x#installing-laravel\">\u043f\u0430\u043a\u0435\u0442-\u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/a>. \u041f\u043e\u0441\u043b\u0435 \u0435\u0433\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0443\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u043a\u043e\u043c\u0430\u043d\u0434\u0443: <\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">laravel new shop<\/code><\/pre>\n<p>  <\/p>\n<h4 id=\"anchorentitiesanchorsozdanie-suschnostey\"><a name=\"entities\"><\/a>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/h4>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0431\u044b\u043b\u043e \u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0432\u044b\u0448\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u0432\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u2014 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0437\u0430\u043a\u0430\u0437. \u0422\u0430\u043a \u043a\u0430\u043a Laravel \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u0433\u043e\u0442\u043e\u0432\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0442\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0437\u0430\u043a\u0430\u0437\u0430. \u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0437\u0430\u043a\u0430\u0437\u0430, \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0411\u0414 \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432. \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0441\u0435\u0433\u043e \u044d\u0442\u043e\u0433\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">php artisan make:model Order -m -f<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0444\u0430\u0439\u043b \u043c\u043e\u0434\u0435\u043b\u0438 \u0432 App\/, \u0444\u0430\u0439\u043b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0432 \u043f\u0430\u043f\u043a\u0435 database\/migrations\/ \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u0432 database\/factories\/. <\/p>\n<p>  <\/p>\n<p><strong>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438<\/strong>. \u0412 \u0437\u0430\u043a\u0430\u0437\u0435 \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043d\u043e \u043c\u044b \u043e\u0431\u043e\u0439\u0434\u0451\u043c\u0441\u044f \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u043e\u0439 \u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0441 \u0432\u043d\u0435\u0448\u043d\u0438\u043c \u043a\u043b\u044e\u0447\u043e\u043c \u0438 \u0442\u0430\u0439\u043c\u0441\u0442\u0435\u043c\u043f\u0430\u043c\u0438. \u0414\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043a\u043e\u0435: <\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  use Illuminate\\Database\\Migrations\\Migration; use Illuminate\\Database\\Schema\\Blueprint; use Illuminate\\Support\\Facades\\Schema;  class CreateOrdersTable extends Migration {     \/**      * Run the migrations.      *      * @return void      *\/     public function up()     {         Schema::create('orders', function (Blueprint $table) {             $table-&gt;id();             $table-&gt;unsignedBigInteger('user_id');             $table-&gt;timestamps();              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')                 -&gt;onDelete('cascade');         });     }      \/**      * Reverse the migrations.      *      * @return void      *\/     public function down()     {         Schema::dropIfExists('orders');     } }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u043f\u0435\u0440\u044c \u043a \u043c\u043e\u0434\u0435\u043b\u0438<\/strong>. \u0417\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e fillable \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c <a href=\"https:\/\/laravel.com\/docs\/7.x\/eloquent-relationships#one-to-one\">\u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d<\/a> \u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e: <\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace App;  use Illuminate\\Database\\Eloquent\\Model; use Illuminate\\Database\\Eloquent\\Relations\\BelongsTo;  class Order extends Model {     protected $fillable = ['user_id'];      \/**      * Relation to user      * @return BelongsTo      *\/     public function user(): BelongsTo     {         return $this-&gt;belongsTo(User::class);     } }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0444\u0430\u0431\u0440\u0438\u043a\u0435<\/strong>. \u041f\u043e\u043c\u043d\u0438\u043c, \u0447\u0442\u043e \u0441\u0432\u044f\u0437\u044c \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0431\u0440\u0430\u0442\u044c \u0435\u0451 id. <\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  \/** @var \\Illuminate\\Database\\Eloquent\\Factory $factory *\/  use App\\Order; use App\\User; use Faker\\Generator as Faker;  $factory-&gt;define(Order::class, function (Faker $faker) {     return [         'user_id' =&gt; factory(User::class)-&gt;create()-&gt;id     ]; });<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0433\u043e\u0442\u043e\u0432\u044b, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0442\u0435\u0441\u0442\u043e\u0432. <\/p>\n<p>  <\/p>\n<h4 id=\"anchortestsanchornapisanie-testov\"><a name=\"tests\"><\/a>\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432<\/h4>\n<p>  <\/p>\n<p><a href=\"https:\/\/laravel.com\/docs\/7.x\/testing\">\u041f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443<\/a>, Laravel \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 PHPUnit \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f \u0437\u0430\u043a\u0430\u0437\u0430: <\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">php artisan make:test OrderTest<\/code><\/pre>\n<p>  <\/p>\n<p>\u0424\u0430\u0439\u043b \u0442\u0435\u0441\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 tests\/Feature\/. \u0414\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0411\u0414 \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0439\u0442 RefreshDatabase. <\/p>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u0441\u0442 \u21161. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0444\u0430\u0431\u0440\u0438\u043a\u0438<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace Tests\\Feature;  use App\\Order; use App\\User; use Illuminate\\Foundation\\Testing\\RefreshDatabase; use Illuminate\\Foundation\\Testing\\WithFaker; use Tests\\TestCase;  class OrderTest extends TestCase {     use RefreshDatabase;      \/** @test *\/     public function order_factory_can_create_order()     {         \/\/ When we use Order factory         $order = factory(Order::class)-&gt;create();         \/\/ Then we should have new Order::class instance         $this-&gt;assertInstanceOf(Order::class, $order);     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u0441\u0442 \u043f\u0440\u043e\u0448\u0435\u043b! <\/p>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u0441\u0442 \u21162. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443 \u0437\u0430\u043a\u0430\u0437\u0430 \u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u0430<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">\/** @test *\/ public function order_should_have_user_relation() {     \/\/ When we use Order factory     $order = factory(Order::class)-&gt;create();     \/\/ Then we should have new Order::class instance with user relation     $this-&gt;assertNotEmpty($order-&gt;user_id);     $this-&gt;assertInstanceOf(User::class, $order-&gt;user); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u0441\u0442 \u043f\u0440\u043e\u0448\u0435\u043b!<\/p>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u0441\u0442 \u21163. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0444\u0430\u0431\u0440\u0438\u043a\u0443<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">\/** @test *\/ public function we_can_provide_user_id_to_order_factory() {     \/\/ Given user     $user = factory(User::class)-&gt;create();     \/\/ When we use Order factory and provide user_id parameter     $order = factory(Order::class)-&gt;create(['user_id' =&gt; $user-&gt;id]);     \/\/ Then we should have new Order::class instance with provided user_id     $this-&gt;assertEquals($user-&gt;id, $order-&gt;user_id); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u0441\u0442 \u043f\u0440\u043e\u0448\u0451\u043b!<\/p>\n<p>  <\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0432\u0430\u043c \u0432\u0430\u0436\u0435\u043d \u0444\u0430\u043a\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043a\u0430\u0437\u0430 \u043d\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c. \u0422\u0430\u043a\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u0441\u043e\u0441\u0430\u043d\u043d\u043e\u0439 \u0438\u0437 \u043f\u0430\u043b\u044c\u0446\u0430, \u043d\u043e \u0435\u0441\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438, \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0432\u0430\u0436\u043d\u043e.<\/p>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u0441\u0442 \u21164. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0437\u0430\u043a\u0430\u0437\u0430 \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">\/** @test *\/ public function when_we_create_one_order_one_user_should_be_created() {     \/\/ Given user     $user = factory(User::class)-&gt;create();     \/\/ When we use Order factory and provide user_id parameter     $order = factory(Order::class)-&gt;create(['user_id' =&gt; $user-&gt;id]);     \/\/ Then we should have new Order::class instance with provided user_id     $this-&gt;assertEquals($user-&gt;id, $order-&gt;user_id);     \/\/ Let's check that system has one user in DB     $this-&gt;assertEquals(1, User::count()); }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u0441\u0442 \u043f\u0440\u043e\u0432\u0430\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f!<\/strong> \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0443\u0436\u0435 \u0431\u044b\u043b\u043e \u0434\u0432\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u041a\u0430\u043a \u0442\u0430\u043a? \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0448\u0430\u0433\u0435.<\/p>\n<p>  <\/p>\n<h4 id=\"anchorproblemanchorproblema\"><a name=\"problem\"><\/a>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/h4>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432 \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u0431\u044b\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0442\u043e, \u0447\u0442\u043e \u0435\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u2014 \u0442\u0440\u0435\u0442\u0438\u0439 \u0442\u0435\u0441\u0442 \u043a\u0430\u043a \u043f\u0440\u0438\u043c\u0435\u0440. \u041d\u043e \u0442\u0430\u043a\u0436\u0435 \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043a\u043e\u0434, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0438 \u043a\u043b\u044e\u0447\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0442. \u0435. \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 \u0441\u0432\u043e\u0435\u0433\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0441 \u044d\u0442\u0438\u043c \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c. <\/p>\n<p>  <\/p>\n<h4 id=\"anchorsolutionanchorreshenie\"><a name=\"solution\"><\/a>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h4>\n<p>  <\/p>\n<p>\u041e\u043f\u0438\u0448\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0441\u044c \u0441\u0430\u043c. \u0412 PHP \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c n-\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u2014 <a href=\"https:\/\/www.php.net\/manual\/en\/function.func-get-arg.php\">func_get_arg()<\/a>, \u0435\u0439 \u043c\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0434\u043b\u044f \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0437\u0430\u043a\u0430\u0437\u043e\u0432. \u041f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443, \u0432 \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u043f\u0435\u0440\u0432\u044b\u043c (\u043d\u0443\u043b\u0435\u0432\u044b\u043c) \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f Faker, \u0430 \u0432\u0442\u043e\u0440\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u2014 \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043c\u0435\u0442\u043e\u0434 create() \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0437\u0430\u043a\u0430\u0437\u0430. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0443\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0439 (\u043f\u0435\u0440\u0432\u044b\u0439) \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0412 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432\u0430\u0436\u043d\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0431\u044b\u043b\u043e \u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u043b\u044e\u0447\u0443 \u0438\u043b\u0438 \u043d\u0435\u0442. \u0427\u0442\u043e \u0438\u043c\u0435\u0435\u043c \u0432 \u0438\u0442\u043e\u0433\u0435: <\/p>\n<p>  <\/p>\n<pre><code class=\"php\">$factory-&gt;define(Order::class, function (Faker $faker) {     \/\/ \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439     $passedArguments = func_get_arg(1);     return [         'user_id' =&gt; function () use ($passedArguments) {             \/\/ \u0415\u0441\u043b\u0438 \u043d\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043b\u0438 user_id, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0441\u0432\u043e\u0435\u0433\u043e             if (! array_key_exists('user_id', $passedArguments)) {                 return factory(User::class)-&gt;create()-&gt;id;             }         }     ]; });<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0442\u0435\u0441\u0442 \u21164 \u0435\u0449\u0451 \u0440\u0430\u0437 \u2014 \u043e\u043d \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442!<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0451, \u0447\u0435\u043c \u044f \u0445\u043e\u0442\u0435\u043b \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f. \u0427\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0441\u044c \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439, \u0447\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u0430\u0431\u0440\u0438\u043a \u0441 \u044d\u0442\u0438\u043c \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f. <\/p>\n<p>  <\/p>\n<p>\u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0441\u043b\u044b\u0448\u0430\u0442\u044c \u0432\u0430\u0448\u0438 \u0445\u0438\u0442\u0440\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0432\u044b \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435\u0441\u044c \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043d\u0430 Laravel \u0438\u043b\u0438 PHP.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/493926\/\"> https:\/\/habr.com\/ru\/post\/493926\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/493926\/\">\n<h4 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h4>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Laravel \u0432\u0435\u0440\u0441\u0438\u0438 \u0432\u044b\u0448\u0435 6 \u0438 \u0445\u043e\u0442\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u0435\u0441\u0442\u044b. <\/p>\n<p>  <\/p>\n<p><strong>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043e \u043d\u0438\u0436\u0435:<\/strong><\/p>\n<p>  <\/p>\n<ol>\n<li><a href=\"#start\">\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438<\/a><\/li>\n<li><a href=\"#app\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/a><\/li>\n<li><a href=\"#entities\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/a><\/li>\n<li><a href=\"#tests\">\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432<\/a><\/li>\n<li><a href=\"#problem\">\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/a><\/li>\n<li><a href=\"#solution\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/a><\/li>\n<\/ol>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-300653","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300653"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300653\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}