Installation Run these commands in your Laravel 10.x project (PHP 8.1+):
composer require sajadsdi/marketplace
php artisan marketplace:publish
php artisan marketplace:install
marketplace:publish copies config, migrations, and views to your project.marketplace:install runs migrations and sets up default configurations.First Use Case: Basic Product Listing
php artisan vendor:publish --tag=marketplace-views
Marketplace\Product (if customizing):
use Sajadsdi\Marketplace\Models\Product;
class MyProduct extends Product {}
/products) or define custom routes in routes/web.php:
use Sajadsdi\Marketplace\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
Sanctum Setup (Required) Ensure Sanctum is configured for API auth:
composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate
Add Sanctum middleware to api.php:
Route::middleware('auth:sanctum')->group(function () {
// Your marketplace API routes
});
Test the API
Use the provided Postman collection (postman/Marketplace.postman_collection.json) to interact with endpoints like:
GET /api/products (List products)POST /api/products (Create product, requires auth)Product Management
ProductController for basic operations. Extend it for custom logic:
namespace App\Http\Controllers;
use Sajadsdi\Marketplace\Http\Controllers\ProductController as BaseProductController;
class ProductController extends BaseProductController {
public function index() {
return parent::index()->withSearch(['category_id' => request('category')]);
}
}
app/Providers/AppServiceProvider:
use Sajadsdi\Marketplace\Rules\ProductRules;
public function boot() {
ProductRules::extend('custom_rule', function ($attribute, $value, $fail) {
// Custom logic
});
}
User Roles and Permissions
can:create-products). Example in app/Http/Kernel.php:
'create-product' => \Sajadsdi\Marketplace\Http\Middleware\CanCreateProduct::class,
Marketplace\Policies\ProductPolicy to define permissions:
public function create(User $user) {
return $user->hasRole('seller');
}
API Integration
ProductResource for API responses:
use Sajadsdi\Marketplace\Http\Resources\ProductResource;
return new ProductResource(Product::with('category')->get());
Marketplace\Events\ProductCreated event for custom logic:
event(new ProductCreated($product));
// Listen in EventServiceProvider
Frontend Integration
resources/views/vendor/marketplace/products/index.blade.php) or extend them:
@extends('vendor.marketplace.products.index')
@section('extra_script')
<script>console.log("Custom JS");</script>
@endsection
use Sajadsdi\Marketplace\Livewire\ProductList;
<livewire:product-list :categories="$categories" />
Search and Filtering
ProductQueryBuilder for scoped queries:
use Sajadsdi\Marketplace\Database\Query\ProductQueryBuilder;
$query = Product::query();
$query->applyFilters(request()->all());
$products = $query->get();
config/marketplace.php:
'filters' => [
'price' => [
'operator' => 'between',
'column' => 'price',
],
'category' => [
'operator' => '=',
'column' => 'category_id',
],
],
Sanctum Misconfiguration
401 Unauthorized even with valid tokens.auth:sanctum in api.php:
Route::middleware(['auth:sanctum', 'marketplace.auth'])->group(...);
config/sanctum.php for stateful and expiring settings.Migration Conflicts
marketplace:install after custom migrations fail due to column conflicts.database/migrations/ for overlapping columns (e.g., products table). Use:
php artisan marketplace:migrate --force
Warning: This may overwrite custom columns.Caching Headaches
php artisan cache:clear
php artisan config:clear
config/marketplace.php:
'cache_enabled' => env('APP_ENV') !== 'local',
Role-Based Access Denied
seller role can’t create products.Marketplace\Traits\HasRoles:
$user->assignRole('seller');
config/marketplace/roles.php for valid roles.Postman Collection Gaps
/api/products/{id}/reviews) or regenerate the collection using:
php artisan marketplace:generate-postman
Log Queries
Enable query logging in config/marketplace.php:
'debug' => [
'queries' => true,
'events' => true,
],
Check logs in storage/logs/laravel.log.
Event Listeners
Listen for Marketplace\Events\Product* events to debug lifecycle:
public function boot() {
event(new ProductCreated($product));
// Add to EventServiceProvider
}
Custom Validation
Override validation in app/Providers/AppServiceProvider:
public function boot() {
Validator::extend('custom_rule', function ($attribute, $value, $fail) {
\Log::debug("Validating $attribute: $value");
return true; // or false with $fail->fail(...);
});
}
Custom Models
Extend Sajadsdi\Marketplace\Models\Product to add fields:
class MyProduct extends Product {
protected $casts = [
'is_featured' => 'boolean',
];
}
Update migrations in database/migrations/.
Custom Controllers
Override methods in app/Http/Controllers/ProductController:
public function store(Request $request) {
$request->merge(['custom_field' => 'value']);
return parent::store($request);
}
Custom Views Publish and extend views:
php artisan vendor:publish --tag=marketplace-views
Override resources/views/vendor/marketplace/products/create.blade.php.
Custom API Resources
Extend ProductResource:
namespace App\Http\Resources;
use Sajadsdi\Marketplace\Http\Resources\ProductResource as BaseProductResource;
class ProductResource extends BaseProductResource {
public function toArray($request) {
$array = parent::toArray($request);
$array['custom_field'] = $this->custom_field;
return $array;
}
}
Custom Middleware
Add marketplace-specific middleware in app/Http/Middleware:
namespace App\Http\Middleware;
use Closure;
use Sajadsdi\Marketplace\Traits\HasRoles;
class EnsureSeller {
How can I help you explore Laravel packages today?