-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTruffleExample.php
More file actions
72 lines (59 loc) · 2.51 KB
/
Copy pathTruffleExample.php
File metadata and controls
72 lines (59 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Waad\Truffle\Examples;
use Illuminate\Database\Eloquent\Model;
use Waad\Truffle\Enums\DataType;
use Waad\Truffle\Truffle;
/**
* Full-featured example demonstrating most Truffle capabilities.
*/
class TruffleExample extends Model
{
use Truffle;
protected $table = 'example_users';
protected $fillable = ['name', 'email', 'age', 'is_active', 'salary', 'department', 'metadata'];
protected $casts = [
'is_active' => 'boolean',
'age' => 'integer',
'salary' => 'float',
'metadata' => 'array',
];
protected $records = [
['id' => 1, 'name' => 'John Doe', 'email' => 'john@example.com', 'age' => 30, 'is_active' => true, 'salary' => 75000.00, 'department' => 'Engineering', 'metadata' => ['skills' => ['PHP', 'Laravel']]],
['id' => 2, 'name' => 'Jane Smith', 'email' => 'jane@example.com', 'age' => 28, 'is_active' => true, 'salary' => 80000.00, 'department' => 'Engineering', 'metadata' => ['skills' => ['React', 'Node.js']]],
['id' => 3, 'name' => 'Bob Johnson', 'email' => 'bob@example.com', 'age' => 35, 'is_active' => false, 'salary' => 65000.00, 'department' => 'Marketing', 'metadata' => ['skills' => ['SEO', 'Analytics']]],
['id' => 4, 'name' => 'Alice Brown', 'email' => 'alice@example.com', 'age' => 32, 'is_active' => true, 'salary' => 90000.00, 'department' => 'Management', 'metadata' => ['skills' => ['Leadership', 'Strategy']]],
];
protected $schema = [
'id' => DataType::Id,
'name' => DataType::String,
'email' => DataType::String,
'age' => DataType::Integer,
'is_active' => DataType::Boolean,
'salary' => DataType::Float,
'department' => DataType::String,
'metadata' => DataType::Json,
];
public function scopeActive($query)
{
return $query->where('is_active', true);
}
public function scopeInDepartment($query, string $department)
{
return $query->where('department', $department);
}
public function scopeWithSalaryAbove($query, float $amount)
{
return $query->where('salary', '>', $amount);
}
public function getFormattedSalaryAttribute(): string
{
return '$' . number_format($this->salary, 2);
}
}
// Usage:
// TruffleExample::all();
// TruffleExample::active()->count();
// TruffleExample::inDepartment('Engineering')->get();
// TruffleExample::withSalaryAbove(70000)->active()->orderBy('salary', 'desc')->get();
// TruffleExample::avg('salary');
// TruffleExample::paginate(10);