Eloquent Resource
The easiest way to define what kind of Eloquent Model should be displayed in a Table is by passing the Model class to the $resource
property on your Table component:
class Users extends Table
{
protected ?string $resource = User::class;
}
Alternatively, you can define the resource using the resource()
method. It should return an Eloquent Builder instance or a string representing the Model class name. This method is useful when you need to display the data based on additional criteria:
use Illuminate\Database\Eloquent\Builder;
class ActiveAdministrators extends Table
{
public function resource(): Builder|string
{
return User::where('role', 'admin')->whereNotNull('activated_at');
}
}
The resource()
method can also be used to query a relationship. For example, if you want to display all the posts of the authenticated user, you can do it like this:
use Illuminate\Database\Eloquent\Relations\HasMany;
class Posts extends Table
{
public function resource(): HasMany
{
return auth()->user()->posts();
}
}
Please note that if you are using the authenticated user in the resource()
method, ensure that the Table component is rendered on a route protected by authentication middleware.
Stateful Resources
Up until now in the documentation, we have only seen table resources that are stateless, like User::class
and User::where('role', 'admin')
. Though they can contain constraints, you can't pass any additional data to them at runtime to change the query. Like regular classes in PHP, you can add a constructor to your Table class to inject dependencies. This can be useful to pass an Eloquent Model instance from the controller to the Table.
use App\Models\Company;
use App\Tables\Employees;
class CompaniesController
{
public function show(Company $company)
{
return inertia('Companies/Show', [
'employees' => new Employees($company),
]);
}
}
This Employees
Table class now accepts a Company
instance in its constructor. You can use this instance to query the employees based on the given company.
use App\Models\Company;
use Illuminate\Database\Eloquent\Relations\HasMany;
use InertiaUI\Table\Remember;
class Employees extends Table
{
public function __construct(
#[Remember] public Company $company
) { }
public function resource(): HasMany
{
return $this->company->employees();
}
}
Remember Attribute
You probably noticed the #[Remember]
attribute on the $company
property in the example above. This attribute is required if your Table has Actions or Exports. The Table package comes with a dedicated route to handle these features. The Remember
attribute ensures that the Company
instance is preserved in the URL of the Action and Export URL. It is generally recommended to only use literal values or Eloquent Models in the constructor.
The URL is encrypted, so the constructor parameters are not exposed to the user, and it is signed to prevent tampering. Even with these security measures, don't forget to implement proper authorization checks in your Actions and Exports.