PHP code example of heroest / laravel-model

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

    

heroest / laravel-model example snippets


use Heroest\LaravelModel\Traits\Compatiable\Model;

/* 
    这条语句的作用相当于添加了一条connection到PDO连接池
    并取名为project, 最后在Model设置默认链接为project 
*/
$this->addConnection('project', [
            'type' => 'mysql',
            'host' => 'localhost',
            'username' => 'root',
            'password' => '664664',
            'db_name' => 'laravel_test',
            'port' => 3306,
        ]);

$this->addConnection('project', $pdo_object);



use Heroest\LaravelModel\Traits\Model;
//use Heroest\LaravelModel\Traits\Relationship;

class User
{
    use Model;

    protected $table = 'user';

    protected $primaryKey = 'id';

    protected $fillable = ['username', 'password', 'email'];

    protected $updated_at  = 'updated_at';

    protected $hidden = ['password'];

    public function __construct()
    {
        $this->addConnection('project', [
            'type' => 'mysql',
            'host' => 'localhost',
            'username' => 'root',
            'password' => '664664',
            'db_name' => 'laravel_test',
            'port' => 3306,
        ]);
    }

    public function ext()
    {
        return $this->hasOne(\Test\Model\UserExt::CLASS, 'uid', 'id');
    }
}

class UserExt
{
    use Model;

    protected $table = 'user_ext';

    protected $primaryKey = 'id';

    protected $fillable = ['uid', 'title'];

    protected $updated_at  = 'updated_at';

    public function __construct()
    {
        $this->addConnection('project', [
            'type' => 'mysql',
            'host' => 'localhost',
            'username' => 'root',
            'password' => '664664',
            'db_name' => 'laravel_test',
            'port' => 3306,
        ]);
    }
}

$model = new \Test\Model\User();

//$list = $model->with(['ext' => function($q){ $q->where('uid', '>', 0); }])->findMany([1,2,3]);
$model->beginTransaction();

$list = $model
            ->with(['ext' => function($q){
                $q->where(function($qu){
                    $qu->where('uid', '>', 1);
                    $qu->orWhere('uid', '<', 10);
                });
            }])
            ->leftJoin('user_ext e1', function($join){
                $join->on('e1.uid', '=', 'user.id');
                $join->on('e1.uid', '!=', 0);
                $join->orOn(function($join){
                    $join->on('e1.title', '!=', '');
                });
            })
            ->leftJoin('user_ext e2', function($join){
                $join->on('e2.uid', '=', 'user.id');
                $join->on('e2.uid', 0);
            })
            ->select('user.*')
            ->limit(3)
            ->get();

//ppd($list);
foreach($list as $user) {
    vp($user->toArray());
    //$user->username = mt_rand(100, 999);
    $user->save();
}
//$model->rollback();
$model->commit();
vp($model->getQueryLog());
/*
$model->fill(['username' => 'abc', 'password' => 'def', 'email' => '[email protected]'])->save();
$model->fill(['username' => 'cba', 'password' => 'fed', 'email' => '[email protected]'])->save();

vp($model->getQueryLog());
*/
 PHP