我有一个想法和社区模型。他们之间存在着多对多的关系。关系正在形成完美。但是,我面临一个问题。
可以在社区中找到一个想法,社区可以有很多想法。因此,基于此,有一个中间表。我无法将community_id
和idea_id
存储在数据透视表中。
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>
答案 0 :(得分: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
答案 1 :(得分:0)
执行此操作的一种方法是首先保存您的创意记录,然后创建一个IdeaComunnity对象,设置相应的ID(想法和社区)并保存此对象。
您的错误可能是因为您的Idea对象尚未保存,因此,您有此约束异常。