laravel5数据库操作

4 篇文章 0 订阅


<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use DB;

class User extends Model {

	static function getResult(){
		$res=null;
		/*
		//插入
		$user=array('username'=>'test','password'=>'123456','age'=>23);
		$res = DB::table('users')->insert($user);
		*/
		
		/*
		//查询
		res = DB::table('users')->where('username','test')->first();//一维
		$res = DB::table('users')->where('username','test')->get(); //二维
		$res = DB::select('SELECT * FROM users WHERE username="test"');
		*/
		
		/*
		//更新数据
		$res =DB::update('UPDATE users SET age=25 WHERE username="test"');
		$res =DB::table('users')->where('username','test')->update(array('age' =>23));
		*/
		
		/*
		//删除数据
		$res = DB::delete('DELETE FROM users WHERE username="joy"');
		$res = DB::table('users')->where('username','joy')->delete();
		*/
		
		/*
		//字段添加
		$res = DB::statement('ALTER TABLE users ADD sex tinyint(1) not null default 0');
		*/
		
		/*事务
		DB::transaction(function(){
			$user=array(xxxx);
			DB::table('user')->insert($user);
			DB.....
		});
		*/
		
		/*
		//结果查询器
		$res = DB::table('users')->where('username','test')->pluck('age');//去特定字段值
		$res = DB::table('users')->lists('age');//特殊字段list值
		$res = DB::table('users')->select(array('id','username'))->get();多个特殊字段查询
		$res = DB::table('users')->where('id','>',1)->get();
		$res = DB::table('users')->where('id',1)->get();
		$res = DB::table('users')->whereId(1)->get();
		
		$res = DB::table('users')->where('id','<',3)->where('username','test')->get();
		$res = DB::table('users')->where('id','>',3)->orWhere('username','test')->get();
		
		
		$res = DB::table('users')->where('username','test')->where(function($query){
		$query->where('age','>',13)->orWhere('sex',0);
		})->get();//相当于username = 'test' and (age>13 or sex=0)
		
		$res = DB::table('users')->whereBetween('age',array(0,15))->get();
		$res = DB::table('users')->whereNotBetween('age',array(0,15))->get();
		$res = DB::table('users')->whereIn('age',array(23,30))->get();
		$res = DB::table('users')->whereNot In('age',array(23,30))->get();
		*/
		
		
		//$res = DB::table('users')->groupBy('sex')->orderBy('id','DESC')->having('sex','=',0)->get();
		//$res = DB::table('users')->limit(2,3)->get();
		//$res = DB::table('users')->skip(2)->take(1)->get();

		//$res = DB::table('posts')->join('users','posts.user_id','=','users.id')->get();
		// $res = DB::table('posts')->join('users',function($query){
		// $query->on('posts.user_id','=','users.id');//->On(...);
		// })->get();
		
		/*
		//嵌套查询
		$res = DB::table('users')->where(function($query){
		if(Input::get('status')){
		$query->whereStatus(Input::get('status'));
		}
		if(Input::get('source')){
		$query->whereSource(Input::get('source'));
		}
		})->get();
		*/
		
		/*	
		$res = DB::table('users')->whereExists(function($query){
			$query->select('*')->from('posts')->whereRaw(DB::raw('users.id = posts.user_id'));
		})->get();
		*/
		
		$res = DB::table('users')->where('id','>','2')->count();
		$res = DB::table('posts')->where('id',6)->decrement('view_count',5);//减5
		//$res=DB::table('posts')->truncate();//清表
		

		print_r($res);exit;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值