Laravel5.7使用查询构造器和Eloquent ORM实现简单的增删改查操作

Laravel5.7使用查询构造器和Eloquent ORM实现简单的增删改查操作

准备一张表:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `m_pic`
-- ----------------------------
DROP TABLE IF EXISTS `m_pic`;
CREATE TABLE `m_pic` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
  `path` varchar(200) NOT NULL DEFAULT '' COMMENT '目录路径',
  `file_name` varchar(100) NOT NULL DEFAULT '' COMMENT '文件名',
  `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
  `update_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=553 DEFAULT CHARSET=utf8 COMMENT='系统图片表';

-- ----------------------------
-- Records of m_pic
-- ----------------------------
INSERT INTO `m_pic` VALUES ('1', '20160530', '574c4a00c86dd.jpg', '1464617472', '0');
INSERT INTO `m_pic` VALUES ('2', '20160530', '574c4fbbadb9e.png', '1464618939', '0');
INSERT INTO `m_pic` VALUES ('3', '20160530', '574c50ae09b1b.jpg', '1464619182', '0');
INSERT INTO `m_pic` VALUES ('4', '20160531', '574cee35f25da.png', '1464659510', '0');
INSERT INTO `m_pic` VALUES ('5', '20160531', '574d06a1bcf5c.jpg', '1464665761', '0');
INSERT INTO `m_pic` VALUES ('6', '20160531', '574d06ac8f2f0.jpg', '1464665772', '0');
INSERT INTO `m_pic` VALUES ('7', '20180526', '9b7cfc13b8fde7f635f37e7c6d4ac29c.jpg', '1527298563', '1527298563');
INSERT INTO `m_pic` VALUES ('8', '20180526', '9974fca13e218250a29895423cd10602.jpg', '1527298634', '1527298634');
INSERT INTO `m_pic` VALUES ('9', '20180526', '19f9e9c5b5d28db07d1d275443732a0a.jpg', '1527298634', '1527298634');
INSERT INTO `m_pic` VALUES ('10', '20180526', 'b82eee0c22dbde61fad4b909befcb8d0.jpg', '1527299936', '1527299936');
INSERT INTO `m_pic` VALUES ('11', '20180526', 'f107d87ff53afab4adb964b4e5c1d51d.jpg', '1527300203', '1527300203');
INSERT INTO `m_pic` VALUES ('12', '20180526', 'df146022f7dfb5958287c9225152fbab.jpg', '1527300384', '1527300384');
INSERT INTO `m_pic` VALUES ('13', '20180526', '315077cb6076b57e2f7860a62346fa09.jpg', '1527300642', '1527300642');
INSERT INTO `m_pic` VALUES ('14', '20180526', 'a5164488f3451e5061df3f86244cb9a7.jpg', '1527300668', '1527300668');
INSERT INTO `m_pic` VALUES ('15', '20180526', '28aba5920571957a2b80d50568d58725.jpg', '1527300879', '1527300879');

 

第一:使用查询构造器增删改查操作

 

第二:使用Eloquent ORM实现增删改查操作(需要定义模型)

1、在app目录下新建Model文件夹,使用 php artisan make:model Model/Pic 命令创建模型实例,

   如下:Taskmanager为项目名称,成功后再Model下新建一个Pic.php模型类

vagrant@homestead:~/Code/Taskmanager$ php artisan make:model Model/Pic
Model created successfully.

2、在Pic.php模型类中关联数据表

     /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'pic';

3、在Http/Controllers下的PicController控制器中实现模型查询,引入Pic.php模型类 use App\Model\Pic;

  //查询
  public function index(){
        //Pic::all()
        $pic = Pic::where('path',20160530)->orderBy('id','desc')->get();
        foreach ($pic as $picObject) {
            echo $picObject->file_name."
"; } dump($pic->toArray());die(); }

***使用 Eloquent 中的 all 和 get 方法可以检索多个结果,并会返回一个 Illuminate\Database\Eloquent\Collection 实例

 

第三:在项目的routes/web.php中配置

//            访问名          控制器       方法
Route::get('pic/getList', 'PicController@index');

第四:访问该地址 http://local.taskmanager.com/pic/getList,得到一下数据

Laravel5.7使用查询构造器和Eloquent ORM实现简单的增删改查操作_第1张图片

参考:

Laravel - 项目目录结构介绍(各文件夹功能说明):https://www.hangge.com/blog/cache/detail_2324.html

Laravel 5.7 中文文档:https://learnku.com/docs/laravel/5.7/routing/2253#basic-routing

你可能感兴趣的:(laravel)