Introducing Laravel Playground
Introducing Laravel Playground #
I'm super excited to finally reveal a project that I have been working on for a couple of months already. The overall development was pretty time consuming and it involves a lot of interesting challenges to make this work - but I feel like it's finally in a state that I feel good about sharing with others.
Say hello to Laravel Playground:
What is Laravel Playground? #
Laravel Playground is a sandbox for your Laravel code and the spiritual successor of Tinkerwell Web. Laravel Playground makes it easy to quickly tinker with any PHP and Laravel code right in your browser. You can embed it into your blog, documentation or website easily!
While the code for Tinkerwell Web was running entirely in your browser, Laravel Playground is running your code on actual servers. This allows you to quickly try out features such as request validation, multiple Laravel routes or even Livewire components!
To get started with Laravel Playground, you can go ahead and take a look at this basic example:
In this example, we are creating two Laravel routes that each display a simple view. Try clicking on the link to the other route and you will see that the URL changes and the other view gets loaded.
This embedded Playground is also editable. You can click on the arrow next to the filename to open the file selection. Feel free to modify one of the views or the route definition file. Now press Ctrl+Enter to reload the code - boom you have updated the snippet right in your browser!
If you want to open the playground in a full-screen editor, just click on the "Edit on Laravel Playground" link on the upper right of the embed. This will open up https://laravelplayground.com with your embedded code prefilled. There you have the same ability to edit your code snippet and run your code.
Livewire support #
Laravel Playground comes pre-installed with a hand full of useful Laravel packages. As you can see, the Laravel Debugbar is enabled by default. Another package that you can make use of is Livewire.
Let's take a look at how this works:
As you can see, this playground gives you a ready-to-use Livewire component that you can modify and play with.
When you are on the Laravel Playground website, you can quickly create a new Livewire project (or choose from one of the other available presets) by clicking on the "+" button.
Eloquent models and database support #
Laravel playground also allows you to persist data to a SQLite database within each playground. All migrations that you create will automatically be migrated.
Embedding Playgrounds #
As you can already see in this blog posts, embedding your code is a big feature of Laravel Playground. That's why we made it extremely simple to embed your code - while still maintaining total control of your code on your own website, documentation or blog.
Let's say that you want to include this piece of PHP code in your blog post:
$slice = Str::of('This is my name')->after('This is');
Laravel Playgrounds prefill embeds can now execute this PHP code for you in a sandboxed environment. This means that you keep your code on your website and Laravel Playground executes and renders it for you.
Here's the example code embedded:
<?php $slice = Str::of('This is my name')->after('This is'); dd($slice);
The embed code that makes this work is very developer friendly. You can keep your code wrapped in <pre>
or <code>
blocks on your website and wrap them in a <div>
with attributes that control the embed, like this:
<div
class="laravel-playground"
data-height="500px"
>
<pre data-filename="index.php">
<?php
$slice = Str::of('This is my name')->after('This is');
dd($slice);
</pre>
</div>
<script src="https://embed.laravelplayground.com"></script>
When embedding your code you can configure and customize quite a lot of things, such as the theme, making embeds read-only, which file to open by default, to show or hide the result/code view, etc.
Closing note #
As I said in the beginning of this post: I'm super excited to finally share this with all of you as I've been working on this for a very long time. I hope you like Laravel Playground as much as I do and I can't wait to see it being used in blog-posts all over the place.
If you want to learn more, check out the official documentation on the Beyond Code website.