【发布时间】:2019-06-15 11:35:39
【问题描述】:
我在 Laravel 5.7 中有以下工厂,调用它时没有返回任何内容:
<?php
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Model;
$factory->define(App\Record::class, function (Faker $faker) {
return [
"name" => $faker->name,
];
});
而我的模型是:
<?php
namespace App;
use App\Product;
use Illuminate\Database\Eloquent\Model;
class Record extends Model
{
protected $table = "records";
protected $fillable = ["name"];
function __construct()
{
parent::__construct();
}
}
我在这里调用工厂:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App;
use App\Product;
use App\Record;
use App\User;
class RecordTest extends TestCase
{
use RefreshDatabase;
use WithoutMiddleware;
/** @test */
public function when_record_page_for_existing_record_is_accessed_then_a_product_is_displayed()
{
//$record = factory(App\Record::class)->make();
$record = factory(App\Record::class)->create();
echo "\n\n$record->name\n\n";
}
}
打印时
$record->name
我什么都没有,不是 null,没有空字符串,什么都没有。似乎是什么问题?如果我将工厂生成的任何内容保存到变量中而不是立即返回它,我可以看到该名称正在被填充,但在返回它之后没有任何反应,它就消失了
【问题讨论】:
-
你试过dd() $record 变量吗?
-
是的,这不是打印问题,问题在于尝试打印的内容
标签: php laravel phpunit factory