博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
laravel的MVC
阅读量:7013 次
发布时间:2019-06-28

本文共 2657 字,大约阅读时间需要 8 分钟。

一、laravel路由(应用中的大多数路由都会定义在 app/routes.php 文件中

routes.php

Route::get('/test','TestController@index');
一个路由就定义好了,当访问 http://xxx.com/public/index.php/test 时就会去找 app/controllers/TestController.php的index方法
TestController.php
class TestController extends Controller{        public function index(){                echo "hello world";    }}
一个控制器就建立好了,控制器也可以设置闭包,也可以设置别名
Route::get('user/profile', array('as' => 'profile', function(){    echo "hello world";}));
更多路由请看手册:

二、模型 

普通数据库操作

$results = DB::select('select * from users where id = ?', array(1));//查$results = DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle'));//增$results = DB::update('update users set votes = 100 where name = ?', array('John'));//更新$results = DB::delete('delete from users where id = ?',array(1));//删

查询生成器

//查$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();//查多条数据$obj = DB::table('archives')->where('typeid','=','1')->orderBy('id','desc')->first();//查询一条数据//where条件分组$sql = DB::table('archives')    ->where('typeid', '=', 1)    ->orWhere(function($query)    {        $query->where('id', '>', 2)                ->where('title', '<>', 'Admin');    })    ->get();//sql语句: select * from `la_archives` where `typeid` = 1 or (`id` > 2 and `title` <> 'Admin')//聚合查询$users = DB::table('archives')->count();//查总数$price = DB::table('archives')->max('id');//查最大值$price = DB::table('archives')->min('id');//查最小值$price = DB::table('archives')->avg('id');//查平均值$total = DB::table('archives')->sum('id');//查和//增$data = array('title'=>'标题八','body'=>'内容八','typeid'=>2);$obj = DB::table('archives')->insert($data);//改$data2 = array('title'=>'标题九','body'=>'内容九','typeid'=>2);$obj = DB::table('archives')->where('id',2)->update($data2);//删$obj = DB::table('archives')->where('id','>','100')->delete();

//模型(app/models/Archives.php)

class Archives extends Eloquent{        protected $table = 'archives';//表名    public $timestamps  = false;//不自动维护更新时间}
//模型操作和普通SQL查询一样
$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();$data = array('title'=>'新标题','body'=>'新内容');$arr = Archives::where('id','=',2)->update($data);

三、视图(app/views/home/test.blade.php)

$v){ echo $v->title.'
'; }?>

控制器中

public function test(){   $data = Archives::select('*')->get();   return View::make('home.test')->with('items',$data);}

转载于:https://my.oschina.net/tongjh/blog/194231

你可能感兴趣的文章
【跃迁之路】【712天】程序员高效学习方法论探索系列(实验阶段469-2019.2.2)...
查看>>
SpiderData 2019年2月18日 DApp数据排行榜
查看>>
react-refetch的使用小例子
查看>>
周末游攻略 - 南昌之行
查看>>
tcpdump查看Nginx长连接还是短连接
查看>>
Vue+thinkJs博客网站(二)之thinkJs的使用
查看>>
Electron学习笔记:主进程与渲染进程的通信方式
查看>>
JVM(六)为什么新生代有两个Survivor分区?
查看>>
Spark是一种基本的开源大数据技术
查看>>
Iterator 和 for...of 循环
查看>>
Font-face目前浏览器的兼容性
查看>>
Etcd超全解:原理阐释及部署设置的最佳实践
查看>>
聊聊flink的NetworkBufferPool
查看>>
MySQL主从同步机制和同步延时问题追查
查看>>
409. Longest Palindrome
查看>>
LeetCode 319. Bulb Switcher
查看>>
前端知识点——图片
查看>>
学汉语、来云栖、海外布道阿里云……这位印度架构师不一般
查看>>
240. Search a 2D Matrix II
查看>>
Leaflet-Develop-Guide
查看>>