我们已经学会了如何通过db来操作数据库
那么如何通过model模型来操作呢?
首先
php bin/hyperf.php gen:model user
然后打开app\Model\User.php
<?php declare (strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $username
*/
class User extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'user';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = ['id' => 'integer', 'username'=>'string'];
}
添加controller
<?php declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\DbConnection\Db;
use App\Model\User;
class DbController extends AbstractController
{
public function index(RequestInterface $request)
{
$id = $request->input('id', 1);
$user = User::where('id', 1)->first();
return $user;
}
}
如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术
转载请注明:hyperf通过模型来操作mysql数据库 出自老鄢博客 | 欢迎分享