One possible solution is to create a separate controller and view for the read-only datatable that is accessible to guest users. This controller would use the Filament DataTable class to generate the table data and pass it to the view. The view would then render the table using the Filament Blade components.
Here's an example of what the controller and view might look like:
// app/Http/Controllers/GuestController.php
use Filament\Tables\DataTable;
class GuestController extends Controller
{
public function index()
{
$table = DataTable::make(new MyDataTable())
->paginate(10);
return view('guest.index', [
'table' => $table,
]);
}
}
<!-- resources/views/guest/index.blade.php -->
<x-filament::app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Guest Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<x-filament::table :table="$table" />
</div>
</div>
</div>
</x-filament::app-layout>
In this example, MyDataTable is a custom Filament DataTable class that defines the columns and data for the table. The paginate method is used to limit the number of rows displayed on each page.
To restrict access to this controller to guest users, you can use Laravel's built-in middleware:
// app/Http/Controllers/GuestController.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class GuestController extends Controller
{
public function __construct()
{
$this->middleware(function (Request $request, $next) {
if (Auth::check()) {
return redirect('/dashboard');
}
return $next($request);
});
}
// ...
}
This middleware checks if the user is authenticated and redirects them to the dashboard if they are. Otherwise, it allows the request to continue to the controller action.
Finally, to avoid duplication of routes and views, you can use Laravel's route prefixing feature to group the guest routes under a common prefix:
// routes/web.php
use App\Http\Controllers\GuestController;
Route::prefix('guest')->group(function () {
Route::get('/', [GuestController::class, 'index'])->name('guest.index');
// add more guest routes here
});
// regular authenticated routes go here
This groups all the guest routes under the /guest prefix, so you can access the datatable at /guest instead of creating a separate subdomain or duplicating the routes and views.