for ($i = 0; $i<200;$i++){

         $co_id = 1;
         $asset = co_seating_desks::where('co_id','=',$co_id)->get()->random();
         //dd($asset);
         $user_id=\App\appModels\User::all()->random()->id;

         $status = ['verified','closed','canceled','pending'];
         $years=[2018,2019,2020];
         $from = \Carbon\Carbon::create($years[rand(0,2)],rand(1,12),rand(1,30),rand(1,24),rand(1,60),rand(1,60));
         $to = \Carbon\Carbon::parse($from)->addHours(rand(1,10));;
         $cost = rand(50,1000);
         DB::table('co_bookings')->insert([
             'co_id'=>$co_id,
             'user_id'=>$user_id,
             'co_seating_id'=>$asset->id,
             'capacity'=>$asset->capacity,
             'from'=>$from,
             'to'=>$to,
             'cost'=>$cost,
             'status'=>$status[rand(0,3)]
         ]);
     }


//model Knearest

<?php

namespace App\appModels\Classification;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Phpml\Classification\KNearestNeighbors;
use Phpml\Preprocessing\Normalizer;

class UserBookingStatusClassifier
{
    public $co_id;// specific co-working space ID
    private $data; //training data for specific co-working space
    private $X_train;
    private $Y_train;
    private $classifier;
    private $normalizer;

    public function __construct()
    {
        $this->co_id = \request()->session()->get('admin')->id;
        $this->data = [];
        $this->X_train = [];
        $this->Y_train = [];
        $this->normalizer = new Normalizer();
    }

    public function getDataFromDB(){

        $this->data = DB::select('select b.user_id, b.co_seating_id as asset, b.cost,
               TIMEDIFF(b.to,b.from) as bookingPeriod, b.capacity, a.price, b.status
               from co_bookings b inner JOIN co_seating_desks a
               on b.co_seating_id = a.id where b.co_id=? and b.from<? and b.user_id !=0',
               [$this->co_id,Carbon::now()]);
        return $this->data;
    }

    public function preProcessingData(){

        foreach ($this->data as $r){

            if ($r->status =="verified" || $r->status =="closed")
                $r->status = 1;
            elseif ($r->status =="canceled" || $r->status =="pending")
                $r->status = 0;
            $r->bookingPeriod = (int) explode(":",$r->bookingPeriod)[0];
            $this->X_train[]=[$r->user_id,$r->asset,$r->cost,$r->bookingPeriod,$r->capacity,$r->price];
            $this->Y_train[]=$r->status;
        }
    }

    public function train()
    {
        $this->normalizer->transform($this->X_train);

        $this->classifier = new KNearestNeighbors(2);
        $this->classifier->train($this->X_train,$this->Y_train);

        Cache::put('classifier:'.$this->co_id,$this->classifier,7*24*60);
        return $this->classifier;
    }

    public function predict($sample){


        $this->normalizer->transform($sample);
        if (Cache::has('classifier:'.$this->co_id))
            $this->classifier = Cache::get('classifier:'.$this->co_id);
        else{
            $this->getDataFromDB();
            $this->preProcessingData();
            $this->classifier = $this->train();
        }
        return $this->classifier->predict($sample);
    }

    public function getTrainingData(){
        return $this->data;
    }

}











     //model SVC

<?php

namespace App\appModels\Classification;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
use Phpml\Classification\KNearestNeighbors;
use Phpml\Preprocessing\Normalizer;
use Phpml\Classification\SVC;
use Phpml\SupportVectorMachine\Kernel;

class UserBookingStatusClassifier
{
    public $co_id;// specific co-working space ID
    private $data; //training data for specific co-working space
    private $X_train;
    private $Y_train;
    private $classifier;
    private $normalizer;

    public function __construct()
    {
        $this->co_id = \request()->session()->get('admin')->id;
        $this->data = [];
        $this->X_train = [];
        $this->Y_train = [];
        $this->normalizer = new Normalizer();
    }

    public function getDataFromDB(){

        $this->data = DB::select('select b.user_id, b.co_seating_id as asset, b.cost,
               TIMEDIFF(b.to,b.from) as bookingPeriod, b.capacity, a.price, b.status
               from co_bookings b inner JOIN co_seating_desks a
               on b.co_seating_id = a.id where b.co_id=? and b.from<? and b.user_id !=0',
               [$this->co_id,Carbon::now()]);
        return $this->data;
    }

    public function preProcessingData(){

        foreach ($this->data as $r){

            if ($r->status =="verified" || $r->status =="closed")
                $r->status = 1;
            elseif ($r->status =="canceled" || $r->status =="pending")
                $r->status = 0;
            $r->bookingPeriod = (int) explode(":",$r->bookingPeriod)[0];
            $this->X_train[]=[$r->user_id,$r->asset,$r->cost,$r->bookingPeriod,$r->capacity,$r->price];
            $this->Y_train[]=$r->status;
        }
    }

    public function train()
    {
        $this->normalizer->transform($this->X_train);

        $this->classifier = new SVC(
            Kernel::LINEAR, // $kernel
            1.0,            // $cost
            1,              // $degree
            null,           // $gamma
            0.0,            // $coef0
            0.001,          // $tolerance
            100,            // $cacheSize
            true,           // $shrinking
            true            // $probabilityEstimates, set to true
        );
        $this->classifier->train($this->X_train,$this->Y_train);

        Cache::put('classifier:'.$this->co_id,$this->classifier,7*24*60);
        return $this->classifier;
    }

    public function predict($sample){


        $this->normalizer->transform($sample);
        if (Cache::has('classifier:'.$this->co_id))
            $this->classifier = Cache::get('classifier:'.$this->co_id);
        else{
            $this->getDataFromDB();
            $this->preProcessingData();
            $this->classifier = $this->train();
        }
        return $this->classifier->predictProbability($sample);
    }

    public function getTrainingData(){
        return $this->data;
    }

}

