laravel框架基础

1.路由

//基本路由
Route::get('basic1', function() {
    return 'hello basic1';
});
Route::post('basic2', function() {
    return 'hello basic2';
});
//多请求路由
Route::match(['get', 'post'], 'multy1', function() {
    return 'hello multy1';
});
Route::any('multy2', function() {
    return 'hello multy2';
});
//路由参数
Route::get('variable/{id}', function($id) {
    return '$id=' . $id;
});
Route::get('new-variable/{id}/{name?}', function($id, $name='Tom') {
    return '$id=' . $id . '&$name=' . $name;
})->where(['id'=>'[0-9]+', 'name'=>'[a-zA-Z]+']);
//路由别名:别名的作用便于在blade模板中使用route(别名)获得相对应url地址
Route::get('alias', [
    'as' => 'alias',
    function() {
        return 'hello alias';
    }
]);
//路由群组
Route::group(['prefix'=>'group'], function() {
    Route::get('first', function() {
        return 'hello group/first';
    });
});
//路由中输出视图
Route::get('/', function() {
    return view('welcome');
});
//关联控制器方法
Route::get('test/ceshi1', 'TestController@ceshi1');
Route::get('test/ceshi2',[
    'as' => 'ceshi2',
    'uses' => 'TestController@ceshi2',
]);

 

2.数据库操作
laravel有三种操作数据方式:原生sql语句、查询构造器、Eloquent ORM。

    public function test()
    {
        //原生sql语句
//注意:为了防止sql注入,先填充?后赋值 //新增:如果仅插入部分字段,修改database.php中mysql.strict为false
$boolean = DB::insert('INSERT INTO `test`(name, password) values(?, ?)', ['Rose', Hash::make('123456')]); dd($boolean);//布尔值 //删除 $affectRows = DB::delete('DELETE FROM `test` where id=?', [4]); dd($affectRows);//影响行数 //修改 $affectRows = DB::update('UPDATE `test` SET name=? where id=?', ['Jack', 5]); dd($affectRows);//影响行数 //查询 $list = DB::select('SELECT name,password FROM `test` where id>?', [1]); dd($list);//返回值类型:array+stdClass,取值$list[0]->name }
    public function test()
    {
        //查询构造器

        //新增:如果仅插入部分字段,修改database.php中mysql.strict为false
        $boolean = DB::table('test')->insert(['name'=>'Rose', 'password'=>Hash::make('123456')]);
        dd($boolean);//布尔值
        $insertId = DB::table('test')->insertGetId(['name'=>'Rose', 'password'=>Hash::make('123456')]);
        dd($insertId);//当前主键id

        //删除
        $affectRows = DB::table('test')->where('id', 14)->delete();
        dd($affectRows);//影响行数

        //修改
        $affectRows = DB::table('test')->where('id', '>', '5')->update(['password'=>Hash::make('123')]);
        dd($affectRows);//影响行数
        $affectRows = DB::table('test')->where('id', '>', '5')->increment('is_admin', 2);//加increment、减decrement
        dd($affectRows);//影响行数

        //查询
        $list = DB::table('test')->select('id', 'name', 'password')->where('id', '>', 7)->get();//所有数据
        dd($list);//返回结果集(collection),转换$tmp=$list->toArray(),取值$tmp[0]->name
        $info = DB::table('test')->orderBy('created_at', 'desc')->first();//一条数据
        dd($info);//返回StdClass,取值$info->name
$column = DB::table('test')->select('id', 'name', 'password')->where('id', '>', 7)->pluck('name', 'id');//获取指定列, 第二个参数可选,作为键名 dd($column);//返回collection,转换$tmp=$list->toArray(),取值$tmp[0]
DB::table('test')->select('id', 'name', 'password')->where('id', '>', 6)->orderBy('created_at', 'desc')->chunk(2, function() { //循环次数 = ceil(查询总条数 / chunk参数值 ) echo 'hello world' . PHP_EOL; }); }
    public function test()
    {
        //Eloquent ORM:最常用
        //insert insertGetId delete update increment decrement get first plunk chunk等查询构造器的方法都能使用
        $boolean = Test::insertGetId(['name'=>'Rose', 'password'=>Hash::make('123456')]);
        dd($boolean);
    }

Eloquent ORM的save()和create()方法:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

/**
 * 创建model类
 */
class Test extends Model
{
    //指定表名
    protected $table = 'test';
    //指定主键字段(可不写)
    protected $primaryKey = 'id';
    //是否创建updated_at created_at字段
    public $timestamps = true;
    //使updated_at created_at字段以时间戳的方式存储(根据自己时间字段的设置)
    public function getDateFormat()
    {
        return time();
    }
    //指定可以插入的字段:针对Model::create()方法
    protected $fillable = ['name','password'];
    //指定不可以插入的字段
    protected $guarded = ['is_admin'];
}

 ?>
    public function test()
    {
        //save()
        $test = new Test();
        $test->name = 'Jerry';
        $test->password = Hash::make('qq123');
        $boolean = $test->save();//自动填充created_at updated_at字段
        dd($boolean);

        //create()
        $model = Test::create([
            'name' => 'Nancy',
            'password' => Hash::make('qq123'),
        ]);
        dd($model);//返回模型
    }

猜你喜欢

转载自www.cnblogs.com/xincanzhe/p/10742765.html
今日推荐