【发布时间】:2016-11-18 21:13:17
【问题描述】:
我有一个我一直在处理的 Web 应用程序项目,除了在资源上提交标志之外,大多数应用程序功能都可以工作。我想使用模式表单将数据提交到数据库,然后在标记视图页面上显示所有标记的资源Name & Description(来自资源表)Flag_Reason & Other_Comments(来自标记表)我让它工作到它的位置只提交Flag_Reason and Other_Comments,根本没有更新我的资源表。我相信我现在遇到了路由问题,因为在更改我的函数以更新我的资源表并在数据库中创建一个新的标志条目后,我收到这样的错误
Missing argument 1 for App\Http\Controllers\FlagsController::addFlag()
这是我的一些代码,希望有人可以帮助我最终一劳永逸地解决这个问题。
路线
Route::get('resource', array('as'=>'viewResource', 'uses' => 'ResourceController@resource'));
Route::get('flags', 'FlagsController@index');
Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController@addFlag']);
///Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController@postFlag']);///
This route works fine, and only inserts the Flagged table data into the database.
如果我修改我的路线看起来像这样Route::post('resource/{Resource_ID}', ['as' => 'resource', 'uses'=>'FlagsController@addFlag'])
我收到这样的错误
Missing required parameters for [Route: resource] [URI: resource/{Resource_ID}].
标志控制器
class FlagsController extends Controller
{
public function index()
{
$resources = Resources::where('Flagged', 1)->with('flags')->get();
return view('pages.flags', ['resource' => $resources]);
}
public function addFlag($id)
{
$flag = Flagged::create(Request::all());
$resource = Resources::findOrFail($id);
$resource->update(array('Flagged' => 1));
$resource->flags()->attach([$flag->id]);
dd($resource::all());
return back();
}
//////// This function inserts only the Flagged table data into the Flagged table, It doesnt do what I want it to do, so i've commented it out/////
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');
}
}
资源视图
...
@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>{{ $location->City }}</td>
<td>{{ $location->Zip_Code }}</td>
<td>{{ $location->County }}</td>
<td>
<button type="button" class=" msgBtn btn btn-default" style=" display:inline; margin-right:auto;"><a href="pages/editresources/{{$resource['Resource_ID']}}">Edit</a>
</button>
<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>
<button type="button" class=" msgBtn3 btn btn-default pull-right" style="display:inline; margin-right:auto;"><a href="pages/deleteResource/{{$resource['Resource_ID']}}">Delete</a>
</button>
</td>
</tr>
@endforeach
@endforeach
模式,在资源视图内
<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('route'=>'resource', '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 id="submitFlag" type="submit" class="btn btn-primary" style="margin-left:5px;">Flag</button>
</span>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
<script>
$('#flagResource').on('show.bs.modal', function(e) {
//var submitFlag = $(e.relatedTarget);
var resourceName = $(e.relatedTarget).data('resource-name');
var resourceId = $(e.relatedTarget).data('resource-id');
var modal = $(this);
modal.find('.modal-title').text(resourceName);
});
</script>
我认为正在发生的问题是我的表单在我的模态中打开
{!! Form::open(array('route'=>'resource', 'class'=>'form', 'method'=>'POST')) !!},而我的 addFlag 函数接受 ID,但我的资源路由不需要 {id}。
如果有人可以查看我的路线并帮助我调试它,那就太好了。提前致谢。
【问题讨论】:
标签: php sql laravel routes modal-dialog