laravel CURD

Posted wangxusummer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel CURD相关的知识,希望对你有一定的参考价值。

检索一个列值列表
DB::table("tablename")->lists(‘mobile‘); 返回 [ "13455556666", "13455556667", "13455556668", "13455556669", ]
指定一个自定义的键列返回的数组
\DB::table(‘tablename‘)->lists(‘mobile‘,‘email‘);
返回
[
"[email protected]"=>"13022223335",
"[email protected]"=>"13022223336",
"[email protected]"=>"13022223337",
]
检索表中的所有行

$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);
检索单行单列--返回指定字段
DB::table(‘users‘)->where(‘name‘, ‘John‘)->pluck(‘name‘);
返回数组 非字符串
["Jone"] 并不是返回 "Jone"
指定一个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();
where

$users = DB::table(‘users‘)->where(‘votes‘, ‘>‘, 100)->get();
$users = DB::table(‘users‘)->where([‘votes‘=>100,‘name‘=>‘zhangsan‘])->get();
OR 

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

Where Between

$users = DB::table(‘users‘)->whereBetween(‘votes‘, array(1, 100))->get();

Where Not Between

$users = DB::table(‘users‘)->whereNotBetween(‘votes‘, array(1, 100))->get();

Where In With An Array

$users = DB::table(‘users‘)->whereIn(‘id‘, array(1, 2, 3))->get();

$users = DB::table(‘users‘)->whereNotIn(‘id‘, array(1, 2, 3))->get();

Using Where Null To Find Records With Unset Values

$users = DB::table(‘users‘)->whereNull(‘updated_at‘)->get();

Order By
, Group By, And Having

$users = DB::table(‘users‘)->orderBy(‘name‘, ‘desc‘)->groupBy(‘count‘)->having(‘count‘, ‘>‘, 100)->get();

Offset
& Limit

$users = DB::table(‘users‘)->skip(10)->take(5)->get();
Joins
DB::table(‘users‘)
  ->join(‘contacts‘, ‘users.id‘, ‘=‘, ‘contacts.user_id‘)
  ->join(‘orders‘, ‘users.id‘, ‘=‘, ‘orders.user_id‘)
  ->select(‘users.id‘, ‘contacts.phone‘, ‘orders.price‘)
  ->get();

DB::table(‘users‘)
  ->leftJoin(‘posts‘, ‘users.id‘, ‘=‘, ‘posts.user_id‘)
  ->get();

DB::table(‘users‘)
  ->join(‘contacts‘, function($join)
  {
  $join->on(‘users.id‘, ‘=‘, ‘contacts.user_id‘)->orOn(...);
  })
     ->get();

DB::table(‘users‘)
  ->join(‘contacts‘, function($join)
  {
  $join->on(‘users.id‘, ‘=‘, ‘contacts.user_id‘)
  ->where(‘contacts.user_id‘, ‘>‘, 5);
  })
  ->get();
聚合
$users = DB::table(‘users‘)->count();
$price = DB::table(‘orders‘)->max(‘price‘);
$price = DB::table(‘orders‘)->min(‘price‘);
$price = DB::table(‘orders‘)->avg(‘price‘);
$total = DB::table(‘users‘)->sum(‘votes‘);
递增或递减一个列的值
DB::table(‘users‘)->increment(‘votes‘);
DB::table(‘users‘)->increment(‘votes‘, 5);
DB::table(‘users‘)->decrement(‘votes‘);
DB::table(‘users‘)->decrement(‘votes‘, 5);
指定额外的列更新
DB::table(‘users‘)->increment(‘votes‘, 1, array(‘name‘ => ‘John‘));
Inserts

DB::table(‘users‘)->insert(
array(‘email‘ => ‘[email protected]‘, ‘votes‘ => 0)
);

$id = DB::table(‘users‘)->insertGetId(
array(‘email‘ => ‘[email protected]‘, ‘votes‘ => 0)
);

多个记录插入到表中

DB::table(‘users‘)->insert(array(
array(‘email‘ => ‘[email protected]‘, ‘votes‘ => 0),
array(‘email‘ => ‘[email protected]‘, ‘votes‘ => 0),
));
Updates
DB::table(‘users‘)
->where(‘id‘, 1)
->update(array(‘votes‘ => 1));
Deletes
删除表中的记录

DB::table(‘users‘)->where(‘votes‘, ‘<‘, 100)->delete();
 
删除表中的所有记录

DB::table(‘users‘)->delete();

清空一个表

DB::table(‘users‘)->truncate();
共享锁
DB::table(‘users‘)->where(‘votes‘, ‘>‘,
100)->sharedLock()->get();

更新“锁”
DB::table(‘users‘)->where(‘votes‘, ‘>‘, 100)->lockForUpdate()->get();

 

事务 使用匿名函数 任何一个异常都会触发回滚

        return \DB::transaction(function() use($user_params, $staff_params) {
            //写管理员进入user表
            $user_id = DB::table(User::TABLE_NAME)
                ->insertGetId($user_params);
            if(!$user_id) throw new Exception("写入user表失败");

            $staff_params[Staff::DB_FIELD_USER_ID] = $user_id;
            $staff_id=DB::table(Staff::TABLE_NAME)
                ->insertGetId($staff_params);
            if(!$staff_id) throw new Exception("写入staff表失败");

            return $staff_id;
        });

 



































以上是关于laravel CURD的主要内容,如果未能解决你的问题,请参考以下文章

Laravel框架数据库CURD操作连贯操作使用方法

laravel 使用简介 curd

laravel CURD

Laravel框架查询构造器 CURD操作示例

轻松搞定laravel的curd操作搞定简易留言版

PHP笔记-laravel框架中使用DB facade实现CURD