Files
hub/app/Http/Controllers/DriverController.php
kelly 5a1570468e B2B Improvements and Fixes
 PHASE 1-2 COMPLETE SUMMARY
4 Traits Created:
 BelongsToBusinessDirectly - For models with direct business_id
 BelongsToBusinessViaBrand - For models via Brand relationship
 BelongsToBusinessViaProduct - For models via Product relationship
 BelongsToBusinessViaBatch - For models via Batch relationship
12 Models Updated:
Direct business_id (9 models):
 Brand - Added trait, removed scopeForBusiness()
 Order - Added trait
 Invoice - Added trait
 Component - Added trait, removed scopeForBusiness()
 Contact - Added trait
 License - Added trait
 Location - Added trait
 Driver - Added trait, removed scopeForCompany()
 Vehicle - Added trait, removed scopeForCompany()
Via relationships (3 models): 10.  Product - BelongsToBusinessViaBrand 11.  Batch - BelongsToBusinessViaProduct 12.  Lab - BelongsToBusinessViaBatch
Bugs Fixed:
 Manifest.php:569 - sellerCompany → sellerBusiness
All models now have consistent API:
Product::forBusiness($business)->get();
Brand::forBusiness($business)->get();
Order::forBusiness($business)->get();
// ... and 9 more models

 Phase 3 Complete: Seller Controller Refactoring
Successfully refactored 9 controllers to use trait-based business scoping:
Controllers Refactored:
Seller/ProductController.php - 5 edits
Replaced manual brand checks with $product->belongsToBusiness($business)
Replaced manual Brand queries with Brand::forBusiness($business)
Seller/Product/BomController.php - 5 edits
Replaced product ownership checks
Replaced Component queries with Component::forBusiness($business)
Simplified nested whereHas for recent components
Seller/BrandSwitcherController.php - 2 edits
Replaced manual Brand queries with trait scoping
Seller/ComponentController.php - 4 edits
Replaced Component queries and ownership checks
Seller/InvoiceController.php - 5 edits
Replaced Product queries with Product::forBusiness($business)
Replaced Invoice queries with nested Product scoping
Replaced manual product ownership checks in invoice validation
VehicleController.php - 4 edits
Replaced Vehicle queries and ownership checks
OrderController.php - 3 edits
Replaced Order queries with Product scoping
Replaced Driver and Vehicle queries
DriverController.php - 4 edits
Replaced Driver queries and ownership checks
BuyerSetupController.php - 1 edit
Replaced Contact query with trait scoping
Key Improvements:
Cleaner code: Replaced verbose manual where('business_id', $business->id) and whereHas('brand', ...) with concise forBusiness($business) calls
Consistent API: All models now use the same scoping pattern
Safer ownership checks: Replaced manual $model->relation->business_id !== $business->id checks with $model->belongsToBusiness($business)
Reduced duplication: Removed repeated scoping logic across controllers
Better maintainability: Changes to scoping logic now only need to be made in one place (the traits)

 Phase 4 Complete: All buyer controllers updated with business scoping
 Phase 5 Complete: TenantScopingTest created with 5 tests
 Phase 6 Complete: Company information update method added
 Code style fixed: Pint applied
 Caches cleared: Routes, config, views all cleared
 Permissions fixed: Storage logs now writable
New Buyer Routes (Business-Scoped):
/b/{business-slug}/cart
/b/{business-slug}/orders
/b/{business-slug}/invoices
/b/{business-slug}/checkout
Marketplace Routes (Cross-Tenant - No Change):
/b/browse - All products
/b/brands - All brands
2025-11-02 13:04:52 -08:00

112 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Driver;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class DriverController extends Controller
{
/**
* Display a listing of drivers for the company.
*/
public function index(\App\Models\Business $business)
{
$drivers = Driver::forBusiness($business)
->orderBy('created_at', 'desc')
->get();
return view('seller.fleet.drivers', compact('drivers', 'business'));
}
/**
* Store a newly created driver in storage.
*/
public function store(\App\Models\Business $business, Request $request)
{
$validated = $request->validate([
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'license_number' => ['required', 'string', 'max:255'],
'phone' => ['nullable', 'string', 'max:20'],
'email' => ['nullable', 'email', 'max:255'],
'transporter_id' => ['nullable', 'string', 'max:255'],
]);
Driver::create([
...$validated,
'business_id' => $business->id,
'created_by' => Auth::id(),
'is_active' => true,
]);
return redirect()
->route('seller.business.fleet.drivers.index', $business->slug)
->with('success', 'Driver added successfully!');
}
/**
* Update the specified driver in storage.
*/
public function update(\App\Models\Business $business, Request $request, Driver $driver)
{
// Ensure driver belongs to this business
if (! $driver->belongsToBusiness($business)) {
abort(403, 'Unauthorized action.');
}
$validated = $request->validate([
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'license_number' => ['required', 'string', 'max:255'],
'phone' => ['nullable', 'string', 'max:20'],
'email' => ['nullable', 'email', 'max:255'],
'transporter_id' => ['nullable', 'string', 'max:255'],
]);
$driver->update($validated);
return redirect()
->route('seller.business.fleet.drivers.index', $business->slug)
->with('success', 'Driver updated successfully!');
}
/**
* Remove the specified driver from storage (soft delete).
*/
public function destroy(\App\Models\Business $business, Driver $driver)
{
// Ensure driver belongs to this business
if (! $driver->belongsToBusiness($business)) {
abort(403, 'Unauthorized action.');
}
$driver->delete();
return redirect()
->route('seller.business.fleet.drivers.index', $business->slug)
->with('success', 'Driver deleted successfully!');
}
/**
* Toggle the is_active status of the specified driver.
*/
public function toggle(\App\Models\Business $business, Driver $driver)
{
// Ensure driver belongs to this business
if (! $driver->belongsToBusiness($business)) {
abort(403, 'Unauthorized action.');
}
$driver->update([
'is_active' => ! $driver->is_active,
]);
return response()->json([
'success' => true,
'is_active' => $driver->is_active,
]);
}
}