Resource Last updated: 2023-09-21
Iin this page we will know more about the resource file
# Generate a new Resource
# model property
# group property
# displayInMenu property
# icon property
# title property
# search property
# searchWithRelation property
# customName method to set the resource label to your customized language
# vertex method to add statistics or custom html in the heading page
# fields method to add and customize your inputs
# actions method to moderate or manage your data
# filters method to filter your data
# Generate new Resource
how can we make a new resource to use in our dash project
if you ran this command
php artisan dash:make-resource Articles
or you can add flag module to create on your Modules folder directly --module=Articles
First you will recieve an “Articles Resource generated” message .
This resource is generated in the default path App\Dash\Resources\Articles
<?php
namespace App\Dash\Resources;
use Dash\Resource;
class Articles extends Resource {
public static $model = \App\Models\Article::class ;
//public static $policy = \App\Policies\UserPolicy::class ;
public static $group = 'Articles';
public static $displayInMenu = true;
public static $icon = 'group'; // put <i> tag or icon name
public static $title = 'name';
public static $search = [
'id',
'name',
];
public static $searchWithRelation = [];
public static function customName() {
return 'Articles';
}
public static function vertex() {
return [];
}
public function fields() {
return [
id()->make('ID', 'id'),
];
}
public function actions() {
return [];
}
public function filters() {
return [];
}
}
Now you will have to append this Resource In App\Providers\DashServiceProvider
in the Dash/DashResources.php
are running in resources
method
<?php
namespace App\Providers;
use App\Dash\Dashboard\Help;
use Dash\DashServiceProviderInit;
class DashServiceProvider extends DashServiceProviderInit {
/**
* put your dashboard to rendering in home page
* @return array
*/
public static function dashboards() {
return [
Help::class,
];
}
/**
* Put Your Resources Here to register in Dashboard
* @return array
*/
public function resources() {
return include_once(app_path('Dash/DashResources.php'));
}
/**
* put notification class
* @return array
*/
public static function notifications() {
return [
];
}
/**
* Custom Blank Pages
* @return array
*/
public static function blankPages() {
return [
];
}
/**
* boot method
* please dont make any change here
*/
public function boot() {
parent::boot();
}
}
//app/Dash/DashResources.php
<?php
return [
// Users Start //
\App\Dash\Resources\AdminGroupRoles::class,
\App\Dash\Resources\AdminGroups::class,
\App\Dash\Resources\Admins::class,
\App\Dash\Resources\Users::class,
// Users End//
];
# Append Model In Your resource
Add your model path here so you can use it with your fields
public static $model = \App\Models\User::class;
you can group your resources by naming the group
public static $group = 'Users';
To hide or show your new resource in the main menu
public static $displayInMenu = true; // true => show , false => hide
You can set an icon to appear in the main menu by font-awesome 6.2
public static $icon = '<i class="fa fa-users"></i>';
This is responsible for setting the display of the resource master data from the same table and is used formally in relationships and any operations that are associated with the resource internally or externally and also with the resource master table
public static $title = 'name';
You can allow searching by fields only in the table without relationships
For example , the followig user ID , name , and email and more can be added according to your needs
public static $search = [
'id',
'name',
'email',
];
If there is a relationship with other tables, you can allow adding the method with allocating fields and this is an example
With the possibility of adding more than one relationship to allow the search
public static $searchWithRelation = [
'address'=>['id','place'],
];
You can customize a name dynamically and interactively according to the language you want whether it is Arabic or English or any other language that you want to show in the main menu of the dashboard
public static function customName() {
return __('dash.users');
}
This method is used to customize a forced and static query process according to your need and you can dispense with it if you want to prepare all the data
public function query($model) {
return $model->where('account_type', 'user');
}
This method is used to add statistics or any customization to display any data above the main table of the resource itself
public static function vertex() {
return [
Card::small()
->title('All Users')
->column(3)
->icon('<i class="fa fa-users"></i>')
->content(function () {
return User::where('account_type', 'user')->count();
})
->color('success') // primary,success,dark,info,
->render(),
view('customBladeFile')->render(),
];
}
This is the method responsible for adding all the items to the your resource file
and you will find that the ID is automatically added in it and then you can add the rest of your elements as you want
public function fields() {
return [
id()->make('ID', 'id'),
];
}
You will find everything you need to know about our magical fields and how to use it in the Elements section of our Documents
Through this method, you can add all the objects responsible for the actions of the resource
public function actions() {
return [
];
}
Through this method, you can add all the objects responsible for classifying and purifying the data according to the situation (Filters)
public function filters() {
return [
];
}
Additional methods and properties to control the Datatable
// datatable
public static $multiSelectRecord = true;
public static $searching = true;
public static $lengthChange = true;
public static $ordering = true;
public static $processing = true;
public static $serverSide = true;
public static $scrollCollapse = true;
public static $scrollY = false;
public static $scrollX = true;
public static $paging = true;
public static $lengthMenu = [10, 20, 25, 50, 100];
//full_numbers,numbers,simple,scrolling
public static $pagingType = 'full_numbers';
public static function dtButtons() {
return [
'pageLength',
'collection',
'selectedSingle',
'selectRows',
'selectColumns',
'selectCells',
'selectAll',
'searchPanes',
'searchBuilder',
'colvis',
'fixedColumns',
'colvisRestore',
'columnsToggle',
'colvisGroup',
'spacer',
'print',
'pdf',
'excel',
'csv',
'copy',
];
}
We're done with with this part. Let's find out something new :)