Laravel5.1 框架数据库查询构建器用法实例详解(3)
2.2 orWhere
orWhere和where接收的参数是一样的,当where逻辑没有查找到 or查找到了 返回or的结果,当where查找到了 or也查找到了 返回它们的结果。
public function getArticlesInfo()
{
$articles = DB::table("articles")->where('id','=','5')->orWhere('title','laravel database')->get();
dd($articles);
}
2.3 whereBetween和whereNotBetween
whereBetween是指列值是否在所给定的值之间:
public function getArticlesInfo()
{
$articles = DB::table("articles")->whereBetween('id', [1, 3])->get();
dd($articles);
}
↑ 上述代码是查找id在1~3之间的集合。
whereNotBetween和whereBetween相反:
public function getArticlesInfo()
{
$articles = DB::table("articles")->whereNotBetween('id', [1, 3])->get();
dd($articles);
}
↑ 上述代码是查找id不在1~3之间的集合。
2.4 whereIn和whereNotIn
whereIn是查找列值在给定的一组数据中:
public function getArticlesInfo()
{
$articles = DB::table("articles")->whereIn('id', [1, 3, 5, 8])->get();
dd($articles);
}
↑ 上述代码中是查找ID为1,3,5,8的集合,不过我们数据库中只有id为1和3的数据 那么它只会返回id为1和3的集合。
whereNotIn和whereIn相反的:
public function getArticlesInfo()
{
$articles = DB::table("articles")->whereNotIn('id', [1, 3, 5, 8])->get();
dd($articles);
}
↑ 上述代码中是查找ID不是1,3,5,8的集合。
2.5 whereNull和whereNotNull
whereNull是查找列值为空的数据:
public function getArticlesInfo()
{
$articles = DB::table("articles")->whereNull('created_at')->get();
dd($articles);
}
↑ 上述代码中是查找created_at为空的集合。
whereNotNull就不用说啦:
public function getArticlesInfo()
{
$articles = DB::table("articles")->whereNotNull('created_at')->get();
dd($articles);
}
↑ 上述代码中是查找created_at不为空的集合。
3 插入数据
先看下最简单的插入方法:
public function getInsertArticle()
{
// 插入一条数据:
DB::table('articles')->insert(
['title'=>'get more', 'body'=>'emmmmmm......']
);
// 插入多条数据:
DB::table('articles')->insert([
['title'=>'testTitle1', 'body'=>'testBody1'],
['title'=>'testTitle2', 'body'=>'testBody2'],
// ....
]);
}
内容版权声明:除非注明,否则皆为本站原创文章。
