Create Admin Panel with Laravel Backpack - Part 4
Create Admin Panel with Laravel Backpack - Part 4
Learn advanced features and customization options for your Laravel Backpack admin panel, including model relationships, file uploads, custom operations, filters, and validation rules.
Introduction
In this part of the series, we'll explore advanced features that will help you create a more powerful and customized admin panel. We'll cover relationships, file uploads, custom operations, and more.
Prerequisites
- Completed Part 1 of this series
- Basic understanding of Laravel relationships
- Familiarity with file uploads in Laravel
- Knowledge of Laravel validation
1 Working with Relationships
Let's add relationships to our Product model:
// One-to-Many Relationship
$this->crud->addField([
'label' => 'Category',
'type' => 'relationship',
'name' => 'category_id',
'entity' => 'category',
'attribute' => 'name',
'model' => 'App\\Models\\Category',
]);
// Many-to-Many Relationship
$this->crud->addField([
'label' => 'Tags',
'type' => 'relationship',
'name' => 'tags',
'entity' => 'tags',
'attribute' => 'name',
'model' => 'App\\Models\\Tag',
'pivot' => true,
]);
2 File Uploads
Implement file uploads for product images:
$this->crud->addField([
'name' => 'image',
'label' => 'Product Image',
'type' => 'image',
'crop' => true,
'aspect_ratio' => 1,
'disk' => 'public',
'prefix' => 'products/',
]);
3 Custom Operations
Add a custom operation to export products:
public function export()
{
$this->crud->hasAccessOrFail('list');
$products = $this->crud->model->all();
$csv = \League\Csv\Writer::createFromString('');
$csv->insertOne(['ID', 'Name', 'Price', 'Stock']);
foreach ($products as $product) {
$csv->insertOne([
$product->id,
$product->name,
$product->price,
$product->stock,
]);
}
return response($csv->toString())
->header('Content-Type', 'text/csv')
->header('Content-Disposition', 'attachment; filename="products.csv"');
}
4 Custom Filters
Add a price range filter to the list view:
$this->crud->addFilter([
'name' => 'price_range',
'type' => 'range',
'label' => 'Price Range',
'label_from' => 'Min',
'label_to' => 'Max',
'field_name' => 'price',
'from' => 'min_price',
'to' => 'max_price',
]);
5 Custom Validation Rules
Implement custom validation for SKU uniqueness:
$this->crud->setValidation([
'sku' => 'required|unique:products,sku,' . $this->crud->getCurrentEntryId(),
'name' => 'required|min:3',
'price' => 'required|numeric|min:0',
'stock' => 'required|integer|min:0',
]);
Key Features Covered
- Model relationships (one-to-many and many-to-many)
- File uploads with image cropping
- Custom CRUD operations
- Advanced filtering options
- Custom validation rules
Best Practices
- Use eager loading for relationships to prevent N+1 queries
- Implement proper file validation and sanitization
- Add helpful error messages for validation rules
- Use appropriate field types for your data
- Implement proper access control for custom operations
Common Issues
- File upload permissions and disk configuration
- Relationship performance with large datasets
- Validation rule conflicts
- Custom operation security considerations