10. Community store
community_user many to many 관계 테이블 생성
php artisan make:migration create_community_user_table
생성된 migration 파일 수정
Schema::create('community_user', function (Blueprint $table) {
$table->foreignId('community_id');
$table->foreignId('user_id');
});
migrate
php artisan migrate
Community 모델 수정
public function users()
{
return $this->belongsToMany(User::class);
}
유효성 검사 store form request 생성
php artisan make:request StoreCommunityRequest
// StoreCommunityRequest.php
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|min:3|max:20|unique:communities',
'description' => 'required|max:500',
];
}
Community 컨트롤러 수정
public function store(StoreCommunityRequest $request)
{
$community = Community::create($request->validated() + ['owner_id' => auth()->id()]);
$community->topics()->attach($request->topics);
$community->users()->attach(auth()->id());
return redirect()->route('communities.show', $community);
}
git commit
git add .
git commit -m "feat: create community store function"
Author And Source
이 문제에 관하여(10. Community store), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@yeomc/10-Community-store저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)