Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
4 / 12 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Supplier | |
33.33% |
4 / 12 |
|
25.00% |
1 / 4 |
26.96 | |
0.00% |
0 / 1 |
| boot | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| purchases | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAll | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| findById | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models\admin; |
| 4 | |
| 5 | use App\Support\Auditable; |
| 6 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 7 | use Illuminate\Database\Eloquent\Model; |
| 8 | |
| 9 | class Supplier extends Model |
| 10 | { |
| 11 | use Auditable, HasFactory; |
| 12 | |
| 13 | public const TABLE = 'suppliers'; |
| 14 | |
| 15 | protected $table = self::TABLE; |
| 16 | |
| 17 | protected $fillable = [ |
| 18 | 'name', |
| 19 | 'supplier_code', |
| 20 | 'logo', |
| 21 | 'contact', |
| 22 | 'email', |
| 23 | 'address', |
| 24 | 'details', |
| 25 | 'created_by', |
| 26 | ]; |
| 27 | |
| 28 | /** |
| 29 | * Automatic created_by value assigned from logged in user |
| 30 | * */ |
| 31 | protected static function boot() |
| 32 | { |
| 33 | parent::boot(); |
| 34 | |
| 35 | static::creating(function ($query) { |
| 36 | $query->created_by = auth()->id(); |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | public function purchases() |
| 41 | { |
| 42 | return $this->hasMany(Purchase::class); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Repository Methods |
| 47 | * */ |
| 48 | public static function getAll($is_pluck = false, $order_by_id_desc = true, $relation = false) |
| 49 | { |
| 50 | $query = self::query(); |
| 51 | if ($relation) { |
| 52 | return $query->with('purchases')->orderBy('id', 'desc')->get(); |
| 53 | } |
| 54 | ($order_by_id_desc ? $query->latest() : $query->orderBy('id')); |
| 55 | |
| 56 | return $is_pluck ? $query->pluck('name', 'id') : $query->cursor(); |
| 57 | } |
| 58 | |
| 59 | public static function findById($id, $relation = false) |
| 60 | { |
| 61 | $query = self::query(); |
| 62 | |
| 63 | return ($relation ? $query->with('purchases') : $query)->findOrFail($id); |
| 64 | // return self::query()->findOrFail($id); |
| 65 | } |
| 66 | } |