温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Laravel框架怎么对数据库进行CURD操作

发布时间:2020-12-11 15:25:33 阅读:192 作者:Leah 栏目:开发技术
亿速云云数据库,读写分离,安全稳定,弹性扩容,低至0.3元/天!! 点击查看>>

本篇文章给大家分享的是有关使用Laravel框架怎么对数据库进行CURD操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

一、Selects

检索表中的所有行

代码如下:

$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}

从表检索单个行

代码如下:

$user = DB::table('users')->where('name''John')->first();


var_dump($user->name);

检索单个列的行

代码如下:

$name = DB::table('users')->where('name', 'John')->pluck('name');

检索一个列值列表

代码如下:

$roles = DB::table('roles')->lists('title');

该方法将返回一个数组标题的作用。你也可以指定一个自定义的键列返回的数组

代码如下:

$roles = DB::table('roles')->lists('title''name');

指定一个Select子句

代码如下:

$users = DB::table('users')->select('name', 'email')->get();
 $users = DB::table('users')->distinct()->get();
 $users = DB::table('users')->select('name as user_name')->get();

Select子句添加到一个现有的查询$query = DB::table('users')->select('name');

代码如下:

$users = $query->addSelect('age')->get();

where

代码如下:

 DB::table('users')->where('votes''>'100)->get();

OR

代码如下:

$users = DB::table('users')->where('votes', '>'100)->orWhere('name', 'John')->get();

Where Between

代码如下:

$users = DB::table('users')->whereBetween('votes', array(1100))->get();

Where Not Between

代码如下:

$users = DB::table('users')->whereNotBetween('votes'array(1100))->get();
Where In With An Array

代码如下:

$users = DB::table('users')->whereIn('id', array(123))->get();$users = DB::table('users')->whereNotIn('id', array(123))->get();

以上就是使用Laravel框架怎么对数据库进行CURD操作,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×