Ad

Open A Modal In Bootstrap But Also Call A Controller Action To Populate The Form, Laravel 5.2

I have a modal that I open and it contains a form for creating a new post for a blog. Nothing difficult at all. When I click create, a controller action is called, the post is then created and so on and so forth.

This is all awesome.

But now what I want to do is when I click the "edit" I want the same modal to open, but I want the inside form populated with said post. Is there away on the modal trigger to call a controller action , open the modal and then do I want from there?

What I have so far for opening the modal is:

<a target="_blank" rel="nofollow noreferrer" 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() }}

So this works really well for creating a blog. Now I want to do the same thing but this time I want to call a controller action to then return something so I can use it in the modal view and populate said form with it.

Ideas?

Ad

Answer

Yes you can do that by using Form Model Binding.. check out the laravel docs or this link https://scotch.io/quick-tips/laravel-form-model-binding

Ad
source: stackoverflow.com
Ad