$table->timestamp('something_at')->default(DB::raw('CURRENT_TIMESTAMP'));
from : http://laravel.so/tricks/dfdd910723f5e375a53170f04dcdf786
分享 Linux/Apache/PHP+MySQL/Smarty/JQuery/RubyOnRails 的點點滴滴!
$table->timestamp('something_at')->default(DB::raw('CURRENT_TIMESTAMP'));
app/Http/Middleware/NoHttpCache.php:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
class NoHttpCache implements Middleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
// This step is only needed if you are returning
// a view in your Controller or elsewhere, because
// when returning a view `$next($request)` returns
// a View object, not a Response object, so we need
// to wrap the View back in a Response.
if ( ! $response instanceof SymfonyResponse) {
$response = new Response($response);
}
/**
* @var $headers \Symfony\Component\HttpFoundation\HeaderBag
*/
$response->header('Pragma', 'no-cache');
$response->header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
$response->header('Cache-Control', 'no-cache, must-revalidate, no-store, max-age=0, private');
return $response;
}
}
app/Http/Kernel.php
中 $routeMiddleware
中注册它就可以在路由中使用了:...
use App\Http\Middleware\NoHttpCache;
...
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'no_http_cache' => NoHttpCache::class,
];
routes.php
中对要禁用缓存的路由添加中间件:Route::get('foo/bar', ['middleware' => 'no_http_cache', function () {
// ...
}]);
MVVM
模式,给开发带来了很多的便利,可读性、可维护性更高。然而自 Laravel 5.3
开始,VueJS
成为框架默认的标配。Laravel 5.1 LTS
版本引入 Vue 2.0
进行配置。Github
配置好, Laravel 5.1
和 Laravel 5.2
均有,Clone
下来后按照 README
安装依赖后即可用:https://github.com/jcc/vue-laravel-examplepackage.json
:package.json
, 可在 devDependencies
下配置你所需的所有依赖。我的配置如下: {
"private": true,
"scripts": {
"prod": "gulp --production",
"dev": "gulp watch"
},
"devDependencies": {
"bootstrap-sass": "^3.3.7",
"gulp": "^3.9.1",
"jquery": "^3.1.0",
"laravel-elixir": "^6.0.0-9",
"laravel-elixir-vue": "^0.1.4",
"laravel-elixir-webpack-official": "^1.0.2",
"laravel-elixir-browsersync-official": "^1.0.0",
"lodash": "^4.14.0",
"vue": "^2.0.0-rc.2",
"vue-resource": "^0.9.3",
"vue-router": "^2.0.0-rc.3"
}
}
$ npm install
npm install {package_name} --save-dev
的方式安装你所需的包。gulpfile.js
Laravel 5.1
的 gulpfile.js
内容如下: var elixir = require('laravel-elixir');
elixir(function(mix) {
mix.sass('app.scss');
});
ES6
的语法,因此我们修改如下: const elixir = require('laravel-elixir');
require('laravel-elixir-vue');
elixir(mix => {
mix.webpack('main.js');
});
mix.webpack('main.js')
是将 resources/assets/js
下的所有文件进行编译合并,合并到 public/js/main.js
文件。Vue
并创建基础例子resources/assets
文件夹下 创建 js/main.js
文件,该文件主要引入 vue 、vue-router
等所需的包。main.js
: import Vue from 'vue/dist/vue.js'
import App from './App.vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Example from './components/Example.vue'
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/example', component: Example }
]
})
new Vue(Vue.util.extend({ router }, App)).$mount('#app')
vue-router
需要 Vue.js 0.12.10+
并不支持 Vue.js 2.0
,因此以上的是根据 vue-router v2.0.0+
的版本配置,配置跟 0.12.10+
有明显区别。App.vue
:
js
文件夹下创建 components/Example.vue
文件Example.vue
:
{{ msg }}
vue
的配置已完成,接下来需要配置一下 Laravel
, 让 Laravel
成功引导到 Vue
JS
代码app/Http/routes.php
加入: Route::get('example', function () {
return view('example');
});
example.blade.php
模板
Example
JS
代码 $ gulp
gulp watch
created_at
和updated_at
,也有时候只有一个创建时间而没有更新时间,那么我们在 model 定义的时候怎么处理呢?public static function boot()
{
static::creating(function ($model) {
$model->created = $model->freshTimestampString();
});
static::updating(function ($model) {
$model->updated = $model->freshTimestampString();
});
}
created
的话,就去掉下面的static::updating
启动顺序:
1. DetectEnvironment Dotenv 配置
2. LoadConfiguration Config 配置 (/bootstrap/cache/config.php 或者 /config/*)
3. ConfigureLogging 日志系统配置
4. HandleExceptions php报错配置
5. RegisterFacades 注册外观
6. RegisterProviders 注册用户服务提供器
7. BootProviders 启动所有提供器
Route::pattern('id', '\d+');
Route::pattern('hash', '[a-z0-9]+');
Route::pattern('hex', '[a-f0-9]+');
Route::pattern('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}');
Route::pattern('base', '[a-zA-Z0-9]+');
Route::pattern('slug', '[a-z0-9-]+');
Route::pattern('username', '[a-z0-9_-]{3,16}');
overtrue changed the title from "wechat payment wike" to "[wiki] wechat payment" 20 hours ago
Issue::saving(function(Issue $issue){
if ($issue->isDirty('title')) {
$user = Auth::user()->username;
$oldTitle = $issue->getOriginal('title'); // 原始值
$newTitle = $issue->title; // 新值
ActionLog::log("$user 把标题 $oldTitle 修改为 $newTitle");
}
});
$issue->getOriginal('title')
用于获取修改前的值。Issue
模型的 saving
事件,当然这里的 ActionLog::log()
只是一个示例,至于你存到哪儿你随意。// OrderObserver.php
namespace App\Observers;
use App\Order;
use App\Services\SMS;
use Illuminate\Http\Request;
/**
* Order observer.
*/
class OrderObserver
{
/**
* After order created.
*
* @param Order $order
*/
public function created(Order $order)
{
(new SMS())->send('order_created', $order->consumer_phone, [$order->no]);
}
}
created
事件,即在订单创建后给用户发个短信。Model::unsetEventDispatcher();
可以移除事件调度器。我们就可以在 seed 文件里使用它来完成了这个需求了:Order::unsetEventDispatcher();
factory(App\Order::class, 10)->create(); // 创建10条假订单