Backend & integrations/PHP Laravel
Laravel
This example runs from source. Open the repository to clone, build, and run it locally.
Open on GitHubBackend & integrations
PHP Laravel
Laravel backend that exposes Gantt tasks and links through REST controllers and stores them with Eloquent models and migrations.
- readme.md
- GanttController.php
- TaskController.php
- LinkController.php
- gantt.blade.php
readme.md View on GitHub
# dhtmlxGantt with Laravel backend
Implementing backend for dhtmlxGantt using [Laravel](https://laravel.com/) framework.
### Requirements
- PHP 8.2 or higher
- [Composer](https://getcomposer.org/)
- MySQL 5.7+
### Setup
1. Install dependencies: `composer install`
2. Copy `.env.example` to `.env` (if not exists)
3. Create database and update connection settings in `.env`:
```
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password
```
4. Generate application key: `php artisan key:generate`
5. Create and populate tables: `php artisan migrate --seed`
### Run
- Start development server: `php artisan serve`
- Open browser at `http://127.0.0.1:8000`
### Related resources
[Complete tutorial](https://docs.dhtmlx.com/gantt/integrations/php/howtostart-php-laravel/)
[DHTMLX Gantt product page](https://dhtmlx.com/docs/products/dhtmlxGantt/)
[Documentation](https://docs.dhtmlx.com/gantt/)
[Blog](https://dhtmlx.com/blog/)
[Forum](https://forum.dhtmlx.com/)
app/Http/Controllers/GanttController.php View on GitHub
<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use App\Models\Task;
use App\Models\Link;
class GanttController extends Controller
{
public function get(): JsonResponse
{
$tasks = Task::orderBy('sortorder')->get();
$links = Link::all();
return response()->json([
"tasks" => $tasks,
"links" => $links
]);
}
}
app/Http/Controllers/TaskController.php View on GitHub
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Task;
class TaskController extends Controller
{
public function store(Request $request)
{
$data = $request->only(['text', 'start_date', 'duration', 'parent']);
$data['progress'] = $request->input('progress', 0);
$data['sortorder'] = Task::max('sortorder') + 1;
$task = Task::create($data);
return response()->json([
'action' => 'inserted',
'tid' => $task->id,
]);
}
public function update(Request $request, $id)
{
$task = Task::findOrFail($id);
$data = $request->only(['text', 'start_date', 'duration', 'parent']);
$data['progress'] = $request->input('progress', 0);
$task->update($data);
if ($request->has('target')) {
$this->updateOrder($id, $request->input('target'));
}
return response()->json([
'action' => 'updated',
]);
}
private function updateOrder($taskId, $target)
{
$nextTask = false;
$targetId = $target;
if (str_starts_with($target, 'next:')) {
$targetId = substr($target, strlen('next:'));
$nextTask = true;
}
if ($targetId === 'null') {
return;
}
$targetTask = Task::find($targetId);
if (!$targetTask) {
return;
}
$targetOrder = $targetTask->sortorder;
if ($nextTask) {
$targetOrder++;
}
Task::where('sortorder', '>=', $targetOrder)->increment('sortorder');
$updatedTask = Task::find($taskId);
$updatedTask->sortorder = $targetOrder;
$updatedTask->save();
}
public function destroy($id)
{
$task = Task::findOrFail($id);
$task->delete();
return response()->json([
'action' => 'deleted',
]);
}
}
app/Http/Controllers/LinkController.php View on GitHub
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Link;
class LinkController extends Controller
{
public function store(Request $request)
{
$link = Link::create($request->only(['type', 'source', 'target']));
return response()->json([
'action' => 'inserted',
'tid' => $link->id,
]);
}
public function update(Request $request, $id)
{
$link = Link::findOrFail($id);
$link->update($request->only(['type', 'source', 'target']));
return response()->json([
'action' => 'updated',
]);
}
public function destroy($id)
{
$link = Link::findOrFail($id);
$link->delete();
return response()->json([
'action' => 'deleted',
]);
}
}
resources/views/gantt.blade.php View on GitHub
<!DOCTYPE html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script src="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.js"></script>
<link href="https://cdn.dhtmlx.com/gantt/edge/dhtmlxgantt.css" rel="stylesheet">
<style type="text/css">
html, body{
height: 100%;
padding: 0px;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<div id="gantt_here" style='width:100%; height:100%;'></div>
<script type="text/javascript">
gantt.config.date_format = "%Y-%m-%d %H:%i:%s";
gantt.config.order_branch = true;
gantt.config.order_branch_free = true;
gantt.init("gantt_here");
gantt.load("/api/data");
const dp = gantt.createDataProcessor({
url: "/api",
mode: "REST"
});
</script>
</body>