Reputation: 47
An error has occurred where I set the validation.
put validation(policy) in index.blade (@ can ~ endcan)
, and click "Edit",
403 This action is unauthorized.
Is displayed and I cannot move to the edit page.
I want the code to be able to move to the Edit page.
I tried bb on PostPolicy. 2nd line is null. What do I need to do to...? Please help.
$factory->define(Post::class, function (Faker $faker) {
return [
'created_at' => $faker->date('Y-m-d H:i:s', 'now'),
'updated_at' => $faker->date('Y-m-d H:i:s', 'now'),
'subject' => $faker->realText(16),
'message' => $faker->realText(200),
'name' => $faker->name,
'user_id' => factory(App\User::class),
];
});
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
});
/**
* 編集と削除の認可を判断する。
*
* @param \App\User $user
* @param \App\Post $post
* @return mixed
*/
public function edit(User $user, Post $post)
{
dump($user->id);
dd($post->user_id);
return $user->id === $post->user_id;
#return $user->id == $post->user_id;
}
public function update(User $user, Post $post)
{
return $user->id === $post->user_id;
}
}
/**
* 編集と削除の認可を判断する。
*
* @param \App\User $user
* @param \App\User $model
* @return mixed
*/
public function edit(User $user, User $model)
{
return $user->id == $model->id;
}
Upvotes: 0
Views: 1472
Reputation: 119
Maybe if you would return the user id directly from the result of the factory
Something like that ( I am not sure that this will work, but you can try it )
$factory->define(Post::class, function (Faker $faker) {
return [
'created_at' => $faker->date('Y-m-d H:i:s', 'now'),
'updated_at' => $faker->date('Y-m-d H:i:s', 'now'),
'subject' => $faker->realText(16),
'message' => $faker->realText(200),
'name' => $faker->name,
'user_id' => factory(App\User::class)->id,
];
});
Upvotes: 1
Reputation: 1384
Try this and this is only for registering seeder factory maybe there will be some other errors if you do we will debug it as per your error.
change this 'user_id' => factory(App\User::class)
to this 'user_id' => App\User::all()->random()->id
like below to generate random id for your user
in PostFactory
$factory->define(Post::class, function (Faker\Generator $faker) {
return [
'created_at' => $faker->date('Y-m-d H:i:s', 'now'),
'updated_at' => $faker->date('Y-m-d H:i:s', 'now'),
'subject' => $faker->realText(16),
'message' => $faker->realText(200),
'name' => $faker->name,
'user_id' => App\User::all()->random()->id,
];
});
and then in DatabaseSeeder.php
call it like below
$this->call(UsersTableSeeder::class);
note: maybe your seeder name will be different
Upvotes: 1