PHP code example of icehouse-ventures / laravel-mermaid
1. Go to this page and download the library: Download icehouse-ventures/laravel-mermaid library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
icehouse-ventures / laravel-mermaid example snippets
<x-mermaid::component>
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
</x-mermaid::component>
// In your controller
public function index()
{
$data =
'graph LR;
A[Label 1];
A-->B;
A-->C;
B[Label 2];
B-->D;
C[Label 3];
C-->D;
D[Label 4];';
return view('your-view', compact('data'));
}
// Your page blade file
<x-mermaid::component :data="$data" />
// In your controller
public function index()
{
$data = [
'A-->B',
'A-->C',
'B-->D',
'C-->D'
];
app('mermaid')->generateDiagramFromArray($data);
return view('your-view', compact('data'));
}
// Your page blade file
<x-mermaid::component :data="$data" />
// In your controller
public function index()
{
$collection = User::with('posts')->get();
$data = app('mermaid')->generateDiagramFromCollection($collection);
return view('your-view', compact('data'));
}
// Your page blade file
<x-mermaid::component :data="$data" />
$users = User::with('posts')->take(3)->get();
$data = [];
$data[] = "classDef user fill:#e0f2fe,stroke:#bae6fd,stroke-width:4px";
$data[] = "classDef post fill:#f0fdf4,stroke:#86efac,stroke-width:4px,color:#1e3a8a,stroke-dasharray: 5 5";
foreach ($users as $user) {
$data[] = "U{$user->id}((<a href="route('user.show', $user)">$user->name</a>))";
foreach ($user->posts as $post) {
$data[] = "P{$post->id}[<a href="route('post.show', $post)">$post->title</a>]";
$data[] = "U{$user->id} --> P{$post->id}";
}
}
$data[] = "class U1,U2 user";
$data[] = "class P1,P2,P3 post";
$data[] = "linkStyle default stroke:#94a3b8,stroke-width:4px";
$data = app('mermaid')->generateDiagramFromArray($data);
<x-mermaid::component :data="$data" class="border rounded p-2" />
// Your Livewire Class:
namespace App\Livewire;
use App\Models\User;
use Livewire\Component;
class Mermaid extends Component
{
public $limit = 2;
public $mermaid;
public function render()
{
$this->mermaid = app('mermaid')->generateDiagramFromCollection(
User::with('posts')->limit($this->limit)->get()
);
return view('livewire.mermaid');
}
}
// Your Livewire View:
<div>
<x-mermaid::livewire-component wire:model="mermaid" />
<div>
<label for="limit">Limit:</label>
<input wire:model.live="limit" id="limit" type="number">
</div>
</div>
bash
php artisan vendor:publish --provider="IcehouseVentures\LaravelMermaid\ServiceProvider" --tag="config"