Laraval 入门

配置与安装

需要条件:

- PHP >= 5.5.9 - OpenSSL PHP 扩展 - PDO PHP 扩展 - Mbstring PHP 扩展 - Tokenizer PHP 扩展

查看方式:phpinfo();

配置composer. Route::get(‘/’, function () { return view(‘welcome’); }); 配置~/.composer/vendor/bin到PATH

安装Laravel

sudo composer global require "laravel/installer=~1.1"

之后就laravel new blog 爽一爽

/config里都是配置文件 需要仔细看看.

配置/storage和bootstrap/cache服务器写权限

 chmod -R  777   storage/
 chmod -R  777   bootstrap/cache

ok,把项目放进apache路径 然后

http://localhost/laravel/public/

就会看到Laravel 5字样了

我们自己试一试弄一个页面吧

打开/app/Http/routes.php,设置Url跳转和传递title参数

Route::get('/hi',function(){
return View::make('hi')->with("title","Hello Word");
});

打开/resources/views创建hi.blade.php

@include('header')
<h1></h1>
<p>one Laravel</p>

再在其目录下创建header.blade.php

<p>header</p>

ok 访问urlhttp://localhost/laravel/public/index.php/hi

如何去掉可恶的index.php

  1. 设置apache支持.htaccess,打开apache http.conf,搜索AllowOverride 把None都改为All
  2. 设置加载mod_rewrite模块,打开apache http.conf,搜索mod_rewrite,把前面的#去掉(校验方法-php_info()),重启下apache哦~.
  3. 设置Laravel的.htaccess,把下面的内容代替之前的内容.

     Options +FollowSymLinks
     RewriteEngine On
    
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^ index.php [L]
    

okhttp://localhost/laravel/public/hi爽一发吧.

参考链接http://oublog.cn/u_58/

环境配置

//获取系统配置
config('app.timezone')
//设置系统配置
config(['app.timezone' => 'Asia/Shanghai'])

路由

访问Controllers

Route::get('/Text_Laravel/Text_DB','Text_Laravel.Text_DB@index');

访问View

Route::get('/User/Login',function () {
return view('User.Login');
});

数据库配制

配置文件config/database.php

Laravel还可以配置读写两个不同的ip地址.

'mysql' => [
'read' => [
    'host' => '192.168.1.1',
],
'write' => [
    'host' => '196.168.1.2'
],
'driver'    => 'mysql',
'database'  => 'database',
'username'  => 'root',
'password'  => '',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
],

光配置这个还不行 还要去配置.env ? 难道.env是通过config实时生成的?

阅读API文档

http://www.golaravel.com/laravel/docs/5.1/

routing

http://laravel.com/docs/5.1/routing

路由配置在app/Http/routes.php

先来看下简单的配置,get,post请求,还有match,any匹配多种请求,还有通过url传递参数(参数不能包含-),

应用中碰到的应用

获取路径

获取相对于目录的绝对路径

详见http://www.golaravel.com/laravel/docs/5.1/helpers/#paths

  1. 获取网络路径

  2. 关闭CSRF,App\Http;\Kernel,注释

      // \App\Http\Middleware\VerifyCsrfToken::class,
    

快速查找SQL语句

Route::get('/test-sql', function() {

DB::enableQueryLog();
$text = App\Models\Sample_Model::find(647)
    ->patient()
    ->Paginate(15)
    ->toJson();

return response()->json(DB::getQueryLog());
});

eg:
参考http://laravel.so/tricks/3c8bda45c15f494eaeba1690556aa40a

记录SQL到 日志

http://laravel.so/tricks/6086494cbfde9950e9118b1dad6270d5

Laravel级联查询

会调用patient()方法

Sample_Model::with('patient');

with属于懒加载..一般是最后需要用到的时候才执行

with记载上层数据时,不会显示被删除的数据

更改前

class Patient_Controller extends Controller{

public function query(Request $request){

$result = Patient_Model::withTrashed();
    ->orderBy("updated_at","desc")
    ->Paginate(15)
    ->toJson();

   return $result;
}
//更改前执行的sql
select count(*) as aggregate from `samples`
select * from `samples` order by `updated_at` desc limit 1 offset 0
select * from `patients` where  `patients`.`deleted_at` is null and `patients`.`code` in ('P-SZ150917134BrC')

更改后

public function query(Request $request){

  $result = Sample_Model::with(['patient' => function($q) {
    $q->withTrashed();
}])
->withTrashed()
->orderBy("updated_at","desc")
->Paginate(1)
->toJson();

return $result;
}

//更改后执行的sql是
select count(*) as aggregate from `samples`
select * from `samples` order by `updated_at` desc limit 1 offset 0
select * from `patients` where `patients`.`code` in ('P-SZ150917134BrC')

ok with 函数究竟是神马鬼?来看下源代码

  /**
 * Begin querying a model with eager loading.
 *
 * @param  array|string  $relations
 * @return \Illuminate\Database\Eloquent\Builder|static
 */
public static function with($relations)
{
    if (is_string($relations)) {
        $relations = func_get_args();
    }

    $instance = new static;

    return $instance->newQuery()->with($relations);
}   

首先他是懒加载,需要用的时候才加载,$relations可以是string也可以是数组,string的话就直接调用model的函数,数组的话,就是后面可以加判定条件.
参考链接http://stackoverflow.com/questions/33006470/laravel-query-delete-relationships

参考资料

安装Laravelhttps://github.com/maliang/LikeLaravel/blob/master/base/install.md

php

Comments