http://my.oschina.net/tongjh/blog/194231
http://baike.baidu.com/view/1020297.htm
一、laravel路由(应用中的大多数路由都会定义在 app/routes.php 文件中)
routes.php
1 |
Route::get( '/test' , 'TestController@index' );
|
一个路由就定义好了,当访问 http://xxx.com/public/index.php/test 时就会去找 app/controllers/TestController.php的index方法
TestController.php
1 |
class TestController extends Controller{
|
2 |
public function index(){
|
一个控制器就建立好了,控制器也可以设置闭包,也可以设置别名
1 |
Route::get( 'user/profile' , array ( 'as' => 'profile' , function ()
|
更多路由请看手册: http://www.golaravel.com/docs/4.1/routing/
二、模型
普通数据库操作
1 |
$results = DB::select( 'select * from users where id = ?' , array (1)); //查
|
2 |
$results = DB::insert( 'insert into users (id, name) values (?, ?)' , array (1, 'Dayle' )); //增
|
3 |
$results = DB::update( 'update users set votes = 100 where name = ?' , array ( 'John' )); //更新
|
4 |
$results = DB:: delete ( 'delete from users where id = ?' , array (1)); //删
|
查询生成器
02 |
$obj = DB::table( 'archives' )->select( 'archives.*' , 'arctype.typename' )->where( 'archives.id' , '>' ,2)->join( 'arctype' , 'arctype.id' , '=' , 'archives.typeid' , 'left' )->orderBy( 'archives.id' , 'desc' )->skip(10)->take(5)->get(); //查多条数据
|
03 |
$obj = DB::table( 'archives' )->where( 'typeid' , '=' , '1' )->orderBy( 'id' , 'desc' )->first(); //查询一条数据
|
05 |
$sql = DB::table( 'archives' )
|
06 |
->where( 'typeid' , '=' , 1)
|
07 |
->orWhere( function ( $query )
|
09 |
$query ->where( 'id' , '>' , 2)
|
10 |
->where( 'title' , '<>' , 'Admin' );
|
12 |
->get(); //sql语句: select * from `la_archives` where `typeid` = 1 or (`id` > 2 and `title` <> 'Admin')
|
14 |
$users = DB::table( 'archives' )-> count (); //查总数
|
15 |
$price = DB::table( 'archives' )->max( 'id' ); //查最大值
|
16 |
$price = DB::table( 'archives' )->min( 'id' ); //查最小值
|
17 |
$price = DB::table( 'archives' )->avg( 'id' ); //查平均值
|
18 |
$total = DB::table( 'archives' )->sum( 'id' ); //查和
|
20 |
$data = array ( 'title' => '标题八' , 'body' => '内容八' , 'typeid' =>2);
|
21 |
$obj = DB::table( 'archives' )->insert( $data );
|
23 |
$data2 = array ( 'title' => '标题九' , 'body' => '内容九' , 'typeid' =>2);
|
24 |
$obj = DB::table( 'archives' )->where( 'id' ,2)->update( $data2 );
|
26 |
$obj = DB::table( 'archives' )->where( 'id' , '>' , '100' )-> delete ();
|
//模型(app/models/Archives.php)
1 |
class Archives extends Eloquent{
|
2 |
protected $table = 'archives' ; //表名
|
3 |
public $timestamps = false; //不自动维护更新时间
|
//模型操作和普通SQL查询一样
1 |
$arr = Archives::select( 'archives.*' , 'arctype.typename' )->where( 'archives.id' , '>' ,2)->join( 'arctype' , 'arctype.id' , '=' , 'archives.typeid' , 'left' )->orderBy( 'archives.id' , 'desc' )->skip(0)->take(10)->get();
|
2 |
$data = array ( 'title' => '新标题' , 'body' => '新内容' );
|
3 |
$arr = Archives::where( 'id' , '=' ,2)->update( $data );
|
三、视图(app/views/home/test.blade.php)
2 |
foreach ( $items as $k => $v ){
|
控制器中
1 |
public function test(){
|
2 |
$data = Archives::select( '*' )->get();
|
3 |
return View::make( 'home.test' )->with( 'items' , $data );
|