English 中文(简体)
Laravel - 工厂在定义方法上不承认的领域
原标题:Laravel - Factory does not recognized fields in definition method
  • 时间:2023-08-08 02:01:08
  •  标签:
  • laravel

我刚刚创建了一个工厂,以便进行一些试验:

CategoryFactory.php

<?php

namespace DatabaseFactories;

use IlluminateDatabaseEloquentFactoriesFactory;

/**
 * @extends IlluminateDatabaseEloquentFactoriesFactory<AppModelsCategory>
 */
class CategoryFactory extends Factory
{
    /**
     * Define the model s default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            //
             company_id  => 1,
             name  => fake()->name(),
             parent_id  => null
        ];
    }
}

并在模型中添加:

类别.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;

use AppScopesCompanyScope;

class Category extends Model
{
    //
    use SoftDeletes, HasFactory;

    protected $fillable = [
         company_id ,
         name ,
         parent_id 
    ];

public function __construct() {
    parent::__construct();
    if(isset(auth()->user()->company_id)) {
        $this->attributes[ company_id ] = auth()->user()->company_id;
    }
}


protected static function boot()
{
    parent::boot();

    static::addGlobalScope(new CompanyScope);
}

My test file looks like this:

public function test_category_edit_can_be_redered(): void
    {
        $user = User::factory()->create();
        $category = Category::factory()->create();

        $response = $this->actingAs($user)->get( /devices/categories/1/edit );

        $response->assertStatus(200);
    }

但是,当我尝试进行测试时,我发现以下错误:

General error: 1364 Field  company_id  doesn t have a default value (Connection: mysql, SQL: insert into `categories` (`updated_at`, `created_at`) values (2023-08-08 01:55:43, 2023-08-08 01:55:43))

它认为工厂的定义不适用于制造方法...... 任何想法,我是错做的?

问题回答

如果今后任何人都面临这一问题,答案是:

Laravel工厂在模型有构件时失败。

You trigger your category before you signIn a user so that the code doesn t have a default auth()->user()

页: 1

you can use use IlluminateDatabaseConsoleSeedsWithoutModelEvents or saveQuietly() in your seeder to not trigger global scope events.

我也认为:

public function test_category_edit_can_be_redered(): void
    {
        $user = User::factory()->create();
        Auth::loginUsingId($user->id);
        $category = Category::factory()->create();

        $response = $this->actingAs($user)->get( /devices/categories/1/edit );

        $response->assertStatus(200);
    }




相关问题
Table of calificacions in php and laravel

I created a table in order to show the name of the students, name of the subjects and the grade of the subject of the student I have the next code: this is the index part <form action="{{ ...

How to fix CORS for Capacitor/Quasar App in a Laravel API

I have an App built on top of Quasar and Capacitor, with an API built on top of Laravel. Everything works well on the dev environment until I build and install the app on my phone, then I keep getting ...

Laravel +Vuejs CORS issue

I am facing a dilemma wit the CORS issue. I have application that is configured as follow: API: https://abc.mydomain.com (laravel) front end: https://subdomain.mydomain.com(VueJS) Both applications ...

Laravel wrong redirecting to login page after login

In a multi tenancy with subdomain enviroment (http://zapburger.dex.local) when user fills login form with valid data and make login, laravel redirect to dashboard route (as expected) and then redirect ...

热门标签