dimanche 22 mars 2020

Routing to function not working but somehow closure does | Laravel 7

I have a problem with routing and I'm near of freaking. In my API route file:

Route::post('/role-user/attach/{user}', 'RoleUserController@attach')->name('role.user.attach');

This is my Controller:

public function attach(User $user)
{ 
    $user->assignRole($this->roles);
    return response('',200);
}

I making post request to the route by name in my test: (also tried path)

public function roles_can_be_attached_to_a_user()
{
    $user = factory(\App\User::class)->create();
    $roles = factory(\App\Role::class,5)->create();

    $this->post(route('role.user.attach', ['user' => $user]), ['roles' => $roles]);

    $this->assertTrue($user->Roles->contains($roles->first()));
    $this->assertCount(5, $user->Roles);
}

And then it gives me an error like:

PDOException: SQLSTATE[HY000]: General error: 25 column index out of range

The funny part is, when I move the code from controller to API route file as a closure:

Route::post('/role-user/attach/{user}', function(App\User $user) {
    $user->assignRole($this->roles);
    return response('',200);
})->name('role.user.attach');

Then it lights a green without any problem. What just I missing? I struggling about 2 hours and I don't even know how to google it. If it's because of sqlite, then it should also give a red light in closure, right? Also, my controller is in main Controller directory as it should be. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire