laravel学习笔记(二)
-
路由
HTTP方法:支持http1.1中所有类型传参方式,get,post,put,delete,options,patch
Route::get($url,$callback);
路由参数:
Route::get('user/{id}',function($id){return 'userid:'.$id});
路由分组:
Route::group(['prefix'=>'admin'],function(){ Route::get('users',function(){ //"admin/users" }) });
绑定模型:
//默认对应关系:user=>表users ,主键=>id
Route::get('api/users/{user}',function(App\user $user){ return $user->email; })
-
创建控制器
php artisan make:controller PostController
相关:
php artisan
php artisan help make:controller
-
创建路由(文章模块)
routes/web.php
//文章列表页
Route::get('/posts','\App\Http\Controllers\PostController@index');
//文章创建
Route::get('/posts/create','\App\Http\Controllers\PostController@create');
Route::post('/posts','\App\Http\Controllers\PostController@store');
//文章详情页
Route::get('/posts/{post}','\App\Http\Controllers\PostController@show');
//文章编辑
Route::get('/posts/{post}/edit','\App\Http\Controllers\PostController@edit');
Route::put('/posts/{post}','\App\Http\Controllers\PostController@update');
//文章删除
Route::get('/posts/delete','\App\Http\Controllers\PostController@delete');
-
创建控制器具体方法
位置:app/Http/Controllers/PostController.php
public function index(){ return view("post/index"); //模版渲染 } public function show(){ return view("post/show"); }
public function delete(Post $post){
$post->delete();
return(redirect('posts/')); //页面跳转
}
-
创建模板
在 resources/views/下创建post文件夹,在其中创建对应的 *.blade.php ,eg: resources/views/post/create.blade.php
-
模版语法
{{}}
@if @endif
@foreach @endforeach
注:参数传递 compact
-
继承模型
extends 引入公共模板 @extends('layout.main')
section 进行填充 @section("content") @endsection
yield 定义填充 @yield("content")
引入视图 include(模型再细化) @include('layout.footer')
-
数据表
migrate ,对所有的表的操作都以日志来记录,便于协同开发
表名(名词+s)
外键(表名_键名)
时间(created_at,updated_at)
-
数据库迁移
创建posts表:
php artisan make:migration create_posts_table
自动创建 database/migrations/2018_09_29_072923_create_posts_table.php
Q: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
A: app/Providers/AppServiceProvider.php boot()
use Illuminate\Support\Facades\Schema;
Schema::c(250);
自动检查sqllog并执行:
php artisan migrate
-
文章模块模型
ORM Laravel 的 Eloquent ORM 提供了漂亮、简洁的 ActiveRecord 实现来和数据库交互。
每个数据库表都有一个对应的「模型」用来与该表交互。你可以通过模型查询数据表中的数据,并将新记录添加到数据表中。
创建模型:
php artisan make:model Post
生成: app/Post.php //对应数据表posts,可通过 private $table = 'newtableName'; 来修改默认表名
about: php artisan help make:model
-
thinker 常驻进程
php artisan tinker
add: $post = new \App\Post(); $post->title="this is post'; $post->content="this is content'; $post->save(); select: $post->find(2); // \App\Post::find(2); 仅支持主键查找 \App:Post::where('title','zhangsan')->first(); \App:Post::where('title','zhangsan')->get(); modify $post = \App\Post::find(2); $post->title = 'hahazhangsan'; $post->save(); delete: $post->delete();
修改时区(默认英国时区)
config/app.php
'timezone' => 'Asia/Shanghai', //UTC
-
数据填充thinker
参考:https://github.com/fzaninotto/Faker
创建工厂,目录:database/factories/ModelFactory.php
$factory->define(App\Post::class,function(Faker\Generator $faker) { return [ 'title'=>$faker->words($nb = 3, $asText = true), 'content'=>$faker->text($maxNbChars = 100) ]; });
在tinker中(新创建的工厂记得重启tinker后才可生效)
php artisan tinker factory(App\Post::class,20)->make(); //仅显示,未真正添加至库中 factory(App\Post::class,20)->create(); //创建并写入库
-
分页
参考:https://laravel-china.org/docs/laravel/5.4/pagination/1260
//$lists = Post::orderBy('created_at','desc')->get(); $lists = Post::orderBy('created_at','desc')->paginate(5); // $lists = [['title'=>'nana'],['title'=>'nana2'],['title'=>'nana'],['title'=>'nana2'],['title'=>'nana'],['title'=>'nana2']]; {{$posts->links()}}
-
辅助函数
参考:https://laravel-china.org/docs/laravel/5.4/helpers/1252 ,eg:str_limit()
栗子:html原生输出:{!! str_limit($post->content,30,'...') !!}