Laravel on Modals

Credits: Sharon McCutcheon on Unsplash

jquery-ujs

There are not many articles on how to set up and use Laravel and jquery-usj. Except for this one, which is a good place to start to realize the purpose of it:

Installation jquery-ujs on Laravel

I’m sure you know the drill, so let’s skip Laravel installation. First, install the package by npm with other default packages (e.g. jquery) by commands:

npm install
npm install --save jquery-ujs
require('jquery-ujs');

Usage jquery-ujs for Modals in Laravel

On RoR, I was also using jquery-ujs in ways as described in the post above. But especially for the Modals, where Modals became as easy as Blade Layouts. With Validation! And (almost) without JS!

Set up JS part and Modal Layout

Create remote.js and add it to the resources/js/app.js somewhere at the end.

// resources/js/app.jsrequire('./bootstrap');
require('jquery-ujs');
require('./remote');

// resources/js/remote.js
$(document).on('ajax:success', function(e, xhr){
if(!$('#modal').length){
$('body').append($('<div class="modal" id="modal"></div>'))
}
$('#modal').html(xhr.responseText).modal('show');
});
NOTICE! (Update 06.04.2020)
In the example above I receive Modal's HTML from XHR as xhr.responseText
But some devs report that it doesn't work for them,
and xhr property already contains HTML, instead of object.
Please check what do you receive in xhr by logging first:
console.log(xhr);
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">@yield('title')</h5>
</div>
<div class="modal-body">@yield('content')</div>
<div class="modal-footer">@yield('footer')</div>
</div>
</div>
// Keep in mind there's no <div class="modal" id="modal"></div>

Usage of Modal Layout

To show the View in Modal Layout, extend it as you extend any other layout.

// Example of Create Post Form on Modal Layout in
//
resources/views/posts/create.blade.php
//
'layout.errors' simple template to show validation errors.
@extends('layouts.modal')@section('title') Demo Modal @endsection@section('content')<form action="/store" method="POST" data-remote="true">
@csrf
@include('layouts.errors')
<input type="email" name="email" class="form-control">
<input type="name" name="name" class="form-control">
<button type="submit" class="btn btn-primary">Close</button>
</form>
@endsection@section('footer')
<button type="button" data-dismiss="modal">Close</button>
@endsection
public function create()
{
return view('posts.create');
}
<a href="/posts/create" data-remote="true"> Create Post </a>
<a href="/posts/1/edit" data-remote="true"> Edit Post</a>
<a href="/posts/1" data-remote="true"> Show Post </a>
// App\Http\PostController.php...
public function destroy(Request $request, Post $post)
{
if($request->ajax()){
return view('posts.destroy', compact('post'));
}else{
$post->delete();
return redirect()->route('posts.index');
}
}
// ... some link anywhere
<a href="{{ route('posts.destroy') }}" data-remote="true"
data-method="DELETE">
Delete
</a>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store