LARAVEl快速入门

100 阅读2分钟

控制器 路由

  • 创建控制器artisan命令php artisan make:controller Demo/DemoController 命名规范遵循大驼峰
  • web路由层 为了方便分类整理故采用一个文件夹下一个分组的命名形式
Route::group(['prefix'=>'Demo'],function(){
    Route::get('/index','Demo\DemoController@index');
});
//此处路由别名为www.localhost.com/Demo/index
//他对应的是Controller文件夹下的Demo文件夹里面的DemoController控制器里面的index方法

模型 控制器

  • 创建模型的artisan命令 php artisan make:model Model/Demo 命名规范遵循一个表一个模型
  • 创建model成功后打开该文件
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    //
}

  • 随便写一个方法用于控制器去调用
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class demo extends Model
{
    //控制器调用的测试方法
    public static function demo(){
        return '这是一个测试方法';
    }

}
  • 在之前就创建好的DemoController控制器里面调用这个方法
<?php

namespace App\Http\Controllers\Demo;

use App\Model\demo;   //引入模型文件

use http\Env\Response;   //引入响应
use Illuminate\Http\Request;   //引入请求
use App\Http\Controllers\Controller;  //引入控制器基础
use Illuminate\Support\Facades\DB;  //引入数据库查询构造器
use Illuminate\Database\Eloquent\Model;  //引入模型基础


class DemoController extends Controller
{
    public function demo(){
    $aa = demo::demo();  //demo模型里面的demo方法
    dd($aa);   //打印出来
    }
}

数据库查询构造器简单的增删改查

  • 首先对在laravel框架下的.env文件还有config文件夹下面的database.php进行 配置文件进行数据库选项配置
  • .env配置文件 由于我用的数据库是mysql,所以在这就只配置了mysql的一些配置
APP_NAME=
APP_ENV=
APP_KEY=
APP_DEBUG=
APP_URL=

LOG_CHANNEL=

DB_CONNECTION=mysql   
DB_HOST=127.0.0.1    //链接地址
DB_PORT=3306        //端口号
DB_DATABASE=demo     //数据库名称
DB_USERNAME=root     //数据库用户名
DB_PASSWORD=****	 //数据库密码在 此用****号代替

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

  • 这是config/database.php配置文件里的配置
<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mysql'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',            //sql服务驱动
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),    //数据库链接地址
            'port' => env('DB_PORT', '3306'),         //链接端口
            'database' => env('DB_DATABASE', 'demo'),   //数据库名称
            'username' => env('DB_USERNAME', 'root'),   //用户名
            'password' => env('DB_PASSWORD', '****'),   //用户密码  在此处用****替代
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',                     //字符集
            'collation' => 'utf8mb4_unicode_ci',        //排序方式
            'prefix' => '',        						//数据库前缀
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer body of commands than a typical key-value system
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */
 //redis 缓存数据库
    'redis' => [

        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],

        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

    ],

];

  • 数据库增删改查简单的查询构造器的操作
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\DB;

class User extends Authenticatable
{
    use Notifiable;
 /**
     * 查询数据
     */
    public static function get($arr = ''){
        if ($arr == ''){
            return DB::table("user")->get();
        }else{
            return DB::table("user")->where($arr)->get();
        }
    }
    /**
     * 删除数据
     */
    public static function del($arr = ''){
        if ($arr == ''){
            return '请输入删除的条件,无条件将不进行删除';
        }else{
            return DB::table('user')->where($arr)->delete();
        }
    }
    /**
     * 更新数据
     */
    public static function updated($arr = '',$brr = ''){
        if ($arr == '' || $brr == ''){
            return '请输入需要更新的条件或值';
        }else{
            DB::table('user')->where($arr)->update($brr);
        }
    }
}