PHP code example of andydefer / laravel-mixins

1. Go to this page and download the library: Download andydefer/laravel-mixins 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/ */

    

andydefer / laravel-mixins example snippets


'providers' => [
    // ...
    AndyDefer\Mixins\MixinsServiceProvider::class,
],

// config/mixins.php

return [
    'slot_duration' => env('MIXINS_SLOT_DURATION', 30),
    'min_slot_duration' => env('MIXINS_MIN_SLOT_DURATION', 15),
];

use AndyDefer\Mixins\Traits\HasAvailabilityAttributes;
use Illuminate\Database\Eloquent\Model;

final class Doctor extends Model
{
    use HasAvailabilityAttributes;

    // Surchargez cette méthode pour ajouter des conditions personnalisées
    protected function isSchedulable(): bool
    {
        return $this->is_active && $this->user_type->isDoctor();
    }
}

$doctor = Doctor::find(1);

// Vérifier la disponibilité immédiate
if ($doctor->is_available_now) {
    echo "Le médecin est disponible maintenant";
}

// Récupérer le prochain créneau
$nextSlot = $doctor->next_slot;
if ($nextSlot) {
    $start = $nextSlot->getStart()->toDateTimeString();
    $end = $nextSlot->getEnd()->toDateTimeString();
    echo "Prochain créneau : $start - $end";
}

// Vérifier les disponibilités du jour
if ($doctor->has_availability_on_date) {
    $minutes = $doctor->total_available_minutes;
    echo "Disponible aujourd'hui : $minutes minutes";
}

use AndyDefer\Mixins\Traits\HasRatingAttributes;
use Illuminate\Database\Eloquent\Model;

final class Product extends Model
{
    use HasRatingAttributes;

    // Surchargez cette méthode pour ajouter des conditions personnalisées
    protected function isRateable(): bool
    {
        return $this->is_active && $this->status === 'published';
    }
}

$product = Product::find(1);

// Afficher la note moyenne
echo "Note moyenne : {$product->average_rating} / 5";

// Afficher le nombre d'avis
echo "{$product->rating_count} avis";

// Vérifier la présence d'avis
if ($product->has_ratings) {
    $distribution = $product->rating_distribution;
    // [1 => 2, 2 => 5, 3 => 10, 4 => 32, 5 => 78]
}

if ($doctor->is_available_now) {
    // Le médecin est disponible
}

$slot = $doctor->next_slot;
if ($slot) {
    echo $slot->getStart()->toDateTimeString();
}

if ($pharmacy->has_availability_on_date) {
    echo "La pharmacie est ouverte aujourd'hui";
}

$hours = $doctor->total_available_minutes / 60;
echo "Disponible {$hours}h aujourd'hui";

echo $product->average_rating; // 4.5

echo $product->rating_count; // 42

$distribution = $product->rating_distribution;
// [1 => 0, 2 => 0, 3 => 1, 4 => 2, 5 => 5]

if ($product->has_ratings) {
    // Afficher les évaluations
}



declare(strict_types=1);

namespace App\Models;

use AndyDefer\Mixins\Traits\HasAvailabilityAttributes;
use AndyDefer\Mixins\Traits\HasRatingAttributes;
use Illuminate\Database\Eloquent\Model;

final class Doctor extends Model
{
    use HasAvailabilityAttributes;
    use HasRatingAttributes;

    protected $fillable = [
        'name',
        'email',
        'is_active',
        'user_type',
    ];

    protected function isSchedulable(): bool
    {
        return $this->is_active && $this->user_type === 'doctor';
    }

    protected function isRateable(): bool
    {
        return $this->is_active && $this->user_type === 'doctor';
    }
}



declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Models\Doctor;
use Illuminate\Http\JsonResponse;

final class DoctorController
{
    public function show(Doctor $doctor): JsonResponse
    {
        return response()->json([
            'id' => $doctor->id,
            'name' => $doctor->name,
            'email' => $doctor->email,
            'available_now' => $doctor->is_available_now,
            'next_slot' => $doctor->next_slot,
            'has_availability_today' => $doctor->has_availability_on_date,
            'available_minutes' => $doctor->total_available_minutes,
            'average_rating' => $doctor->average_rating,
            'rating_count' => $doctor->rating_count,
            'rating_distribution' => $doctor->rating_distribution,
            'has_ratings' => $doctor->has_ratings,
        ]);
    }
}

$doctors = Doctor::all()->filter(fn($doctor) => $doctor->has_availability_on_date);

foreach ($doctors as $doctor) {
    echo $doctor->name . ' - ' . $doctor->average_rating . '⭐';
}
bash
php artisan vendor:publish --tag=mixins-config

tests/
├── Fixtures/
│   ├── migrations/
│   │   └── 0001_00_00_000001_create_test_tables.php
│   └── Models/
│       ├── TestCar.php
│       ├── TestPost.php
│       └── TestUser.php
├── Integration/
│   └── Traits/
│       ├── HasAvailabilityAttributesTest.php
│       └── HasRatingAttributesTest.php
└── IntegrationTestCase.php