私はアイディアとコミュニティモデルを持っています。それらの間には多対多の関係が存在します。関係は完全に形成されています。しかし、私は1つの問題に直面しています。 mayコミュニティにアイデアがあり、コミュニティには多くのアイデアがあります。したがって、これに基づいて中間テーブルがあります。ピボットテーブルにcommunity_id
とidea_id
を格納できません。中間テーブルに値を保存できませんlaravel
public function storePost(IdeaRequest $request)
{
if ($request->hasFile('idea_image')) {
$filename = $request->file('idea_image')->getClientOriginalName();
$moveImage = $request->file('idea_image')->move('images/', $filename);
}
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = Auth::id();
$idea->idea_image = $moveImage;
$idea->idea_info = $request->input('idea_info');
$idea->communities()->attach($request->input('selection'));
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = strip_tags($request->input('idea_description'));
$idea->save();
Session::set('idea_id',$idea->id);
session()->flash('flash_message', 'Your idea has been submitted for Review, Kindly Fill this Work BreakDown of You Work');
return redirect()->route('create_wbs');
}
この行は問題
$ idea->コミュニティ()作っている - >添付($要求 - >入力( '選択'));
編集:
<form id="create_idea" class="clearfix" action="{{url('/idea')}}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form form-post-idea">
<div class="left-input"><h3>Project Title:</h3></div>
<label for="project_title"></label>
<input type="text" name="idea_title" class="txt fill-width project_title" placeholder="Project Title"/>
<label for="project_image"><h3>Project Image:</h3></label>
<input type="file" name="idea_image" class="txt fill-width project_image" placeholder="Project Image" value=""/>
<label for="project_info"><h3>Short Info of Project:</h3></label>
<textarea name="idea_info" class="txt fill-width" placeholder="Info should not exceed 150 characters."></textarea>
<label for="project_community"><h3>Project Community:</h3></label>
<select class="txt fill-width project_community" name="selection[]" multiple>
@foreach($Community as $selectCommunity)
<option>{{$selectCommunity->community_name}}</option>
@endforeach
</select>
<label for="project_location"> <h3>Project Location:</h3></label>
<input id="project_location" type="text" name="idea_location" class="txt fill-width idea_location" placeholder="Project Location" value=""/>
<label for="funding_goal"><h3>Funding Goal:</h3></label>
<input id="funding_goal" type="number" name="idea_goal" class="txt fill-width idea_goal" placeholder="Amout needed to complete Project" value=""/>
<label for="project_description"><h3>Project Description:</h3></label>
<textarea id="content" name="idea_description" class="txt fill-width" placeholder="Project Description"></textarea>
<label for="submit_for_review"></label><br /><br />
<input type="submit" class="btn btn-green btn-buck-project" value="Submit For Review" id="review">
</div>
</form>
これは、 '$ idea-> save();の後ろに記述した行を追加する必要があることを意味し、' idea_id'を保存します。もしそれが肯定であれば、idは 'community_id'を格納するために何をすべきでしょうか? –
はい、そうです。コミュニティは既に存在していますか? yesの場合、$ requestから正しく渡された場合、community_idは自動的に保存されます。 – geoandri
はい 'communities'はすでに存在しますが、その' id'はエラーを引き起こしている 'community_name'を返すことを保存していません。 –