When building desktop applications with NativePHP, you’re not just stuck rendering static screens. You can bring in the full power of Laravel — including Models, Controllers, and Business Logic — and directly connect it to your app’s desktop UI.
In this article, we’ll cover how to:
-
Use Laravel Models to interact with the database
-
Create Controllers to handle logic and endpoints
-
Bind your backend logic with the NativePHP desktop frontend
-
Trigger UI updates based on model/controller actions
🎯 Why Use MVC in a Native App?
MVC (Model-View-Controller) is at the heart of Laravel’s clean architecture. NativePHP lets you apply that same architecture in desktop apps — allowing you to:
-
Keep your logic clean and modular
-
Reuse Laravel code you’ve already written
-
Connect your UI with real-time data updates
-
Scale the app more easily as it grows
📦 Step 1: Creating a Model
Let’s create a simple model called Task
for a to-do desktop app:
php artisan make:model Task -m
Migration (database/migrations/xxxx_create_tasks_table.php
):
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->boolean('completed')->default(false);
$table->timestamps();
});
Run migration:
php artisan migrate
🧠 Step 2: Writing a Controller
Create a controller to handle task-related logic:
php artisan make:controller TaskController
Inside TaskController.php
:
use App\Models\Task;
use Illuminate\Http\Request;
class TaskController extends Controller
{
public function index()
{
return Task::all();
}
public function store(Request $request)
{
return Task::create([
'title' => $request->input('title'),
]);
}
public function update(Request $request, Task $task)
{
$task->update([
'completed' => $request->input('completed'),
]);
return $task;
}
}
🌐 Step 3: Register Routes
Define API-style routes in routes/web.php
(or use api.php
):
use App\Http\Controllers\TaskController;
Route::get('/tasks', [TaskController::class, 'index']);
Route::post('/tasks', [TaskController::class, 'store']);
Route::put('/tasks/{task}', [TaskController::class, 'update']);
🖥 Step 4: Connect the UI to the Controller
Now that your logic and routes are ready, you can call them from your NativePHP desktop frontend using HTTP requests or NativePHP bridges.
Option 1: Use HTTP Requests (e.g., using fetch):
fetch('http://localhost:8000/tasks')
.then(res => res.json())
.then(data => console.log(data));
Option 2: Use Laravel Data in Blade-based UI
If you’re using Blade templates in your window:
use Native\Laravel\Facades\Window;
Window::open()
->route('dashboard')
->data([
'tasks' => \App\Models\Task::all(),
]);
In your Blade view (resources/views/dashboard.blade.php
):
<ul>
@foreach($tasks as $task)
<li>{{ $task->title }} - {{ $task->completed ? 'Done' : 'Pending' }}</li>
@endforeach
</ul>
🔄 Real-Time Syncing (Optional)
You can integrate Laravel Events or even Laravel Echo to send real-time updates to the Native window when models change — although this is more advanced and ideal for collaborative tools or live dashboards.
📌 Best Practices
-
Use Form Requests for validation in your controllers
-
Stick to RESTful routes to keep things organized
-
Keep the UI reactive — reload data after every create/update
-
Leverage Laravel’s Eloquent relationships for more complex UIs
🔚 Wrapping Up
Thanks to NativePHP, you don’t need to re-learn a new architecture or framework for desktop apps. Laravel’s MVC pattern fits naturally into the native world. With models powering your data and controllers handling business logic, you can build scalable, maintainable desktop apps — all in Laravel.
0 Comments