I
I
Ilya Loopashko2021-04-28 10:04:27
Laravel
Ilya Loopashko, 2021-04-28 10:04:27

How to pass an array with the Post method?

Good afternoon. I am writing a test for a controller,

Controller

public function update(Request $request) {

        $input = $request->get('category', []);
        foreach (Category::all() as $category) {
            $category->roles()->sync(Arr::get($input, $category->id, []));
        }
        dd($input);
        return view('home');
    }


Test
/** @test */
public function test_admin_user_can_edit_categories_roles()
{
  $category = \App\Models\Category::factory()->create(['id' => 1]);
  $role = \App\Models\Role::factory()->create(['id' => 1]);
  
  $data = [
            '1' => ['1' => '1'],
            '2' => ['1' => '1'],
        ];
    
  $this->actingAs(\App\Models\User::factory()->create(['role_id' => 1]));
    $this->post('/admin/connections', $data);
  
  $this->assertDatabaseHas('categories_roles', [
            'categories_id' => $category->id,
            'roles_id' => $role->id
        ]);
}


When passing the test, only square brackets ([]) come out and that's it. How to pass an array correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2021-04-28
@deadloop

$data = [
  'category' => [
    '1' => ['1' => '1'],
    '2' => ['1' => '1'],
  ],
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question