Creating a resource
A resource (similar to Nova) is a class that will define a full CRUDA for a model.
This means an overview table, create & edit forms and a detail page will be created for you.
Below you can find all steps that should be taken in order to fully set up a resource from scratch.
Creating a model
If you do not have a model already, you can create one.
Notice that because we're using multi tenancy, we need to place the files in a logical directory.
The model
php artisan make:model Tenant/Example
class Example extends Model
{
use HasFactory;
}
The migration
database/migrations/tenant/2021_05_20_115031_create_examples_table.php
Schema::create('examples', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('type');
$table->text('description');
$table->timestamps();
});
Migrating
Finally, to migrate run:
php artisan tenants:migrate
Setting up authorization
Qore uses Laravel policies in combination with Spatie Permissions for authorization.
Permission seeder
To create permissions, let's open database/seeders/Tenant/PermissionsSeeder.php
.
Qore comes with a helper class to easily create permissions:
$creator = app(PermissionCreator::class);
Let's create CRUDA permissions for our resource:
$creator->cruda('examples');
To fully refresh & seed the database run:
composer fresh
Policy
Let's start by creating a policy:
php artisan make:policy ExamplePolicy
The Qore User
model uses a trait to have extensive (scoped) permissions that will automatically be checked by the
helper methods you will find below:
class ExamplePolicy
{
use HandlesAuthorization;
public function viewAny(User $user)
{
return $user->canViewAny('examples');
}
public function view(User $user, Example $model)
{
return $user->canView('examples', $model);
}
public function create(User $user)
{
return $user->canCreate('examples');
}
public function update(User $user, Example $model)
{
return $user->canUpdate('examples', $model);
}
public function delete(User $user, Example $model)
{
return $user->canDelete('examples', $model);
}
}
Finally, register the policy in the AuthServiceProvider
:
protected $policies = [
Example::class => ExamplePolicy::class,
// ...
];
Creating the resource
Now that we have set up a model and authorization, we can create a Resource.
Let's create the ExampleResource.php
class in the app/Resources
directory (you can place it anywhere you want).
You can make this resource manually, or use the command: php artisan qore:resource ExampleResource
use Qore\System\Fields\Select;
use Qore\System\Fields\Text;
use Qore\System\Fields\Textarea;
use Qore\System\Resource\Field\FieldCollection;
use Qore\System\Resource\QoreResource;
class ExampleResource extends QoreResource
{
public function model(): string
{
return \App\Models\Tenant\Example::class;
}
public function fields(): FieldCollection
{
return new FieldCollection(
Text::make(__('Name'), 'name'),
Select::make(__('Type'), 'type')
->options(
[
[
'value' => 'type_1',
'label' => __('Type 1')
],
[
'value' => 'type_2',
'label' => __('Type 2')
]
]
),
Textarea::make(__('Description'), 'description')
);
}
public function icon(): string
{
return 'face';
}
// This is optional:
public function name(): string
{
return 'examples';
}
}
Registering the resource
Now the resource is created, we need to register it in order to use it.
By default, resources are automatically scanned based on your qore.php
configured directories.
If you want to manually register your Resource, you can register a resource in a ServiceProvider
, for example
in AppServiceProvider
:
public function boot()
{
app(Resources::class)->register(new ExampleResource());
}
In order to display the resource in the menu, we need to add the resource anywhere
in Http/Controllers/GlobalsController.php
.
Let's add the resource below our Home page:
private function menu(): Menu
{
return (new Menu())
->tab('home', function (MenuTab $tab) {
// HOME
$tab->addMenuItem(__('Home'), '/home', [], function (MenuItem $item) {
$item->setIcon('home');
});
// Here:
$tab->addResourceMenuItem(resource('examples'));
// ..
You should now see the resource in the front-end after doing a hard refresh.