Summer Sale! All accounts are 50% off this week.

Shivamyadav's avatar

field is required showing error?

my factory code for owner_id

 public function definition()
    {
        return [
            'title' => $this->faker->sentence,
            'description' => $this->faker->sentence,
            'owner_id' => function () {
                return User::factory()->create()->id;
            }
        ];
    }

my test code

public function test_a_project_requires_a_owner()
    {
        // $this->wthouthExceptionHandling();
        $attributes = Project::factory()->raw(['owner_id' => null]);
        $this->post('/projects', $attributes)->assertSessionHasErrors('owner');
    }
0 likes
16 replies
tykus's avatar

What's the error?

Aside, the assertion is looking for the wrong key in the Error Bag:

$this->assertSessionHasErrors('owner_id')
1 like
Shivamyadav's avatar

@tykus done this change but when i dd the request here is the result i didn't get the owner_id from factory why? factory code is on my first question of this forum..

array:2 [ // app\Http\Controllers\ProjectsController.php:22
  "title" => "Exercitationem ipsa distinctio aut et."
  "description" => "Ab dolorem veniam eum quia et saepe aut."
]
Shivamyadav's avatar

@tykus this is the result i get

array:2 [ // app\Http\Controllers\ProjectsController.php:22
  "title" => "Exercitationem ipsa distinctio aut et."
  "description" => "Ab dolorem veniam eum quia et saepe aut."
]
Shivamyadav's avatar

@tykus yea ! telling me that owner_id doesn't have a default value but i am passing null when we create it and also from my factory..why showing error?

SQLSTATE[HY000]: General error: 1364 Field 'owner_id' doesn't have a default value (SQL: insert into `projects` (`title`, `description`, `updated_at`, `created_at`) values (Laudantium quae inventore dolores laudantium nam sunt., Voluptas quo asperiores id magnam., 2022-11-09 22:07:32, 2022-11-09 22:07:32))

my factory code

 public function definition()
    {
        return [
            'title' => $this->faker->sentence,
            'description' => $this->faker->sentence,
            'owner_id' => function () {
                return User::factory()->create()->id;
            }
        ];
    }
tykus's avatar

@Shivamyadav please:

public function test_a_project_requires_a_owner()
{
    // $this->wthouthExceptionHandling();
    $attributes = Project::factory()->raw(['owner_id' => null]);
    dd($attributes);
    $this->post('/projects', $attributes)->assertSessionHasErrors('owner');
}
Shivamyadav's avatar

@tykus here it is

array:3 [ // tests\Feature\ProjectsTest.php:63
  "title" => "Praesentium eaque cupiditate odio nemo ipsam veniam quibusdam iste."
  "description" => "Enim qui in aut doloremque hic aut sit."
  "owner_id" => null
]


Shivamyadav's avatar

@tykus now it showing that owner_id have not a default value but when i dd it gives me the id

 public function test_a_project_requires_an_owner()
    {
        $attributes = Project::factory()->raw();
        // dd($attributes);
        $this->post('/projects', $attributes)->assertSessionHasErrors('owner');
    }

error

SQLSTATE[HY000]: General error: 1364 Field 'owner_id' doesn't have a default value (SQL: insert into `projects` (`title`, `description`, `updated_at`, `created_at`) values (Impedit iusto molestias saepe id est velit labore., Dolores qui ratione id nobis aliquam repellat., 2022-11-09 22:23:31, 2022-11-09 22:23:31))
Shivamyadav's avatar

@tykus controller store

 public function store()
    {
        $attributes = request()->validate([
            'title' => 'required',
            'description' => 'required',
            // 'owner_id' => 'required'
        ]);
        // dd($attributes);
        Project::create($attributes);
        return redirect('/projects');
    }
tykus's avatar

@Shivamyadav okay, you don;'t have owner_id in the $attributes array because there is no rule (commented) for it! The validate method returns only the keys from the Request that have a rule:

 public function store()
{
    $attributes = request()->validate([
        'title' => 'required',
        'description' => 'required',
         'owner_id' => 'required'
    ]);
    Project::create($attributes);

    return redirect('/projects');
}
1 like
Shivamyadav's avatar

@tykus now showing that field is required as per validation

The following errors occurred during the last request:

The owner id field is required.


tykus's avatar
tykus
Best Answer
Level 104

@Shivamyadav but that should be handled whenever you assert that the Session has errors for owner_id, right?

 public function test_a_project_requires_an_owner()
{
    $attributes = Project::factory()->raw();
    $this->post('/projects', $attributes)
        ->assertSessionHasErrors('owner_id');
}

Please or to participate in this conversation.