在 bootstrap 中打开模式,但也调用控制器操作来填充表单,laravel 5.2

Open a modal in bootstrap but also call a controller action to populate the form, laravel 5.2

我有一个打开的模式,它包含一个用于为博客创建新 post 的表单。一点都不难。当我单击创建时,将调用控制器操作,然后创建 post,依此类推。

这一切都很棒。

但现在我想做的是当我点击 "edit" 时,我希望打开相同的模式,但我希望内部表格填充上述 post。模态触发器是否可以调用控制器操作,打开模态然后我想从那里开始?

目前我打开模态的方法是:

<a href="#" class="btn btn-create" data-toggle="modal" data-target="#createNewPost">Create Post</a>

// Which calls:

<div class="modal fade" id="createNewPost" tabindex="-1" role="dialog" aria-labelledby="createNewPost">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="createNewBlog">Create a new post</h4>
            </div>
            <div class="modal-body">
                @include('blog.posts.create')
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

// Note the include (from above):

{{ Form::open(array('url' => 'create-new-post', 'class' => 'form-horizontal')) }}
    <div class="form-group">
        {{ Form::label('title', 'Post Title', array('class' => 'col-sm-2 control-label')) }}
        <div class="col-sm-10">
            {{ Form::text('title', '', array('placeholder' => 'sample title', 'class' => 'form-control')) }}
        </div>
    </div>
    <div class="form-group">
        {{ Form::label('content', 'Post Content', array('class' => 'col-sm-2 control-label')) }}
        <div class="col-sm-10">
            {{ Form::textarea('content', '', array('placeholder' => 'some content', 'class' => 'form-control')) }}
        </div>
    </div>
    <div class="form-group">
        {{ Form::label('tags', 'Post Tags', array('class' => 'col-sm-2 control-label')) }}
        <div class="col-sm-10">
            {{ Form::text('tags', '', array('class' => 'form-control', 'data-role' => 'tagsinput')) }}
        </div>
    </div>
    <div class="form-group">
        {{ Form::label('categories', 'Post Categories', array('class' => 'col-sm-2 control-label')) }}
        <div class="col-sm-10">
            {{ Form::text('categories', '', array('class' => 'form-control', 'data-role' => 'tagsinput')) }}
        </div>
    </div>

    {{ Form::hidden('blogId', $blog->id) }}

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            {{ Form::submit('Create', array('class' => 'btn btn-primary')) }}
        </div>
    </div>
{{ Form::close() }}

所以这非常适合创建博客。现在我想做同样的事情,但这次我想调用一个控制器动作然后 return 一些东西所以我可以在模态视图中使用它并用它填充所述表单。

想法?

是的,您可以使用表单模型绑定来做到这一点。请查看 laravel 文档或此 link https://scotch.io/quick-tips/laravel-form-model-binding