laravel5.5

1. 安装好之后,把根目录中的.env.example改成.env (并且可以配置数据库信息),访问就好啦!

控制器位置在app/http/controller目录下  模型层中安装的时候没有统一目录,是根据自己的习惯建个模型层就ok啦!或者在app目录下比如:User.php 视图层位于resources目录下

路由位于route目录下  数据库的配置文件在config目录下的database.php里

  Laravel提供了3种操作数据库方式:DB facade(原始方式)、查询构造器和Eloquent ORM。

2.数据库操作之DB facade

在app->Http->Controllers目录下新建一个控制器,代码如下:

<?php
namespace AppHttpControllers;
use AppHttpControllers;
use IlluminateSupportFacadesDB;
use AppHttpmodelTest;
class IndexController extends Controller {
 
    /**
     * 显示首页。
     *
     * @return Response
     */
    public function index(){
        //查询数据
        $Test = new Test();
        $list = $Test->readTest();
 
        //添加
        //$sql="insert into think_test (name,email) values ('lee','lee@gmail.com')";
        //$result=DB::insert($sql);
        $array_data=array();
        $array_data[0]['name']='tom';
        $array_data[0]['email']='tom@gmail.com';
        $array_data[1]['name']='jerry';
        $array_data[1]['email']='jerry@gmail.com';
        $result=DB::table('think_test')->insert($array_data);
        //dump($result);exit;
        if($result===false){
            echo 'insert error';exit;
        }
 
        //列表
        /*$sql="select name from think_test where id>1 order by id desc limit 0,20";
        $list=DB::select($sql);*/
        /*$list=DB::table('think_test')->select('id','name','email')->where('id', '>', 1)->offset(2)->limit(2)->orderBy('id', 'desc')->get()->toArray();
        $list=$this->object_to_array($list);*/
 
        //单行
        $info= DB::table('think_test')->where('id','>', '1')->first();
        $info=$this->object_to_array($info);
 
        //单字段
        $email = DB::table('think_test')->where('id','=', '3')->value('email');
 
        //指定字段增加或减少
        //$result=DB::table('think_test')->where('id','=', '1')->increment('votes', 5); //指定字段增加,返回的是影响的行数...
        //$result=DB::table('think_test')->where('id','=', '1')->decrement('votes', 5); //指定字段减少,返回的是影响的行数...
 
        //删除
        //$result=DB::table('think_test')->where('id','>', '8')->delete();    //返回影响的行数
 
        //清空表 删减表
        //$result=DB::table('think_test')->truncate();    //成功清空时返回null
        return view('index',compact('title','list','info','email'));
    }
 
}

 3.数据库操作之 - Eloquent ORM 模型的建立及查询数据。laravel所自带的Eloquent ORM 是一个ActiveRecord实现,用于数据库操作。每个数据表都有一个与之对应的模型,用于数据表交互。 

原文地址:https://www.cnblogs.com/cnn2017/p/9578136.html