我正在尝试通过模式添加2个输入字段,但是一旦我点击提交,我会收到一条错误消息
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'Flag_Reason' cannot be null (SQL: insert into `Flagged` (`Flag_Reason`, `Other_Comments`) values (, ))
以下是我的一些与此问题相关的代码。
资源&国旗模型
class Resource extends Model
{
protected $table = 'Resources';
protected $primaryKey = 'Resource_ID';
public $timestamps = false;
protected $fillable = [
'Name',
'Description',
'Misc_Info'
];
protected $guarded = [];
/** A resource can have many locations */
public function locations()
{
return $this->belongsToMany('App\Models\Location', 'ResourceLocation', 'Location_ID', 'Resource_ID');
}
public function flag ()
{
return $this->hasMany('App\Models\Flagged');
}
class Flagged extends Model
{
protected $table = 'Flagged';
protected $primaryKey = 'Flag_ID';
public $timestamps = false;
protected $fillable = [
'Flag_Reason',
'Other_Comments',
];
protected $guarded = [];
}
资源视图(在此视图中触发模式)
<table class=" display table table-hover table-bordered" , id="resource">
<thead>
<th>Name</th>
<th>Description</th>
<th>Address</th>
</thead>
<tbody>
@foreach($resources as $resource) @foreach ($resource->locations as $location)
<tr>
<td> <a class="btn btn-small btn-default" style="float:right; margin-right:5px;" href="{{ URL::to('resource/addToCart/' .$resource->Resource_ID) }}">+</a> {{ $resource->Name }}</td>
<td>{{ $resource->Description }}</td>
<td>{{ $location->Address }}</td>
<td>
<button type="button" id="submitFlag" class=" msgBtn btn btn-default" style=" display:inline; margin-right:auto;"><a href="#flagResource" data-toggle="modal" data-resource-id="{{ $resource->Resource_ID }}" data-resource-name="{{ $resource->Name }}">Flag</a>
</button>
</td>
</tr>
@endforeach @endforeach
</tbody>
</table>
模态
<div class="modal fade" id="flagResource" tabindex="-1" role="dialog" aria-labelledby="flagModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="flagResourceLabel" style="text-align:center;"> Flagged
</h4>
</div>
<div class="modal-body">
{!! Form::open(array('url'=>'flags', 'class'=>'form', 'method'=>'POST')) !!}
<div class="form-group">
<label for="reason" class="control-label">Reason for Flagging:</label>
{!! Form::text('reason', null, array('class'=> 'form-control', 'placeholder'=>'Reason')) !!}
</div>
<div class="form-group">
<label for="comments" class="control-label">Other Comments:</label>
{!! Form::text('comments', null, array('class'=> 'form-control', 'placeholder'=>'Comments')) !!}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<span class="pull-right">
<button type="button" class="btn btn-primary" style="margin-left:5px;"><a href="setflag/{{$resource['Resource_ID']}}">Submit</a></button>
</span>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
<script>
$('#flagResource').on('show.bs.modal', function(e) {
var submitFlag = $(e.relatedTarget);
var resourceName = submitFlag.data('resource-name');
var resourceId = submitFlag.data('resource-id');
var modal = $(this);
modal.find('.modal-title').text(resourceName);
});
</script>
举报控制器
public function addFlag($id)
{
$flag = Flagged::create(Request::only(
'Flag_Reason',
'Other_Comments' ));
$flag->save(); \Session::flash('flash_message', 'Flagged!');
return back();
}
输入标志的原因和注释后,我收到上述消息。我在这做错了什么?提前谢谢!
编辑路线
Route::get('flags', 'FlagsController@index');
Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController@postFlag']);
Route::get('flags/edit/{Resource_ID}', 'FlagsController@editResource');
Route::patch('flags/edit/{Resource_ID}', 'FlagsController@updateResource');
Route::get('setflag/{Resource_ID}', 'FlagsController@addFlag');
Route::get('pages/editresources/rmflag/{Resource_ID}', 'FlagsController@removeFlag');
Route::get('pages/editresources/rmdelete/{Resource_ID}', 'FlagsController@removeDelete');
Route::get('setdelete/{Resource_ID}', 'FlagsController@addDelete');
答案 0 :(得分:0)
通过在我的控制器中更改它来修复它:
$flag = Flagged::create(Request::only(
'Flag_Reason',
'Other_Comments' ));
&#13;
到此:
public function postFlag()
{
$flag = Flagged::create([
'Flag_Reason' => Input::get('reason'),
'Other_Comments' =>Input::get('comments')]);
$flag->save();
\Session::flash('flash_message', 'Flagged!');
return redirect('resource');
}
&#13;