-
首先在项目根目录下写好composer.json文件
{
“require”: {
“illuminate/database”: “>=5.5”,
}
} -
打开黑窗口,composer update
-
安装完成后,修改database.php数据库配置文件
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
‘driver’ => ‘mysql’,
‘host’ => $db[‘default’][‘hostname’],
‘database’ => $db[‘default’][‘database’],
‘username’ => $db[‘default’][‘username’],
‘password’ => $db[‘default’][‘password’],
‘charset’ => $db[‘default’][‘char_set’],
‘collation’ => $db[‘default’][‘dbcollat’],
‘prefix’ => $db[‘default’][‘dbprefix’],
]);// Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent();
-
修改autoload.php文件
将$autoload[‘libraries’] = array(); 改成 $autoload[‘libraries’] = array(‘database’); -
在根目录下index.php页面底部 添加require_once ‘./vendor/autoload.php’;
记住 一定要在require_once BASEPATH.‘core/CodeIgniter.php’;之前添加 -
创建Reviews.php文件(控制器)
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Reviews extends CI_Controller { // eloquent orm写法 public function index($id=1){ $this->load->model("Reviews_model"); $student = Reviews_model::find($id); var_dump($student); } } ?> -
创建Reviews_model.php(模型)
<?phpdefined('BASEPATH') OR exit('No direct script access allowed'); use iLLuminate\Database\Eloquent\Model as Eloquent; class Reviews_model extends Eloquent { protected $table = 'articles'; protected $fillable = ['title']; // ci框架原生写法 public function __construct() { $this->load->database(); } public function get_reviews( $id ){ if( $id != FALSE ){ $query = $this->db->get_where('student', array('id'=>$id)); // $query = $this->db->query("SELECT * FROM student LIMIT 10"); // return $query->result(); return $query->row_array(); } else { return FALSE; } } }
?>
CI 框架 融合 laravel框架的 orm
最新推荐文章于 2023-08-25 15:43:15 发布