本文介绍了如何在核心PHP中使用Laravel Eloquent模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用核心PHP开发一个项目,并且我想在我的项目中使用雄辩的查询结构来简化设置mySQL连接和执行mySQL查询的过程.
I am developing a project in core PHP and I want to use eloquent query structure in my project to make ease of setting up mySQL connections and executing mySQL queries .
推荐答案
-
开始使用
composer require illuminate/database vlucas/phpdotenv
创建一个引导文件以引导 Eloquent
的连接字符串:
create a bootstrap file to bootstrap Eloquent
's connection string:
//bootstrap.php
<?php
require 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$capsule = new Capsule;
$capsule->addConnection([
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
添加环境变量!!( .env文件)
创建模型文件,您可以将其放置在所需的任何位置
Create a model file, you can put this anywhere you want
//Models/User.php
<?php
namespace Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
}
使用它们!
Use them!
<?php
require('bootstrap.php');
use Models\User;
use Illuminate\Database\Connection as DB;
$user = User::find(1);
$user2 = User::where('name', 'somename')->first();
这篇关于如何在核心PHP中使用Laravel Eloquent模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!