PHP code example of service-to / uses-detail

1. Go to this page and download the library: Download service-to/uses-detail library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

service-to / uses-detail example snippets


Schema::create('your_table', function (Blueprint $table) {
    $table->id();
    $table->json('detail');
    $table->timestamps();
});

use ServiceTo\UsesDetail;

class YourModel extends Model
{
    use UsesDetail;
}

// Query any column - works automatically for both schema and detail columns
YourModel::where('name', 'John')->get();
YourModel::where('email', 'like', '%@example.com')->get();

// Use orWhere
YourModel::where('status', 'active')
         ->orWhere('priority', '>', 10)
         ->get();

// Use whereIn / whereNotIn
YourModel::whereIn('status', ['active', 'pending'])->get();
YourModel::whereNotIn('role', ['admin', 'superadmin'])->get();

// Use whereNull / whereNotNull
YourModel::whereNull('deleted_at')->get();
YourModel::whereNotNull('description')->get();

// Use whereBetween / whereNotBetween
YourModel::whereBetween('priority', [1, 10])->get();
YourModel::whereNotBetween('age', [18, 65])->get();

// Chain multiple conditions
YourModel::where('status', 'active')
         ->whereBetween('priority', [5, 15])
         ->whereNotNull('description')
         ->whereIn('category', ['A', 'B'])
         ->get();

// Use orderBy / orderByDesc
YourModel::orderBy('name')->get();
YourModel::orderByDesc('priority')->get();
YourModel::latest()->get();  // Orders by created_at desc
YourModel::oldest()->get();  // Orders by created_at asc

// Use groupBy with detail columns
YourModel::select('detail->category as category')
         ->selectRaw('COUNT(*) as count')
         ->groupBy('category')
         ->get();

// Group by multiple columns (mix schema and detail)
YourModel::select('status', 'detail->category as category')
         ->selectRaw('COUNT(*) as count')
         ->groupBy('status', 'category')
         ->get();

// Use having clause with groupBy
YourModel::select('detail->category as category')
         ->selectRaw('AVG(json_extract(detail, "$.priority")) as avg_priority')
         ->groupBy('category')
         ->having(\DB::raw('AVG(json_extract(detail, "$.priority"))'), '>', 5)
         ->get();

// Combine everything
YourModel::where('status', 'active')
         ->whereBetween('priority', [1, 10])
         ->groupBy('category')
         ->having(\DB::raw('COUNT(*)'), '>', 1)
         ->orderBy('category')
         ->get();

YourModel::detail('custom_field', '=', 'value')->get();
YourModel::detail('status', 'active')->get();

$model->merge($object);
$model->merge(['key' => 'value']);
$model->merge('{"key": "value"}');

// Find by primary key
$model = YourModel::find(1);

// Find by UUID (automatically detected if ID is 36 characters)
$model = YourModel::find('550e8400-e29b-41d4-a716-446655440000');