2016-06-27 14 views
0

私はアイディアとコミュニティモデルを持っています。それらの間には多対多の関係が存在します。関係は完全に形成されています。しかし、私は1つの問題に直面しています。 mayコミュニティにアイデアがあり、コミュニティには多くのアイデアがあります。したがって、これに基づいて中間テーブルがあります。ピボットテーブルにcommunity_ididea_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->コミュニティ()作っている - >添付($要求 - >入力( '選択'));

cannot add/update

編集:

<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> 

答えて

0

あなたがラインに

$idea->communities()->attach($request->input('selection')); 

を関係を追加するときに避難所ため、ピボットテーブルに追加するidea_idはありませんアイデアオブジェクトをデータベースに保存しないでください。それを保存するには前に必要ですすべての属性であり、次に関係を追加します。また、あなたがクエリで見ることができるように、あなたはあなたの要求からcommunity_idを渡すのではなく、私が思うコミュニティ名も問題になるでしょう。

編集更新

後にまた、あなたのビューからcommunity_idの値を渡す必要があります。これを行うには

@foreach($Community as $selectCommunity) 
    <option value="{{$selectCommunity->community_id}}">{{$selectCommunity->community_name}}</option> 
@endforeach 
+0

これは、 '$ idea-> save();の後ろに記述した行を追加する必要があることを意味し、' idea_id'を保存します。もしそれが肯定であれば、idは 'community_id'を格納するために何をすべきでしょうか? –

+0

はい、そうです。コミュニティは既に存在していますか? yesの場合、$ requestから正しく渡された場合、community_idは自動的に保存されます。 – geoandri

+0

はい 'communities'はすでに存在しますが、その' id'はエラーを引き起こしている 'community_name'を返すことを保存していません。 –

0

一つの方法は、あなたのアイデアの記録と保存IdeaComunnityオブジェクトを作成し、それぞれのIDの(イデアとコミュニティ)を設定し、このオブジェクトを保存する最初のです。

Ideaオブジェクトがまだ保存されていないため、おそらくエラーが発生しています。したがって、この制約の例外があります。

関連する問題