【问题标题】:Laravel Routing Issues For Modal Form模态表单的 Laravel 路由问题
【发布时间】: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">&times;</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'=&gt;'resource', 'class'=&gt;'form', 'method'=&gt;'POST')) !!},而我的 addFlag 函数接受 ID,但我的资源路由不需要 {id}。

如果有人可以查看我的路线并帮助我调试它,那就太好了。提前致谢。

【问题讨论】:

    标签: php sql laravel routes modal-dialog


    【解决方案1】:

    您需要保持路由的方式,以便它通过Resource_ID,但是您还需要在设置表单时通过它。

    {!! Form::open(array('route'=>'resource' array('Resource_ID' => $yourId), 'class'=>'form', 'method'=>'POST')) !!}
    

    关于您的评论,并仔细查看您的代码,我认为重新考虑它的工作原理可能是有意义的。

    您正在向此视图传递多个资源,因此我认为像我最初建议的那样将资源 id 添加到 URL 并不是最好的主意,因为它需要是动态的,具体取决于单击的资源并且 URL 不是最容易通过 javascript 进行更改。

    我认为更好的解决方案是重新使用 Form::open(array('route'=&gt;'resource', 'class'=&gt;'form', 'method'=&gt;'POST')) !!},然后从您的路由中删除 /{Resource_ID} 部分,并从 public function addFlag() 中删除 $id,因为我们不再通过 URL 传递它.

    然后在你的表单中,为resource_id添加一个隐藏的有效

    <input type="hidden" name="resource_id" id="resource_id" value="" />
    

    那么你已经在监听 bootstrap show 事件并获取了正确的资源 id,我们只需要将它添加到值中。

            $('#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);
                    $('#resource_id').val(resourceId);
            });
    

    在您的addFlag() 方法中没有,您可以通过...获取资源ID...

    $id = \Input::get('resource_id');
    

    【讨论】:

    • 感谢您的回复,在您的表单方法中$yourId 我应该在那里使用哪个ID?我试过$id 我得到undefined variable id,我试过$resource 并得到一个Array to string conversion 错误。
    • 我刚刚更新了答案,也许最好换一种方式,因为我们正在处理多个资源。
    【解决方案2】:

    您在 FlagsController 中的 addFlag 函数接受一个名为 $id 的参数,您收到的异常是因为您忘记将参数添加到您的路由中。

    Route::post('resource', ['as' => 'resource', 'uses'=>'FlagsController@addFlag']);
    

    应该是

    Route::post('resource/{id}', ['as' => 'resource', 'uses'=>'FlagsController@addFlag']);
    

    我相信 Laravel 路由参数是特定于名称的,这就是 {Resource_ID} 不起作用的原因。

    你可以在 Laravel here 中阅读更多关于带参数的路由 至于资源视图中的模式,您还需要在表单上传递要更新的资源的参数

    {!! Form::open(array('route'=>'resource', array('id' => $yourIdHere), 'class'=>'form', 'method'=>'POST')) !!}
    

    确保将 $yourIdHere 替换为您要添加的资源。

    其他一切似乎都井井有条。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 2017-07-08
      • 1970-01-01
      • 2019-12-04
      • 2014-08-14
      • 2018-11-20
      相关资源
      最近更新 更多